ce00314-2 further programming concepts in c++
DESCRIPTION
CE00314-2 Further 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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/1.jpg)
CE00314-2Further Programming Concepts in C++
Lecture 6
Part 1 - Templates
Part 2 - Further Inheritance
![Page 2: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/2.jpg)
Introduction
Part 1 Template definition Function templates Class templates
Part 2 Further inheritance
![Page 3: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/3.jpg)
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
![Page 4: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/4.jpg)
Function overloading
Swap revisited To handle integers
void swap(int& rnA, int& rnB){ int nTemp;
nTemp = rnA; rnA = rnB;
rnB = nTemp}
![Page 5: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/5.jpg)
Function overloading
Swap revisited To handle characters
void swap(char& rnA, char& rnB){ char nTemp;
nTemp = rnA; rnA = rnB;
rnB = nTemp}
![Page 6: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/6.jpg)
Function overloading
Swap revisited float, double ……. Separate functions for each ??
Generic function Generic functionality Generic data – automatically typed when used Template function
![Page 7: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/7.jpg)
Template function
Conceptually
void swap(Type_of_Var& Var1, Type_of_Var& Var2){ Type_of_Var Temp; Temp = Var1; Var1 = Var2; Var2 = Temp;}
![Page 8: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/8.jpg)
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
![Page 9: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/9.jpg)
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
![Page 10: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/10.jpg)
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
![Page 11: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/11.jpg)
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
![Page 12: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/12.jpg)
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”
![Page 13: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/13.jpg)
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
![Page 14: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/14.jpg)
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?
![Page 15: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/15.jpg)
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?
![Page 16: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/16.jpg)
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”
![Page 17: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/17.jpg)
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
![Page 18: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/18.jpg)
Abstract Base Class
Shape
Circle Square Triangle
This is meaninglessin a real sense
![Page 19: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/19.jpg)
Abstract Base Class
May not be able to instantiate But …… ….. still vital to hierarchy
See Abstract1.cpp & Abstract1.h
![Page 20: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/20.jpg)
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( )
![Page 21: CE00314-2 Further Programming Concepts in C++](https://reader033.vdocument.in/reader033/viewer/2022051218/5681585a550346895dc5b5e2/html5/thumbnails/21.jpg)
Tutorial Work
As for templates Plus
Experiment with ABCs – using Abstract1.ccp & .h Research for assignment
Next Week Bob Hobbs