Monday, 17 February 2014

Templates in c++

 


Apart from programming, template is one, which just defines the outline. Consider the leave letter template, which does not contain much information. As a template it contains only from address column but it does not have a from address. similarly it has a body section but it does not have for what purpose the leave is taken.
In c++, templates are much more similar to that. Here, in class template only the class name will be given while declaring. But which datatype the class uses will not be mentioned. This is more generic way of programming.
Then you may ask why there is need for template class?
Here the class does a same functionality for all datatypes. If template class was not there then you have to define the multiple class for each datatype that the class uses. But using templates we are creating only one class.

Real time example:

Consider the Arithemetic operation class. The datatype for the Arithemetic operation can be int, float, double. Inorder to define the multiple class we can just create the single template class and we can make specific whenever we are needed.

Syntax:

template <class type1> class template_class_name;
Eg:
template <class type1> class arithmetic_oper;

In above syntax, the template is keyword just to indicate the class is of type template. Then another new thing is <> brackets. while instantiation, the datatypes that are going to be used are mentioned there. In above we can allowed to instantiate using one type but we can give many as <class type1,class type2>.

DEFINING THE TEMPLATE CLASS:

This is just as normal class definition. In normal class definition you will be using the datatype as int. float etc for declaring the variable. But here you will be using the typename defined inside the angle brackets.
here type1 is identifier. This type1 can be int,float,double etc., 

This is decided while creation of object for the class.

template <class type1>class arithmetic_oper
{
   public:
      type1 variable_name;
     int fixed_type_int;
};


DIFFERENCE BETWEEN CLASS AND TYPENAME IN TEMPLATE:
There is no major technical difference between the class and typename. we can use them interchangeably. But at one rare case it is different. Need to explore that.


CREATING OBJECT FOR TEMPLATE CLASS:

arithmetic_oper <int> int_operation;

Here the arithmetic_oper is template class name. Here I want to make the class to use the int variable. so I have used int inside the angle brackets. I cannot give more than one type here. If I want to give more than one type then I need to declare the template class as <class type1,class type2>

CREATING TEMPLATE FUNCTION INSIDE THE  TEMPLATE CLASS:

Declartion of function template with new type inside the class template goes like this

template <class F_Type1> F_Type1  add(F_Type1,F_Type1);

Here the new type F_Type1 is created .
And the definition of template function goes like this
template <class c_Type1>
template <class type1>
type1 arithmetic_oper<c_Type1> ::  add(type1 a,type1 b)
{
   return a+b;
}
Here consider this, We have to specify the both templates such as class template c_Type1 and function template type1.

TYPE CASTING OF FUNCTION TEMPLATE:

consider the following snippet,
addition_value=(int)int_operation.add<int>(5.10,10);
Here the <int> represent the type casting to parameters. Here both parameter were different. If it was same, then compiler will auto deduct it. we are confusing the compiler in deducting the parameter type. So we explicitly indicate the compiler regarding parameter types. Then (int) is normal type casting. It stores the resultant value in to addition_value variable by typecasting to integer.

SAMPLE PROGRAM:

#include<iostream>
using namespace std;

template <class type1>
class arithmetic_oper; //declaring template

template <class type1>
class arithmetic_oper //defining template
{
   public:
      type1 variable_name; //Here we can change the datatype
      int fixed_type_int;  //cannot change the datatype

      // Using different type for function template
      template <class F_Type1> F_Type1  add(F_Type1,F_Type1);

      //Using same type as class for function template
      type1 sub(type1,type1);

};

template <class c_Type1>
template <class type1>
type1 arithmetic_oper<c_Type1> ::  add(type1 a,type1 b)
{
   return a+b;
}


template <class c_Type1>
c_Type1 arithmetic_oper<c_Type1> :: sub(c_Type1 a,c_Type1 b)
{
   return a-b;
}


int main()
{

   //Creating object as int using template
   arithmetic_oper <int> int_operation;
   int_operation.variable_name=10;
   printf("\nUsing a class as a integer :: %d ",int_operation.variable_name);
   
int addition_value=int_operation.add(5,10);
   printf("\nADDITION OF GIVEN VALUE IS %d",addition_value);
   addition_value=(int)int_operation.add<int>(5.10,10); // Type casting as int.
   printf("\nADDITION OF GIVEN VALUE IS with type casting %d",addition_value);


   //Creating object as double using template
   arithmetic_oper <double> double_operation;
   double sub_value=double_operation.sub(50.52,25.02);
   printf("\nSUBTRACTION OF GIVEN VALUE IS %f",sub_value);

   //Creating object as char using template
   arithmetic_oper <char> char_operation;
   char_operation.variable_name='A';
   printf("\nUsing a class as char type :: %c ",char_operation.variable_name);

   return 0;
}

OUTPUT:



Using a class as a integer :: 10
ADDITION OF GIVEN VALUE IS 15
ADDITION OF GIVEN VALUE IS with type casting 15
SUBTRACTION OF GIVEN VALUE IS 25.500000
Using a class as char type :: A