Class templates allow the creation of common classes that can operate on different data types. A class template is defined with generic data types that are replaced when an object is created. The syntax uses angle brackets <> to declare the template type(s) T. Objects are created by specifying the type, like Stack<double>. Template functions are declared normally but preceded with the template type(s). Class templates can also take non-type parameters with default arguments. Derived classes can be created from template base classes while restricting the template feature. Specialization allows overriding the generated code for specific types.
2. A class template is a common class that
represent various similar classes operating
on data of different types.
Once a class template is defined, we can
create an object of that class using a
specific basic or user defined data types to
replace the generic data types used during
class definition
3. Syntax of Class Template
Template<class T1,class T2,…>
Class classname
{
attributes;
methods;
};
T is the variable of template type.
<>(angle bracket) is used to declare
variables of template type, one or more
variables are declared separated by comma
4. To create an object of the class, type
ClassName< type > myObject;
Example: Stack< double >
doubleStack;
Template class functions
Declared normally, but preceded by
template<class T>
Generic data in class listed as type T
Binary scope resolution operator used
5. Template class function definition:
template<class T>
MyClass< T >::MyClass(int size)
{
myArray = new T[size];
}
Constructor definition - creates an array of
type T
6. Class Templates and Non-type Parameters
Can use non-type parameters in templates
Default argument
Treated as const
Example:
Template <class T, int elements>
Stack<double, 100>
mostRecentSalesFigures;
Declares object of type
Stack<double,100>
7. This may appear in the class definition:
T stackHolder[ elements ];
Creates array at compile time, rather than
dynamic allocation at execution time
Classes can be overridden
For template class Array, define a class
named
Array<myCreatedType>
This new class overrides then class template
for myCreatedType
The template remains for unoverriden types
8. Class templates and static variables :
The rule for class templates is same as
function templates.
Each instantiation of class template has its
own copy of member static variables.
A class generated from a class template is
called a generated class.
9. Derive a class template from a base class,
which is a template class.
Derive a class template from a base class,
which is a template class, add more
template members in the derived class.
Derive a class from a base class which is
not a template, and template member to
that class.
10. Derive a class from a base class which is a
template class and restrict the template
feature, so that the derived class and its
derivatives do not have the template feature.
The syntax for declaring derived classes from
templatebased
base classes is as :
template <class T1, …..>
class baseclass
{
// template type data and functions
};
11. template <class T1, …..>
class derivedclass : public baseclass <T1,
….>
{
// template type data and functions
};
Class Template Specialization :
In some cases it is possible to override the
template-generated code by providing special
definitions for specific types. This called
template specialization.