Template Classes

Template Classes
 A class that depends on an underlying data type(s)
likewise can be implemented as a template class.
 We can have a ‘NewClass’ of ints, chars, strings,
 On page 91 of the text, a template class of
BasicVector is declared. ( I’ll do another one here!!!)
It both depend on an underlying data type of the
items in the data structure.
Declaring and defining a template class requires three
1. The template class declaration
2. Methods (member functions) for the template
3. Make the implementation visible
The template class declaration
template <class ItemType>
class NewClass
NewClass ();
NewClass (ItemType initialData);
NewClass (NewClass other);
void setData (const ItemType& newData);
ItemType getData () const;
void resize (size_t newSize);
NewClass operator +(const NewClass& newClass1,
const NewClass& newClass2)
ItemType theData
#include “NewClass.cpp”
Methods for the template class
 All of the methods that manipulate newClass objects
are now dependent on the ItemType. Within the
template class declaration, the compiler already
knows about this dependency. So declaration of
methods written like they have always been.
 Ex1. void setData (const ItemType& newData);
Outside some rules must be followed to tell the
compiler about the dependency.
the template prefix template <class Item> is placed
immediately before each function definition.
Each use of the class name (such as NewClass) is changed to
the template class name
(such as NewClass <ItemType>).
Each use of complete underlying type name (such as
NewClass::ItemType) may be shortened to just the type name
(such as ItemType).
Ex2. So now instead of
NewClass operator +(const NewClass& newClass1,
const NewClass& newClass2)
. . .
it is
template <class ItemType>
NewClass <ItemType> operator +
(const NewClass<ItemType> & newClass1,
const NewClass<ItemType> & newClass2)
 NOTE: The name NewClass is changed to NewClass
<ItemType> only when it is used as a class name. (The
name NewClass is also used as the name of the NewClass’
constructor, and that usage stays as NewClass.)
int main()
NewClass <int>
NewClass <double> mySecond (4.5);
myFirst.setData (5);
cout << mySecond.getData() << endl;
Make the implementation visible
 Annoying requirement: The implementation
of the ST functions must be present in the
header file.
 Trick: Keep the implementations in a
separate implementation file, but place an
“include” directive at the bottom of the
header file to pick up those implementations.
 So we have files NewClass.h and NewClass.cpp
Parameter Matching for Methods of Template
 In template non-member functions, we are careful to
help the compiler by providing a template parameter for
each of the function’s parameters. HOWEVER, this help
is not needed for member functions of a template class.
 For example, we can use size_t parameter for the
NewClass’ resize function. Unlike ordinary template
non-member function, the compiler is able to match a
size_t parameter of a member function with any of the
usual integer arguments (such as int or const int).
Ex. myTable (42);
//42 will convert to equivalent
//size_t value by the compiler
LOOK AT Table.h and Table.cpp and main skeleton in assignment

similar documents