Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.

Similar presentations

Presentation on theme: "Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a."— Presentation transcript:

1
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a variable be declared to be of the type of an abstract class? Can a pointer be declared to be of the type of an abstract class? 1

2
2 Templates

3
3 Type-independent patterns that can work with multiple data types. –Generic programming –Code reusable Function Templates –These define logic behind the algorithms that work for multiple data types. Class Templates –These define generic class patterns into which specific data types can be plugged in to produce new classes.

7
7 Function Templates Generic function to find a maximum value (see maximum example). Template T maximum(T a, T b, T c) { T max = a; if (b > max) max = b; if (c > max) max = c; return max; } Template function itself is incomplete because the compiler will need to know the actual type to generate code. So template program are often placed in.h or.hpp files to be included in program that uses the function. C++ compiler will then generate the real function based on the use of the function template.

13
Use of template function Can any user defined type be used with a template function? –Not always, only the ones that support all operations used in the function. –E.g. if myclass does not have overloaded << operator, the printarray template function will not work. 13

14
Class template So far the classes that we define use fix data types. Sometime is useful to allow storage in a class for different data types. See simplelist1 (a list of int type elements) example –What if we want to make a simple list of double type? Copy paste the whole file and replace int with double Make use of typedef in C++, See simplelist2. –Still need to change one line of code for a new type. 14

15
Class template –Function templates allow writing generic functions that work on many types. –Same idea applies to defining generic classes that work with many types -- extract the type to be a template to make a generic classes. –See simplelist3 15

16
Class template To make a class into a template, prefix the class definition with the syntax: template –Here T is just a type parameter. Like a function parameter, it is a place holder. –When the class is instantiated, T is replaced by a real type. To access a member function, use the following syntax: –className :: memberName. –SimpleList :: SimpleList() Using the class template: –ClassName variable; –SimpleList list1; 16

17
17 Another Class Template Example MemoryCell template can be used for any type Object. Assumptions –Object has a zero parameter constructor –Object has a copy constructor –Copy-assignment operator Convention –Class templates declaration and implementation usually combined in a single file. –It is not easy to separate them in independent files due to complex c++ syntax. –This is different from the convention of separating class interface and implementation in different files.

18
18 Class Template Usage Example MemoryCell can be used to store both primitive and class types. Remember –MemoryCell is not a class. –It’s a class template. –MemoryCell, MemoryCell etc are classes.