C++ Template

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
{
int *v;
int size;
public:
vector(int m)
{
v=new int[size=m];
for(int i=0;i < size;i++)
v[i]=0;
}
vector(int *a) // create a vector from an array
{
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;
}
};
The vector class can store an array of int numbers and perform the scalar product of two int vectors as shown below
void main()
{
int x[3]={1,2,3};
int y[3]={4,5,6};
vector v1(3);
vector v2(3);
v=x;
v2=y;
int 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 >
class classname
{
//.....
// class member specification
// with anonymous type T
// whenever appropriate
//.....
};
The template definition of vector class shown below the syntax of a template
template
class vector
{
T* v;
int size;
public:
vector( int m)
{
v=new T[size=m];
for(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->[i]*y-v[i];
return 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

Example vector v1(10); // 10 elements int vector
vector v2(20); // 20 elements float vector

Note: The type T may represent a class name as well Example
Vector v3(6); // vector of 6 complex numbers

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
#include <iostream>
using namespace std;
const int size=3;
template < class T >
class vector
{
T* v; // type T vector
public:
vector()
{
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];
}
T 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;
vector < int > v2;
v1=x;
v2=y;
int r=v1*v2;
std::cout << "R=" << r << "\n";
}

output:
R=32

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise