This document discusses templates in C++. Templates allow functions and classes to work with multiple data types without writing separate code for each type. There are two types of templates: class templates, which define a family of classes that operate on different data types, and function templates, which define a family of functions that can accept different data types as arguments. Examples of each template type are provided to demonstrate how they can be used to create reusable and flexible code.
2. TEMPLATES
• Template is a new concept allow the function or class
to work on more than one data type at once without
writing different codes for different data types.
• The parameters used during its definition is of generic
type and can be replaced later by actual parameter.
• This is called the concept of generic programming
• The templates are also called as parameterized classes
or functions.
3. Purpose of Templates
• Used in large programs
• Code reusability
• Time saving
• Flexibility of program
• Used to create a family of classes or functions.
5. Class Templates
• A Class Template can represent various similar
classes operating on different data types.
Syntax:
template < class T1, class T2,…>
class classname
{
functions;
};
6. Eg:
template <class T>
class vector
{
T* v;
int size;
public:
vector (int m)
{
v = new T [size = m];
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;
}
};
7. Template class
• A class created from a class template is called
a Template class.
classname < type> objectname (arglist);
• The process of creating a specific class from
class template is called Instantiation.
8. Class Templates with Multiple Parameters
• More than one generic data types can be used
in a class template.
• They can be declared by comma separated list
within the template specification.
template < class T1, class T2, …..>
class classname
{
………
body of the class
………
};
9. Example:
template <class T1, class T2>
Class Test
{
T1 a;
T2 b;
public :
Test (T1 x, T2 y)
{
a = x;
b = y;
}
void show()
{
cout<< a << “ and “ << b << “n”;
}
};
10. int main()
{
cout<< “Instantiating the class template test1:”;
Test < float, int > test1 (1.23, 123);
test1.show();
cout<< “Instantiating the class template test2:”;
Test < int, char > test2 (100, ‘w’);
test2.show();
return 0;
}
11. Output for the Program
Instantiating the class template test1: 1.23 and 123
Instantiating the class template test2: 100 and w
12. Function Templates
• Function templates used to create a family of
functions with different argument types.
Syntax:
Template < class T>
returntype functionname (arguments of type T)
{
body of the function
}
14. Function Template With Multiple Parameters
Syntax:
template < class T1, class T2,….>
returntype functionname(arguments of types T1,T2,..)
{
body of the function
}
15. Example:
template < class T1, class T2>
void display ( T1 x, T2 y)
{
cout << x “ “ << y << “n”;
}
int main()
{
cout << “calling function template with integer and character
string type parameters…n”;
display( 2000, “ECG”);
cout << “calling function template with float and integer type
parameters…n”;
display( 2.12, 212);
return 0;
}
16. Output
calling function template with integer and
character string type parameters…
2000 ECG
calling function template with integer and
character string type parameters…
2.12 212