Templates can be
used to define generic classes to operate differently on different data types.
Such classes are called class templates. A class template
specifies how individual classes can be constructed similar to normal class
specifications. These classes model a generic class which supports similar
operations for different data types.
For instance,
when a generic vector is created, it can be used for storing data of type
integer, real, double etc.
Consider a program for a vector
class, which takes an array of integers and performs the scalar product
of two integer vectors.
When we want to define a vector that
can store an array of float values, we have to redefine the class by replacing
int declarations with float.
Instead of that, if we can define a
vector class with data type as parameter
using templates, then the same
class can be used for creating a family of vectors for any data type rather than defining a new class every time. This
mechanism is called as class templates.
template <class T>
class classname
{
//--------------
//class data member and member
function specification
//--------------
};
- The prefix template < Class T> specifies that a template is being declared
- And that a type-name T will be used in the declaration
- T is a variable of template type can be in the place of data-type.
- Any call to the template classes, needs to be associated with a data type or a class.
- This prefix informs the compiler that the class declaration following it is a template
- The < >(angle bracket) is used to declare variables of template type that can be used inside the class to define variables of template type
- Both template and class are keywords
- Templates cannot be declared inside a class or a function , They must be global should not be local
- A class created from a class template is called a template class.The syntax for defining an object of a template class is as follows:
Class_name < data-type > obj_name (arg list);
The process of
creating a specific class from a class template is called instantiation.
- The template keyword and <class T> send a signal specifying that the entire class will be a template.
- The template argument is used at every place in the class specification when there is a reference to the type.
- Function templates instantiate when a function call is encountered, classes are instantiated by defining an object using the template arguments.
/*Program illustrating Vector class with
and without a class template definition*/
class vector //class definition
{
private:
int *v;
int size;
public:
vector ( ) { }
vector (int m)
{
size=m;
v = new int
[size];
for (int i=0;
i<size; i ++)
v [ i ] = 0;
}
vector ( int * a)
{
for (int i=0;
i<size; i++)
v[ i ] = a [ i
];
}
int operator * (vector
& y )
{
int sum = 0;
for (int i = 0;
i<size; i++)
sum + = this à
v [i] * y.v [i];
return sum;
}
};
int main ( )
{
int x [3] = {1,2,3};
int y [3] = {4,5,6};
vector V1 (3);
vector V2 (3);
V1 = x;
V2 = y;
int R = V1 * V2;
cout <<”R =”<<R;
return 0;
}
Output:
R=32
|
template < class T >
class vector //class
definition
{
private:
T *v;
int size;
public:
vector ( ) { }
vector (int m)
{
size=m;
v = new T[size];
for (int
i=0; i<size; i ++)
v [i] = 0;
}
vector ( T * a)
{
for (int
i=0; i<size; i++)
v[i] = a
[i];
}
int operator * (vector & y)
{
T sum = 0;
for (int i = 0; i<size;
i++)
sum + = this à
v [i] * y.v [i];
return sum;
}
};
int main ( )
{
int x [3] = {1,2,3};
int y [3] = {4,5,6};
vector < int > V1 (3);
vector < int > V2
(3);
V1 = x;
V2 = y;
int R = V1 * V2;
cout <<”R =”<<R;
return 0;
}
Output:
R=32
|
For the same class vector given in program, we can also use float or double data type instead of int.
int
main ( )
{
int
x [3] = {1.1,2.2,3.3};
int
y [3] = {4.4, 5.5,6.6};
vector <float> V1 (3);
vector
<float> V2 (3);
V1 = x ,
V2 = y;
float R = V1 * V2;
cout
<<”R =”<<R<<endl;
return
0;
}
Output:
R=38.720001
The type T may represent a class name as well.
Vector <complex> V3
(5); //vector of complex numbers
Note: The
class template specification is very much similar to the ordinary class
specification except for the prefix,
template < class T >
template < class T >
- And the use of T in the place of data-type.
- This prefix informs the compiler that the class declaration following it is a template
- Uses T as a type name in the declaration
- Once the template class (also called as objects of class template) is defined, the use of those objects is the same as the non-template class objects.
merge two array by using class template in c++
ReplyDelete