Templates or Generics

Templates help you achieve one of the most important goals in programming: the creation of reusable code. Through the use of template classes you can create structures that can be applied over and over again to a variety of programming situations. Generic functions and classes provide a powerful tool that you can use to expand your programming efforts. Once you have written and debugged a template class, you have a solid software component that you can use with confidence in a variety of different situations. You are saved from the dullness of creating separate implementations for each data type with which you want the class to work. Generic representation purpose we use < any letter >. That may be any character. 
There are two types of templates
1. class templates (or) template classes (or) Generic Classes
2. function templates (or) template Functions (or) generic function

Generic Classes
Generic classes are useful when a class uses logic that can be generalized. When you create a generic class, it can perform the operation you define, such as maintaining a queue or a linked list, for any type of data. The compiler will automatically generate the correct type of object, based upon the type you specify when the object is created.
The general form of a generic class declaration is shown here:
template <class Ttype> class class-name
{
...
}

Once you have created a generic class, you create a specific instance of that class using the following general form:
class-name <type> ob;
Addition of two numbers using template classes.
#include<iostream.h>
#include<conio.h>
template <class T>
class addition
{
public:
T read(T n1,T n2)
{
return n1+n2;
}
};
void main()
{
clrscr();
addition a;
cout<<endl<<"The sum of two numbers is "<<a.read(10,20);
addition b;
cout<<endl<<"The sum of two floats is "<<b.read(12.3,45.3);
getch();
}

swapping of two numbers
#include<iostream.h>
#include<conio.h>
template <class G>
class swap
{
public:
G n1,n2,t;
public:
void read()
{
cout<<endl<<"Enter two values"; cin>>n1>>n2;
}
void print()
{
cout<<endl<<"Before swapping a = "<<n1<<" and b= "<<n2;
t=n1;
n1=n2;
n2=t;
cout<<endl<<"After swapping a = "<<n1<<" and b= "<<n2;
}
};
void main()
{
clrscr();
swap s;
s.read();
swap t;
t.read();
swap u;
u.read();
t.print();
s.print();
u.print();
getch();
}

Generic Function
A generic function defines a general set of operations that will be applied to various types of data. The type of data that the function will operate upon is passed to it as a parameter. a generic function, a single general procedure can be applied to a wide range of data. A generic function is created using the keyword template. A generic function (that is, a function definition preceded by a template statement) is also called a template function.
The general form of a template function definition is shown here:
template ret-type func-name(parameter list)
{
// body of function
}

//addition of two numbers using templates.
#include<iostream.h>
#include<conio.h>
template <class T>
T read(T n1,T n2)
{
return n1+n2;
}
void main()
{
clrscr();
cout<<endl<<"The sum of two numbers is "<<read(10,20);
cout<<endl<<"The sum of two floats is "<<read(12.3,45.3);
getch();
}

Swapping of two numbers using templates
#include<iostream.h>
#include<conio.h>
template <class T>
void read(T n1,T n2)
{
T t;
cout<<endl<<"Before swapping a = "<<n1<<" and b= "<<n2;
t=n1;
n1=n2;
n2=t;
cout<<endl<<"After swapping a = "<<n1<<" and b= "<<n2;
}
void main()
{
clrscr();
read(10,20);
read('a','f');
read(12.3,45.3);
getch();
}