Template Theory


 

Templates

Template enables us to define generic class and generic functions.

Generic programming is an approach where generic type are used as parameter.

Function Template:

Syntax:

Template<class T>

Return type functionname (arguments of type T)

{

…...

}

 Example : 

#include<iostream.h>
#include<conio.h>
template <class T>
void swap(T &x, T &y)
{
T temp=x;
x=y;
y= temp;
}
void main()
{
int a=10,b=20;
cout<<"\n Before swap  "<<a<<"-"<<b;
swap(a,b);
cout<<"\n after swap  "<<a<<"-"<<b;
float a1=2.3,b1=5.3;
cout<<"\n Before swap  "<<a1<<"-"<<b1;
swap(a1,b1);
cout<<"\n after swap  "<<a1<<"-"<<b1;
getch();
}

Function template with multiple parameters is possible.

Example: 

//function template with multiple parameters.

#include<iostream.h>
#include<conio.h>
template<class T1, class T2>
void display(T1 x, T2 y)
{
cout<<"\n"<<x<<"- "<<y<<"\n";
}
void main()
{
clrscr();
display(9999,"abc");
display(234,45.67);
getch();
}

Overloading of function template is possible.

//overloading of function on template function.

#include<iostream.h>
#include<conio.h>
template<class T1>
void display(T1 x)
{
cout<<"\n template type function    "<<x;
}
void display( int x)
{
cout<<"\n ordinary function   " <<x;
}
void main()
{
clrscr();
display("abc");
display(234);
display(12.3);
getch();
}

Class template:

syntax:

Template<class t>

class classname

{

…….

};

Example

//class template with single parameter.

#include<iostream.h>

#include<conio.h>

template<class T1>

class test

{

T1 a;

public:

test(T1 x) { a=x; }

void show() cout<<endl<<a; }

};

void main()

{     clrscr();

test<int> o1(555);

test<float> o2(23.4);

test<char> o3('a');

o1.show();

o2.show();

o3.show();

getch();

}

Class template with multiple parameter

Syntax:

template<class t1,class t2, ..>

Class classname

{

…..

……

};

Example: 
//class template with multiple parameter.
#include<iostream.h>
#include<conio.h>
template<class T1, class T2>
class test
{
T1 a;
T2 b;
public:
test(T1 x, T2 y)
{
a=x;
b=y;
}
void show()
{
cout<<endl<<a<<"    "<<b;
}
};
void main()
{     clrscr();
test<int,int> o1(555,234);
test<float,int> o2(23.4,12);
test<char,float> o3('a',6.4);
o1.show();
o2.show();
o3.show();
getch();
}

Nested class templates

Templates can be embedded or defined within class or within templates.

Now they are referred to as member templates.

Member templates that are classes are referred to as nested class templates.

Nested class templates are declared as class templates inside the scope of the outer class.

The outer class can itself be a normal class or class template. Eg.

#include<iostream.h>
class outer
{
template <class T>
class inner
{
public:
T x;
inner(){}
inner( T t)
{ x=t;
}
};
inner <int>o1;
inner<char >o2;
public:
outer(int i, char c)
{
o1=i;
o2=c;
}
void display()
{
cout<<o1.x<<" "<<o2.x;
}
};
void main()
{
outer obj1(25,'D');
obj1.display();
}

Advantages of templates:

Reduced source code

Less disk needed to store the source file 

Easy to debug the program

Good documentibility.