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
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
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
Template class function definition:
template<class T>
MyClass< T >::MyClass(int size)
{
myArray = new T[size];
}
Constructor definition - creates an array of
type T
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>
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
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.
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.
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
};
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.
 Thank You 

Class template

  • 2.
    A class templateis 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 ClassTemplate 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 anobject 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 functiondefinition: template<class T> MyClass< T >::MyClass(int size) { myArray = new T[size]; } Constructor definition - creates an array of type T
  • 6.
    Class Templates andNon-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 appearin 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 andstatic 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 classtemplate 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 classfrom 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.
  • 12.