templates in c++

Template introduction -: Template is one of the features added to c++. It is a new concept which enable us to define generic classes, functions and thus provides support for generic programming.

Generic programming is an approach where generic types are used as parameters in algorithm so that they work for a variety of suitable data type and structures. A template can be used to create a family of classes or function . Like a class template for an array class would enable us to create array of various data type such as int array and float array.

A template can be considered as a kind of a macro.When an object of a specific type is defined of actual use. the template definition for that class is substituted with the required data type since a template define with a parameter that would be replaced by a specified data type at the time of actual use of the class function.

The template can be used in two types

  1. Class Templates in c++
  2. Function Templates  in c++

 

Class Template in c++ -:  For understanding class template we take a example

 #include <iostream>

class vector
{
   int *v;
   int size;
public:
    vector (int m)
    {
       v= new int[size =m];
       for(int i=0; i<size; i++)
        v[i]= 0;
     }
    vector (int *a)
     {
      for(int i=0; i < size;i++)
       v[i]=a[i];
      }
      int operator*(vector &y)
      {
        int sum=0;
        for(int i=0; i<size; i++)
         sum+=this -> v[i] * y.v[i];
      return sum;
      }
};   

 

In this example the vector class can store an array of int number and perform the scalar product of two int vectors.as like

Int main ()
{
 int x(3)={1,2,3};
 int y(3)={4,5,6};
 vector v1(3);
 vector v2(3);
 v1=x;
 v2=y;
 int R =v1*v2;
 cout << "R=" <<R ;
 return 0;
} 

now suppose if we want to define a vector that can store an array of float value. we can do this by simply replacing the appropriate int declaration with float in the vector class. This means that we have to redefine the entire class all over again.

Assume that we want to define a vector class with the data type as a parameter and then use this class to create a vector of any data type instead of defining a new class every time .

Templates allow us to define generic classes. It is a simple process to create a generic class using a template with an anonymous type. The generic format of a class template is

template<class T>
class vector
{
   //.............
   //class member specification
   //with anonymous type T
   // where appropriate
   //......
   // ......
}

 

Example of class template with single parameter 

#include <iostream>

using namespace std;

const size 3;

templates <class T>

class vector
{
   T*v;           // type t vector

public:

vector ()
    
    {
        v = new T[size];
  
        for(int i=0; i< size ;i++)

           v[i] =0;

     }
  vector (T*a)
    
     {
         for(int  i=0; i<size; i++)
 
          v[i] = a[i];
     }

     T operator * (vector &y)

     {

        T sum =0

        for( int i=0; i<size ; i++)
           
           sum += this ->v[i] * y.v[i];

        return sum;
      }
};

Int main ()
{
 int x(3)={1,2,3};
 int y(3)={4,5,6};
 vector <int> v1;
 vector <int> v2;
 v1=x;
 v2=y;
 int R =v1*v2;
 cout << "R=" <<R <<"\n";
 return 0;
} 

The output of this program

R=32  

 

Class Template with multiple parameter :-  C++ programming language provide more than one generic data type in a class template. They are declared as a comma separated list within template specification as like here

 

template<class T,class T1,class T2...>
class vector
{
   //.............
   //.............
   //.............  body of the class
   // ............
   //.............
   // ............
}

for a example we take two generic data type in class

Class Template With multiple parameter :

#include <iostream>

using namespace std;


templates <class T1,class t2>

class vector
{
   T1 a;
   T2 b;

public:

vector (T1 x, T2 y)

   {
    a =x;
    b =y;
    }
   
  void show()
    {
      cout << a <<"and" << b <<"\n";
     }
}:



Int main ()
{
 Test <float, int> Test1(1.5,123)
  Test <int,char> Test2(100,'N')
  test1.show();
  test2.show();

return 0;
} 

Output of this example

1.5 and 150
100 and N

 

Function Template in c++ -:  Like the class template we can also define function template with different arguments types. The general format of a function template like

template<class T>
returntype functionname ( arguments of type T)
{
 //.............
 //.............
 //............. body of the function
 // ............ with type T
 //............. where appropriate
 // ............
}

The function template syntax is similar to that of the class template except that we are defining function instead of class. we must use the template parameter  T as and when necessary in the function body and int its arguments list.

#include <iostream>

using namespace std;


templates <class T>

void swap(T &x, T &y)


{
   T temp =x ;
    x = y;
   y= temp;
}

void fun (int m,int n,float a, float b)

{  

 cout <<"m and n before swap:" << m<<"" << n << "\n";
 swap(m,n);
  cout <<"m and n after swap:" << m<<"" << n << "\n";

   cout <<"a and b before swap:" << a<<"" << b << "\n";
   swap(a,b);
   cout <<"a and b after swap:" << a <<"" << b << "\n";

}

int main()
{
  fun(10,20,5.22,7.12);

   return );
 
}

 

output of this program

 

m and n before swap : 10 20
m and n after swap : 20 10
a and b before swap : 5.22 7.12
a and b after swap : 7.12 5.22

 

Function Template with multiple parameter in c++ -: Like the template class we can use multiple generic data type in the function template using a coma separated  as like this way

template<class T1,T2....> 
returntype functionname ( arguments of type T1,t2...)
 { 
     //.............
     //.............
     //............. body of the function  
     // ............ with type T  
     //............. where appropriate   
     // ............ 
}

Function with two generic type with example

#include <iostream>
#include <string>

using namespace std;

template <class T1, class T2>
void display(T1 x, T2 y)
 
{
  cout << x << "" << y << "\n";
}

int main()
{
  display (1986, "NETNIC");
  display (11.32, 4321);
  return 0;
}

output of this program

1986  NETNIC
11.32 4321

 

 

 

Leave a Comment