TEMPLATES
C++ Template
Template is one of the features added to c++ language recently. IT is a new powerful concept which enables us to define generic classes and functions and thus provides support for generic programming. the generic programming is an approach where the generic types are used as parameters in
algorithms so that they work for a variety of suitable data types and data structures.
A template in c++ language can be used to create a family of classes or functions. for example a class template for an array class would enable us to create the arrays of the various data types such as float array and int array. similarly we can define a template for a function mul(), that would help us create various version of mul() for multiplication int, double and float type values.
A template can be consider as a kind of macro. whenever an object of a specific type is defined for actual use, the
template definition for the class is substituted with the required data type. so since a temple is defined with a parameter that would be replaced by a specified data type at the time of actual use of the class
or function, the templates are sometimes called parameterized classes or functions
consider a vector class defined as follows
class vector
{
*v;
size;
:
vector(int m)
{
v=new int[size=m];
( i=0;i < size;i++)
v[i]=0;
}
vector(int *a)
{
( i=0;i < size;i++)
v[i]=a[i];
}
operator*(vector &y)
{
sum=0;
(i=0;i < size;i++)
sum+=this->v[i]*y-v[i];
sum;
}
};
The vector class can store an array of int numbers and perform the scalar product of two int vectors as
shown below
main()
{
int x[3]={1,2,3};
int y[3]={4,5,6};
vector v1(3);
vector v2(3);
v=x;
v2=y;
R=v1*v2;
cout << "R=" << r;
}
|
Now suppose we want to define a vector that can store an array of float values. We can do this by simply replacing the appropriate int declaration with float in the vector class. This means that we have to redefine the entire class all over again.
Assume that we want to define a vector class with the data type as a parameter and then use this class to create a vector
of any data type instead of defining a new class every time the template mechanism enables us to achieve this goal
As mentioned earlier , template all us to define generic classes . It is a simple process to create a generic class using a template with a anonymous type.
The general format of class template is
Template< class t >
classname
{
};
The template definition of vector class shown below the syntax of a template
template
class vector
{
T* v;
size;
:
vector( m)
{
v=new T[size=m];
for(int i=0;i < size;i++)
v[i]=a[i];
}
T operator*(vector &y)
{
T sum=0;
(int i=0;i < size;i++)
sum+=this->[i]*y-v[i];
sum;
}
};
|
The class template definition is very similar to an ordinary class definition except the prefix template< class t > and the use of type T. This prefix tells the compiler that we are going to declare a template and use T as a type name in the declaration.
Thus vector has become a parameterized class with the type T as its parameter . T may be substituted by any type including the user defined types. now we can create vectors for holding different data types
Note: The type T may represent a class name as well Example
Vector v3(6);
a class created from a class template is called a template class. The syntax for defining an object of a template class is
classname objectname(arglist);
|
This process of creating a specific class from a class template is called instantiation. The compiler will perform the error analysis only when an instantiation takes place. It is therefore advisable to create and debug an ordinary class before converting it into a template
in the below program show the use of vector class template for performing the scalar product of int type vectors as well as float type vector
EXAMPLE OF CLASS TEMPLATE
<iostream>
using namespace std;
size=3;
template < class T >
class vector
{
T* v;
:
vector()
{
v=new T[size];
(int i=0; i < size;i++)
{
v[i]=0;
}
}
vector(T* a)
{
(int i=0;i < size;i++)
v[i]=a[i];
}
T operator*(vector &y)
{
T sum=0;
for(int i=0;i < size;i++)
sum+=this->v[i]*y.v[i];
sum;
}
};
main()
{
x[3]={1,2,3};
y[3]={4,5,6};
vector < int > v1;
vector < int > v2;
v1=x;
v2=y;
r=v1*v2;
std::cout << "R=" << r << "\n";
}
|