1 / 46

OOP-4-Templates CSCI 2320 Data Abstractions

OOP-4-Templates CSCI 2320 Data Abstractions. Dr. Thomas E. Hicks Computer Science Dept Trinity University. 1. 2. 2. Copy Your Working Student-Interface Project Into C:Temp Name The Folder Template. 3. 3. C:TempTemplates. 4. Compile 1. 5. Use This Main Program. 6.

dlegge
Télécharger la présentation

OOP-4-Templates CSCI 2320 Data Abstractions

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. OOP-4-Templates CSCI 2320 Data Abstractions Dr. Thomas E. Hicks Computer Science Dept Trinity University 1

  2. 2 2

  3. Copy Your WorkingStudent-Interface Project Into C:\Temp Name The Folder Template 3 3

  4. C:\Temp\Templates 4

  5. Compile1 5

  6. Use This Main Program 6

  7. Copy/Paste Main # include "Utilities.hpp" # include "Student.hpp" void MySwap (int Value1, int Value2); int main(int argc, char * argv[]) { int A = 5, B = 10; cout << "A = " << A << " B = " << B << endl; //MySwap (A, B); cout << "A = " << A << " B = " << B << endl; getchar(); return(0); } 7

  8. Write Code For MySwap Do Not Look Ahead In The Slides! void MySwap (int Value1, int Value2){ int Temp; } 8

  9. How Many Of You Have Something Like The Following: void MySwap (int Value1, int Value2){ int Temp; Temp = Value1; Value1 = Value2; Value2 = Temp; } Only To Find That It Does Not Work? Pass ByValue? - BAD! 9

  10. Better - Pass By Reference void MySwap (int & Value1, int & Value2){ int Temp; Temp = Value1; Value1 = Value2; Value2 = Temp; } 10

  11. Compile2 11 11

  12. Change The Main Program 12

  13. Copy/Paste Main int main(int argc, char * argv[]) { int A = 5, B = 10; cout << "A = " << A << " B = " << B << endl; MySwap (A, B); cout << "A = " << A << " B = " << B << endl; float AA = 5.5, BB = 7.7; cout << "AA = " << AA << " BB = " << BB << endl; MySwap (AA, BB); cout << "AA = " << AA << " BB = " << BB << endl; getchar(); return(0); } 13

  14. No Signature For (float, float) 14

  15. Overload MySwap! 15

  16. int Element bool Lumber float Squadron short int Unit double Officer long int Desk Student Flower Part Shrub Employee Tree Infinite #Of Objects Auto China CD Equipment Stamp Soldier Coin 16

  17. Templates 17 17

  18. Templates 18

  19. Template Functions Begin With Something Like: template <class T> Or Maybe Something Like: Or Maybe Something Like: template <class InfoType> template <class I> 19

  20. The class Variable Must Appear In The Parameter List template <class T> void MySwap (T & Value1, T & Value2); template <class T> void MySwap (T & Value1, T & Value2) { } 20

  21. Compile3 21 21

  22. Copy/Paste Main int main(int argc, char * argv[]) { int A = 5, B = 10; cout << "A = " << A << " B = " << B << endl; MySwap (A, B); cout << "A = " << A << " B = " << B << endl; float AA = 5.5, BB = 7.7; cout << "AA = " << AA << " BB = " << BB << endl; MySwap (AA, BB); cout << "AA = " << AA << " BB = " << BB << endl; getchar(); return(0); } 22

  23. Template MySwap Works For in & float 23

  24. Compile4 24 24

  25. Copy/Paste - Add This To Main char C = 'X', D = '?'; cout << "C = " << C << " D = " << D << endl; MySwap (C, D); cout << "C = " << C << " D = " << D << endl; getchar(); return(0); } 25

  26. Compile5 26 26

  27. Copy/Paste - Add This To Main bool E = true, F = false; cout << "E = " << E << " F = " << F << endl; MySwap (E, F); cout << "E = " << E << " F = " << F << endl; getchar(); return(0); } 27

  28. Compile6 28 28

  29. Copy/Paste - Add This To Main Student Student1 ("Pete", 2222, MALE), Student2 ("Sandra", 3333, FEMALE); Student1.Display("Student1"); Student2.Display("Student2"); MySwap (Student1, Student2); Student1.Display("Student1"); Student2.Display("Student2"); getchar(); return(0); } 29

  30. Will Work For All Classes(As Long As Operator = Has Been Overloaded Properly) 30

  31. Generic Solutions 31 31

  32. C++ Also Supports Generics, But Templates Enable Us To Easily Create Functions That Can Be Used With Thousands Of Data Types. template <class T>void BubbleSort(T Array[], long ActNo); template <class InfoType>long Search(InfoType Data[], long ActNo, InfoType SoughtInfo); The class Variable Must Appear In The Parameter List 32

  33. About Templates 33 33

  34. Template Functions Begin With Something Like: Templates Are Not Really Functions. They are more of a design or pattern for what shall become a function if evoked. The CompilerShall Generate the Necessary Variations of this Function During Run Time. Template Functions Must be placed in .hpp files! 34

  35. Template - Multiple Parameters template <class InfoType, class HeaderNodeType> class Binary Tree { ... }; An important goal of both software engineering and object-oriented programming is reuse! Function Templates facilitate code reuse! 35

  36. TemplatedClass ListType 36 36

  37. Write The Class Definition For A Templated Class, Called ListType Whose Template Argument Is InfoType. template <class InfoType> class ListType { public: private: };

  38. Write The Declaration To Create An Integer Type List , Called IntNos, of ListType. template <class InfoType> class ListType { public: private: };

  39. Add An Array To The Private Data of ListType. template <class InfoType> class ListType { public: private: InfoType Info[10]; // Container To Store Data long ActNo; // Actual No Items In Container Bad Design Problem: Not All Of The ListsNeed The Same Capacity ListType <int> IntNos; ListType <char> Chars; ListType <Student> Class;

  40. Add An Array To The Private Data of ListType. template <class InfoType> class ListType { public: private: InfoType * Info; // Container To Store Data long ActNo; // Actual No Items In Container Better Design Problem: Not All Of The ListsNeed The Same Capacity ListType <int> IntNos; ListType <char> Chars; ListType <Student> Class;

  41. Write The Constructor ListType (long int NewMax = 10) template <class InfoType> class ListType { public: private: InfoType Info[10]; // Container To Store Data long Max, // Capacity Of The Container ActNo; // Actual No Items In Container };

  42. Write The Code For The Constructor ListType (long int NewMax = 10) template <class InfoType> class ListType { public: private: InfoType Info[10]; // Container To Store Data long Max, // Capacity Of The Container ActNo; // Actual No Items In Container }; ListType (long int NewMax = 10);

  43. ListType (long int NewMax = 10) template <class InfoType> ListType <InfoType> :: ListType (long int NewMax) { puts("Evoking Constructor ListType(NeMax)"); printf("NewMax = %ld\n\n", NewMax); }; Update main

  44. ListType (long int NewMax = 10) Allocate Max + 1 Memory - Set Max & ActNo template <class InfoType> ListType <InfoType> :: ListType (long int NewMax) { ActNo = 0; Info = new InfoType [NewMax + 1]; if (Info == NULL) { puts("Not Enough Memory!"); Max = 0; } else Max = NewMax; };

  45. "Memory Leak" You Have Created A Memory leak If You Have Executed The Program One TIme!? template <class InfoType> ListType <InfoType> :: ListType (long int NewMax) { ActNo = 0; Info = new InfoType [NewMax + 1]; if (Info == NULL) { puts("Not Enough Memory!"); Max = 0; } else Max = NewMax; }; WHY? Dynamic Memory Allocated!

  46. Write The Destructor ~ListType (void) template <class InfoType> class ListType { public: ListType (long int NewMax = 10); ~ListType(void); private: InfoType Info[10]; long Max, ActNo; };

More Related