The perform same operation on different data type we use tamlate. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>. You can use templates to define functions as well as classes.

 

Type of template  

            1. Function template

            2. Class template

 

Syntax of template –

            void sum ( int a , int b ){

                        cout<< a+b <<endl;

            }

            void sum ( float a , float b ){

                        cout<< a+b <<endl;

            }

 

Function template 

When we apply template on function called function template. Here, type is a placeholder name for a data type used by the function. This name can be used within the function definition

Example of function template

#include<iostream.h>

#include<conio.h>

template<class T>

void sum(T a,T b){

            cout<<a+b<<endl;

}

void main(){

            clrscr();

            sum(5,6);

            sum(1.5,6.3);

getch();

}

Output

        11

        7.8


Example of function template

#include<iostream.h>

#include<conio.h>

template<class T,class T1>

void display(T a,T1 b){

            cout<<a<<" "<<b<<endl;

}

void main(){

            clrscr();

            display(5,8.3);

            display('H',3);

            display(7,'s');

getch();

}

Output

        5       8.3

        H      3

              s


Class template 

When we apply template on class called class template. Just as we can define function templates, we can also define class templates. Here, type is the placeholder type name, which will be specified when a class is

instantiated. You can define more than one generic data type by using a commaseparated list.

Example of class template

#include<iostream.h>

#include<conio.h>

template<class T>

class A{

            T x;

            T y;

            public:

                        A(T i,T j){

                                    x=i;

                                    y=j;

                        }

                        void display(){

                                    cout<<x<<" "<<y<<endl;

                        }

};

void main(){

            A<int> obj(5,6);

            A<float> obj1(6.7,4.6);

            A<char> obj2('A','B');

            clrscr();

            obj.display();

            obj1.display();

            obj2.display();

getch();

}

Output

        5      6

        6.7  4.6

        A     B


Class template overloading  

Overloading a class template means having different sets of class template which different parameter list.

Example of class template overloading 

#include<iostream.h>

#include<conio.h>

template<class T>

void display(T x){

            cout<<x<<endl;

}

void display(char x){

            cout<<x<<endl;

}

void main(){

            clrscr();

            display(56);

            display(6.3);

            display('A');

getch();

}

Output

        56

        6.3

        A