Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 Template Classes

2 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, whatever. 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.

3 Syntax Declaring and defining a template class requires three things. 1. The template class declaration 2. Methods (member functions) for the template class 3. Make the implementation visible

4 The template class declaration template class NewClass { public: 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) private: ItemType theData } ; #include “NewClass.cpp”

5 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);

6 Outside some rules must be followed to tell the compiler about the dependency.  the template prefix template 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 ).  Each use of complete underlying type name (such as NewClass::ItemType) may be shortened to just the type name (such as ItemType).

7 Ex2. So now instead of NewClass operator +(const NewClass& newClass1, const NewClass& newClass2) {... } it is template NewClass operator + (const NewClass & newClass1, const NewClass & newClass2) { … }

8 NOTE: The name NewClass is changed to NewClass 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 myFirst; NewClass mySecond (4.5); myFirst.setData (5); cout << mySecond.getData() << endl;

9 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

10 Parameter Matching for Methods of Template Classes 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


Download ppt "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,"

Similar presentations


Ads by Google