CE00314-2Further Programming Concepts in C++
Lecture 6
Part 1 - Templates
Part 2 - Further Inheritance
Introduction
Part 1 Template definition Function templates Class templates
Part 2 Further inheritance
Template Definition
Cambridge Dictionary template 1 a pattern made of metal, plastic or paper, which is used for making many copies of a shape or to help cut material accurately
2 a system that helps you arrange information on a computer screen
C++ Pattern for handling generic data
Function overloading
Swap revisited To handle integers
void swap(int& rnA, int& rnB){ int nTemp;
nTemp = rnA; rnA = rnB;
rnB = nTemp}
Function overloading
Swap revisited To handle characters
void swap(char& rnA, char& rnB){ char nTemp;
nTemp = rnA; rnA = rnB;
rnB = nTemp}
Function overloading
Swap revisited float, double ……. Separate functions for each ??
Generic function Generic functionality Generic data – automatically typed when used Template function
Template function
Conceptually
void swap(Type_of_Var& Var1, Type_of_Var& Var2){ Type_of_Var Temp; Temp = Var1; Var1 = Var2; Var2 = Temp;}
Template Function - syntax
template<class T> // template prefixvoid swapValues(T& Var1, T& Var2){
T Temp;Temp = Var1;Var1 = Var2;Var2 = Temp;
}
For demonstration see – Template1.cpp
Template functions - array print Handling unspecified arrays Array length?
template<class T>void printArray(const T *array, const int nSize){
for(int nCount = 0; nCount < nSize; nCount++)
cout << array[nCount] << “ “;
cout << endl;}
For usage example see – Template2.cpp
Template Functions - returningAlso possible to have generic return
template<class T>
T addValues(T Val1, T Val2)
{
return Val1 + Val2;
}
See Template3.cpp for usage example
Template Functions – multiple types Possible to have more than one type
template<class T1, class T2> Must use all parameters All must be utilised within function See Template4.cpp
Possible to use Tx for casting and return type See Template5.cpp
Template functions - Plenary
Produce generic solution Data types provided at run time Operators must be able to handle types Overloaded operators included Use of keyword “class” can be replaced with
“typename” ANSI/ISO standard allows Programmers tend to use “class”
Class Templates
Syntax basically the same as when using function templates must use keyword “class”
template<class T>class Pair{private:
T first;T second;
private:Pair();Pair(T Val1, T Val2);
etc….. See Template6.cpp for further details
Class templates
So far….. Class within main file Could place within .h file
and include See Template7.cpp
Shows use of Array of pointers ? – mixed types?
Plenary – Part 1
Concept of templates Template functions
Use of “class” or “typename” keywords Template classes
Must use “class” keyword
Tutorial Work Experiment with given code Is it possible to mix types when using arrays etc?
Part 2 – Further Inheritance
Base Class e.g. Shape Not viable as object Also consider “Dog” May prevent object instantiation Class becomes “Abstract Base Class”
Abstract Base Class
Dummy definitions Overridden within derived class
Virtual methods Without any definition “Pure Virtual Functions” Only within ABC
Class containing ABCs Can not be used to instantiate objects
Abstract Base Class
Shape
Circle Square Triangle
This is meaninglessin a real sense
Abstract Base Class
May not be able to instantiate But …… ….. still vital to hierarchy
See Abstract1.cpp & Abstract1.h
Within “Shape” class
Could have empty methods virtual void Draw( ) { }
Or make it pure virtual virtual void Draw ( ) = 0;
Within Shape – no functionality for Draw( )
Tutorial Work
As for templates Plus
Experiment with ABCs – using Abstract1.ccp & .h Research for assignment
Next Week Bob Hobbs