Templates/Generics
- - > Template is a key word.
- - > It constructs a family of related functions on class
- - > Template in C++ also called as generics
- - > parameterized templates enable you to construct a family of related functions on classes.
- - > Use templates when you need to write lots of nearly identical things.
There are 2 types of templates in C++
1.Function template
2.Class template
Example of a Template in C++
- - > Template in is a parameterized type which receive data type as an argument.
- - > One algorithm can be applied various data types
Generic function/function template
- - > Generic function perform operation on various data types
- - > Generic function is function generator ,which generate number of functions based on data type
- - > Programmer does not required to overload function, using generic function compiler generate number of functions based data types
Syntax
Template<class
type-name/place holder-name>
return-type function-name(parameters)
{
Statements;
}
Type-name is a place holder name which hold data type
Or
Type-name is a parameter –name which hold data type as argument
Example of a Template in C++
#include<isotream.h>
Template<class T>
T max(T x,T y)
{
If (x>y)
Return x;
Else
Return y;
}
Void main( )
{
Int M1=max(10,20);
Float M2=max(1.5,2.5)
Cout<<’/n Max of two integers”<<M1;
Cout<<’/n Max of two floats”<<M2;
}
E.g:
# include<iostream.h> (bubble sort)
template <class T>
void sort (T,*a,int size)
{
T temp;
for(int i=0;i<size;i++)
for (int j=0; j<size-1;j++)
{
if (a[j]>a[j+i}
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
void main()
{
int a [] ={ 20,10,40,50,30};
float b[]={1.5,1.4,1.3,1.1,1.2};
clrscr();
sort(a,5);
sort(b,5);
int i;
for(i=0;i<5;i++)
cout<<”\n”<<a[i}<<”\t”<<b[i];
}
Template having multiple parameters
# include <iostream.h>
template <class t1,class t2>
void print (T1x ,T2y)
{
cout<<”\n x=”<<x;
cout<<”\n y=”<<y;
}
void main()
{
print(10,1.5f);
printf(1.5f,2.5);
printf(65,’A’);
}
void print(intx,inty)
{
}
void print(int x, float y)
{
}
void print(int x, char y)
{
}
Class Template in C++
You use a class template(also called a generic class (for class generator) to define a pattern for class definitions
Generic container classes are good examples whether you have a list of intergers or any other type the basic operations are the same(insert, delete, index,etc)
you can define a class structure value with generate operations once at the system generate class definitions on the fly:
syntax :
template<class type-name,class type-name,…..>
class class-name
{
data members;
member functions;
}
Class template
Genaral form of defining an object of generic class
class-name<type>obj;
type is the name of the data that the class will be operating upon.
Example
#include<iostream.h>
template<class T>
class matrix
{
T int a[2] [2]; // we stop 2 types means int & float only T type
Templates
public:
void read_matrix()
{
for(int i=0;i<2;i++)
cin>>a[i][j];
}
void print_Matrix()
{
for(int i=0;i<2;i++)
{ for(int j=0;j<2;j++)
cout<<a[i][j]<<”\t”;
cout<<endl;
}}};
void main()
{
Matrx<float>M2;
Matrix<int>M1;
cout<<”\n size is”<<sizeof(M1);
M1.read_Matrix();
M2.read_Matrix();
M1.print_Matrix();
M2.Print_Matrix();
}
o/p: Size is 8
size is 16
2
4
Example
# include<iostream.h>
template<class T>
class stack
{
T a[10];
int top;
public :
stack()
{
top=-1;
}
void push(T e)
{
if(top)>9)
cout<<”\n stack is full”;
else
a[++top]=e;
}
void pop()
{
if(top<0)
cout<<”\n stack is empty”;
ele
cout<<endl<<a[top--];
}
};
void main()
{
stack<int>stack1;
stack<float>stack 2;
stack1.push(10);
stack1.push(20);
stack1.push(30);
stack1.pop();
stack1.pop();
stack2.push(1.5);
stack2.push(2.5);
stack2.pop();
}
Example
# include<iostream.h>
template<class T1,class T2>
class alpha
{
T1 x;
T2 Y;
public :
void need()
{
cout<<”\n input x,y values”;
cin>>x>>y;
}
void print()
{
cout<<”\n x=”<<x;
cout<<”\n y=”<<y;
}
};
void main()
{
alpha<int,float>a1;
alpha<float.double>a2;
clrscr();
cout<<endl<<sizeof(a1);
cout<<endl<<sizeof(a2);
}