Class templates in C++

Class template is a way of reusing the code.

When you cut a template of a circle and you can paint a red circle from the template and a green circle from the template.Ha, ha. 

Similar to that you write an array class template and create int array, float array and objects array etc from that template. 

Syntax is similar to that of function template.

 #define SIZE 30  
 #include<iostream>  
 using namespace std;  
 template <class T>  
 class Array  
 {  
   T elements[SIZE];  
 public:  
   Array(int val)  
   {  
    for(int i=0;i<SIZE;i++)  
     elements[i]=val;  
   }  
   void print()  
   {  
    for(int i=0;i<SIZE;i++)  
     cout<<elements[i];  
   }  
   T find_maximum()  
   {   
     T maximum = elements[0];  
     for(int i=1;i<SIZE-1;i++)  
       if(elements[i]>maximum)  
         maximum= elements[i];  
     return maximum;  
    }  
 };  
 Save this in a file Array.h. It should be .h file, not .cpp file. The normal practice of separation of class declaration and member definition does not work with templates. All the member functions should be in the same file as class declaration. 


So the syntax for class template is

 template<class T> class SomeClass{.....};

In the methods of the class, you can use T instead of variable type, as is used in T elements[SIZE]  and also in find_maximum() return type and maximum local variable.

To use this class, we must include the Array.h. To instantiate the class, we should use the syntax 

Array<int> a1; Array<float> a2; etc.

Let us write main function to use these instantiated classes in our program. 

 
 #include<iostream>   
 #include"Array.h"  
 using namespace std;   
 template <class T> class Array;  
 int main()  
 {    
   Array<int> int_array(22);  
   cout<<"maximum of the array is "<<int_array.find_maximum();  
   return 0;  
 }  

Keep two things in mind, in order not to get stuck.
  • Write the class template and save it in .h file
  • Include this .h file in main.cpp
  • Declare the template class in main.cpp 
The size of Array in this case can be a template parameter. That is to say a template can take a basic data type parameter. But in the instantiation, we must provide a constant value. 


template <class T,int size>
class Array
{
 T *element;
  
  
 public:
    Array(int n=1);
    T& operator [](int index);
    T& operator[](int index) const;
    ~Array();
    Array(const Array & obj);
    T& operator =(const T& other);
    T& get_element(int index);
};
template<class T,int size>
Array<T,size>::Array(int n)
{
  
 element = new T[n];
 for(int i=0;i<n;i++)
 {
  element[i]=(T)0;
 }
}
template<class T,int size>
Array<T,size>::Array(const Array &obj)
{
  
 element = new T[size];
 for(int i=0;i<size;i++)
 {
  element[i]=obj.element[i];
 }
}

template<class T,int size>
T& Array<T,size>::operator = (const T &other)
{
 if(this!=&other){
  delete []element;
   
 element = new T[size];
 for(int i=0;i<size;i++)
 {
  element[i]=other.element[i];
 }
 }
 
}
template <class T,int size>
T& Array<T,size>::operator[](int n)
{
 if(n>=0 && n<size)
     return element[n];
}
template <class T,int size>
T& Array<T,size>::operator[](int n) const
{
 if(n>=0 && n<size)
     return element[n];
}
template<class T,int size>
Array<T,size>::~Array()
{
 delete[]element;
}


template<class T,int size>
T & Array<T,size>::get_element(int index)
{
 return element[index];
}

template <class T,int size>
class sortedArray:public Array<T,size>
{
public:
 void print_elements();
};
int main()
{
      Array<int,7> ar1;
      Array<double,4> ar2;
}

  Few things to notice here. 
  • We have written member functions outside body of class
  • Each of member function definitions must start with the line template<class T,int size>
  •  When instantiating, size is given along with type. Second argument which is not a generic type, should be a literal or a constant.
  • As we are using free store, constructor, copy constructor and destructor should be defined to provide deep copy.

Default arguments to templates

Just like function parameters, a template parameter can also have default value.

If you change Array as
template <class T, int size=5>
class Array
{.......}

Then 
Array<float> ar3; 
is valid and takes size as 5.

Similarly 
template <class T=float, int size=5>
class Array
{.......}

Here if type is not given a float array is instantiated. 
Array ar4; 
will be a float array with 5 elements. 


Comments

Popular Posts