11
2 2 2
3 3 3
C:\Temp\Templates Download This PDF From The Web Site 4
5
Use This Main Program Copy-Paste Code From The Next Slide? Compile Program 6
Copy/Paste Main # include "Utilities.hpp" # include "Student.hpp" void MySwap (int Value1, int Value2); Compile Program 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; } HitCarriageRetunToContinue(); return(0); 7
Insert The Following Code For MySwap Do Not Look Ahead In The Slides! void MySwap (int Value1, int Value2) { int Temp; 30 Seconds: Finish Writing The Code That Would Match This Prototype } 8
Some Of You Generally Code Like The Following: void MySwap (int Value1, int Value2) { int } Temp; Temp = Value1; Value1 = Value2; Value2 = Temp; Make Those Changes Necessary To Pass By Value Compile Pass By Value? Does Not Work 9
Solution Pass By Reference Reference Variables Or Pointers Prototype: void MySwap (int & Value1, int & Value2); void MySwap (int & Value1, int & Value2) { int Temp; } Temp = Value1; Value1 = Value2; Value2 = Temp; 10
11
Create Floating Point Variables AA & BB Try Passing Them To MySwap - Copy Paste Code On Next Slide 12
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; HitCarriageReturnToContinue(); return(0); 13
Look At Error No Signature For (float, float) 14
We Can Certainly Overload MySwap! 15
int bool float short int double long int Student Part Employee Auto CD Stamp Coin Infinite # Of Objects Element Lumber Squadron Unit Officer Desk Flower Shrub Tree China Equipment Soldier 16
17
Templates 18
Template Functions Begin With Something Like: template <class T> Or Maybe Something Like: template <class InfoType> Or Maybe Something Like: template <class I> This Class Variable Is Called T This Class Variable Is Called InfoType This Class Variable Is Called I 19
The Class Variable (T) Must Appear In The Parameter List template <class T> void MySwap (T & Value1, T & Value2); template <class T> void MySwap (T & Value1, T & Value2) { T Temp; } Temp = Value1; Value1 = Value2; Value2 = Temp; 20
21
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; HitCarriageReturnToContinue(); return(0); 22
The Templated Function MySwap Works Now Works For Both int & float 23
24
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; HitCarriageReturnToContinue(); return(0); 25 25
26
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; HitCarriageReturnToContinue(); return(0); 27 27
28
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"); } HitCarriageReturnToContinue(); return(0); 29
Will Work For All Classes (As Long As Operator = Has Been Overloaded Properly) 30
31
C++ Also Supports Generics, But Templates Enable Us To Easily Create Functions That Can Be Used With Thousands Of Data Types. The class Variable Must Appear, At Least Once, In The Parameter List 32 template <class T> void BubbleSort(T Array[], long ActNo); template <class InfoType> long Search(InfoType Data[], long ActNo, InfoType SoughtInfo);
33 33 33
Pattern For A "Yet To Be" Function Templates Are Not Really Functions. They are more of a design or pattern for what shall become a function if evoked. The Compiler Shall Generate the Necessary Variations of this Function During Run Time. Template Functions Must be placed in.hpp files! 34 34
Template Can Have 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 35
36 36 36
Our ListType is a container class that shall be used to house and manage a collection of objects. Our ListType is going to use an Array to hold the objects. The container may be Empty, Full, or somewhere in between. 37 3737
Write The Basic 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, using our new ListType template <class InfoType> class ListType { public: Examples: private: }; ListType <int> IntNos; ListType <char> Chars; ListType <Student> Class; 39
Add An Array (Info) To The Private Data of ListType. template <class InfoType> class ListType { public: private: }; InfoType Info[10]; ListType <int> IntNos; // Container To Store Data ListType <char> Chars; ListType <Student> Class; 40
Add An ActNo (counter) To The Private Data template <class InfoType> class ListType { public: private: InfoType * Info; // Container To Store Data long ActNo; // Actual No Items In Container }; 41
Add A Max (counter) To The Private Data template <class InfoType> class ListType { public: private: InfoType * Info; // Container To Store Data long ActNo; // Actual No Items In Container long Max; // Capacity Of The Container }; 42
template <class InfoType> class ListType { public: private: Add A Max (counter) To The Private Data }; InfoType * Info; // Container To Store Data long ActNo; // Actual No Items In Container long Max; bool Sorted; // Capacity Of The Container // Is Data In Info Sorted? 43
Write The Prototype For The Constructor ListType (long int NewMax = 10) template <class InfoType> class ListType { public: ListType (long NewMax = 10); private: }; InfoType *Info; long Max, ActNo; bool Sorted; // Container To Store Data // Capacity Of The Container // Actual No Items In Container // Data Organization Remember: the code for the class methods/functions must be placed in the.hpp file 44
Code The Constructor #1 Dynamic ListType (long NewMax = 10) template <class InfoType> ListType <InfoType> :: ListType (long NewMax) { puts("evoking Constructor ListType(NewMax)"); printf("newmax = %ld\n\n", NewMax); } 45
Add The Following To Main ListType <int> Nos(5); Compile Program 46
Code The Constructor #2 Allocate The Dynamic Memory - CHECK template <class InfoType> ListType <InfoType> :: ListType (long NewMax) { } Info = new InfoType [NewMax + 1]; if (Info == NULL) { Max = 0; // If no memory, we don't want to crash ActNo = 0; // in the future full if ActNo <= Max Sorted = true; // won't attempt to put more in empty } // won't attempt to take some out full else 47
Code The Constructor #3 template <class InfoType> ListType <InfoType> :: ListType (long NewMax) { Info = new InfoType [NewMax + 1]; if (Info == NULL) { Max = 0; // If no memory, we don't want to crash ActNo = 0; Sorted = true; // EMPTY if ActNo = 0 No Withdrawals } // Sort status really does not matter else { Max = NewMax; ActNo = 0; Sorted = true } } // FULL if ActNo <= Max No Insertions 48
Code The Constructor #4 template <class InfoType> ListType <InfoType> :: ListType (long NewMax) { Info = new InfoType [NewMax + 1]; ActNo = 0; Sorted = true; if (Info == NULL) Max = 0; else Max = NewMax; } 49
My Constructor #5 50
My Destructor 51
Templated ListType Class 52
Templated ListType Class 53