GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Apr-2005, 12:53
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

an array class template


the question reads as follows :
write a program with class template Array. The template can instantiate an Array of any elelment type. Override the template with a specific defenition for an Array of float elements (class Array<float>).

i have created the following :
CPP / C++ / C Code:
#ifndef ARRAY_H
#define ARRAY_H

#include<iostream>

template< class T >
class Array {
	public :
        Array(int x = 0) { current_element = x; }//constructor
		void set_index(int i) 
		{
			if ((i < MAX_VAL) && (i >= 0) 
			current_element = i;
		}
		void set_curr(T t) { array[current_element] = t; }
		T get_curr() { return array[current_element]; }
     private:
		T array[MAX_VAL];
		int current_element;
};//end class Array defe

now how do i do that overriding thing?
i'm lost on that.
can anyone help plz?
  #2  
Old 25-Apr-2005, 05:01
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 924
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Sorry for the late reply...

You can create a specialisation of your template class. It is not supported by *old* compilers. I've created a new member function, Test, so you can test it yourself. I guess I express myself better in code


CPP / C++ / C Code:
#ifndef ARRAY_H
#define ARRAY_H

#include<iostream>

#define MAX_VAL 5

template< class T >
class Array {
public :
	Array(int x = 0) { current_element = x; }//constructor
	void set_index(int i) 
	{
		if ((i < MAX_VAL) && (i >= 0))
			current_element = i;
	}
	void set_curr(T t) { array[current_element] = t; }
	T get_curr() { return array[current_element]; }
	void Test(){ std::cout << "General called" << std::endl;}
private:
	T array[MAX_VAL];
	int current_element;
};//end class Array definition

template <> 
class Array <float>
{
public:
	void Test(){ std::cout << "Specialised called" << std::endl;}
};
#endif

Test it:

CPP / C++ / C Code:
#include "array.h"

int main()
{
	Array<int> a;
	Array<float> b;
	a.Test();
	b.Test();
	return 0;
}

If any questions, fell free to ask.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner::friend template class if13121 C++ Forum 1 04-Mar-2005 05:56
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 C++ Forum 2 19-Dec-2004 06:06
hashing help saiz66 C++ Forum 1 06-Jul-2004 06:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:59.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.