Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template.

Similar presentations


Presentation on theme: "Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template."— Presentation transcript:

1

2 Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template  Template and operator overload  More on operator overload -- By Rossella Lau

3 Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 A reason for template  Consider the sample class IntArray  If the objects stored on the array are not integers, but strings, rational numbers, or student records, we may need to create many classes. class IntArray{ int *array; size_t size; size_t capacity; …… }; class StringArray{ string *array; size_t size; size_t capacity; …… }; class StudentArray{ Student *array; size_t size; size_t capacity; …… };   Absolutely terrible for maintenance

4 Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 Array intArray; Templates in C++  In C++, type parameter supports a template class / function to be written as it can be of any data type  Rewrite class IntArray to a template class Array  Whenever instantiating an Array object, a type must be specified class IntArray{ int *array; size_t size; size_t capacity; …… }; template <class T> T Array strArray; Array stdArray; Array

5 Rossella Lau Lecture 11, DCO10105, Semester B,2005-6  Sample program sets:  IntArray.h, IntArray.cpp, testArray.cpp in lab6  Array.h and test testArray.cpp  Implementation of Array.h cannot be pre-compiled since the type cannot be determined before the class is used  implementation codes will be placed inside.h file to allow for complete user program compilation  When declaring a template class, a type should be specified whether in a user program, e.g., Array in testArray.cpp, or is implemented outside the class definition Array :: Example: IntArray  Array

6 Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 Template and operator overload  For type class used in a template, the specified type must have operator overload functions used in the template class in order to use the respective functions  Inside copy constructor, push_back(), resize() : assignment operator must be properly overloaded  Inside fillData() :  Inside printData() :  Inside searchData() :  Inside sum(), average(), min(), max() :  As an example in testArray.cpp, the instance “ names ” does not call aggregate functions since there are not respective operator overload functions defined in

7 Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 More notes on friend function  Usually, we can avoid friend function declared in a class  However, if, for example, a class overloads << (output), then it should be used inside the class: void aFunction(…) { … cout << thisClassObject; … }  The operator overload function must be declared before that statement can be used in order to get through the compiler  However, if this statement must be declared inside the class definition, friend function of << should be declared since operator<<(ostream&, TheClass const &) cannot be simply moved up before the class declaration since the second parameter is defined after it!  In this case, we must declare operator<<() as a friend function inside the class  Again, this can be avoided if the implementation codes of the class can be placed after the definition of operator<<()

8 Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 More notes on virtual operator overload  To allow for operator overload override, it can be declared with “virtual”  However, the non-member operator overload function cannot be achieved  Solution: Define a member function which calls the non-member operator function with “virtual”

9 Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 Advantages of template over inheritance  A template class can serve for different types – no new class is created – maintenance is simpler  Inheritance must create new classes  Former Java must use inheritance, creating many classes, to achieve similar results of Array  Together with operator overload, many functions can become generic – a user type further define its detailed operation in order to apply for the same process

10 Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 Summary  Template makes rewriting codes and maintenance to be minimized  Together with operator overload, it makes the potential to define generic algorithms in the STL

11 Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 Reference  Malik: 15 -- END --


Download ppt "Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template."

Similar presentations


Ads by Google