1 / 21

CE00314-2 Further Programming Concepts in C++

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

lindley
Download Presentation

CE00314-2 Further Programming Concepts in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CE00314-2Further Programming Concepts in C++ Lecture 6 Part 1 - Templates Part 2 - Further Inheritance

  2. Introduction • Part 1 • Template definition • Function templates • Class templates • Part 2 • Further inheritance

  3. Template Definition • Cambridge Dictionary • template1 a pattern made of metal, plastic or paper, which is used for making many copies of a shape or to help cut material accurately2 a system that helps you arrange information on a computer screen • C++ • Pattern for handling generic data

  4. Function overloading • Swap revisited • To handle integersvoid swap(int& rnA, int& rnB){ int nTemp; nTemp = rnA; rnA = rnB; rnB = nTemp}

  5. Function overloading • Swap revisited • To handle charactersvoid swap(char& rnA, char& rnB){ char nTemp; nTemp = rnA; rnA = rnB; rnB = nTemp}

  6. Function overloading • Swap revisited • float, double ……. • Separate functions for each ?? • Generic function • Generic functionality • Generic data – automatically typed when used • Template function

  7. Template function • Conceptuallyvoid swap(Type_of_Var& Var1, Type_of_Var& Var2){ Type_of_Var Temp; Temp = Var1; Var1 = Var2; Var2 = Temp;}

  8. Template Function - syntax template<class T> // template prefix void swapValues(T& Var1, T& Var2) { T Temp; Temp = Var1; Var1 = Var2; Var2 = Temp; } • For demonstration see – Template1.cpp

  9. Template functions - array print • Handling unspecified arrays • Array length?template<class T>void printArray(const T *array, constint nSize){for(int nCount = 0; nCount < nSize; nCount++) cout << array[nCount] << “ “; cout << endl;} • For usage example see – Template2.cpp

  10. Template Functions - returning Also possible to have generic returntemplate<class T> T addValues(T Val1, T Val2) { return Val1 + Val2; } • See Template3.cpp for usage example

  11. 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

  12. 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”

  13. 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

  14. 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?

  15. 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?

  16. 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”

  17. 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

  18. This is meaninglessin a real sense Abstract Base Class Shape Circle Square Triangle

  19. Abstract Base Class • May not be able to instantiate • But …… • ….. still vital to hierarchy • See Abstract1.cpp & Abstract1.h

  20. 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( )

  21. Tutorial Work • As for templates • Plus • Experiment with ABCs – using Abstract1.ccp & .h • Research for assignment • Next Week • Bob Hobbs

More Related