Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP-4-Templates, ListType

Similar presentations


Presentation on theme: "OOP-4-Templates, ListType"— Presentation transcript:

1 OOP-4-Templates, ListType
CSCI 2320 Data Abstractions Dr. Thomas E. Hicks Computer Science Dept Trinity University 1

2 2 2

3 Copy Your Working Student-Interface Project Into C:\Temp
Name The Folder Template 3 3

4 C:\Temp\Templates Download This PDF From The Web Site

5 Compile 1

6 Copy-Paste Code From The Next Slide?  Compile Program
Use This Main Program Copy-Paste Code From The Next Slide?  Compile Program

7 Copy/Paste Main # include "Utilities.hpp" Compile Program
# 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); HitCarriageRetunToContinue(); return(0); } Compile Program

8 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

9 Make Those Changes Necessary To Pass By Value Pass By Value?
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

10 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; }

11 Compile 2

12 Create Floating Point Variables AA & BB Try Passing Them To MySwap - Copy Paste Code On Next Slide

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

14 Look At Error  No Signature For (float, float)

15 We Can Certainly Overload MySwap!

16 Infinite # Of Objects 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

17 Templates

18 Templates

19 Template Functions Begin With Something Like:
template <class T> This Class Variable Is Called T template <class InfoType> Or Maybe Something Like: This Class Variable Is Called InfoType template <class I> Or Maybe Something Like: This Class Variable Is Called I

20 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; }

21 Compile 3

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

23 The Templated Function MySwap Works Now Works For Both int & float

24 Compile 4

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

26 Compile 5

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

28 Compile 6

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); HitCarriageReturnToContinue(); return(0); }

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

31 Generic Solutions

32 The class Variable Must Appear, At Least Once, In The Parameter List
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, At Least Once, In The Parameter List

33 About Templates 33 33

34 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

35 Function Templates facilitate code reuse!
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

36 Templated Class ListType
36 36

37 Our ListType is going to use an Array to hold the objects.
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 37

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

39 Examples: ListType <int> IntNos;
Write The Declaration To Create An Integer Type List, Called IntNos, using our new ListType template <class InfoType> class ListType { public: private: }; Examples: ListType <int> IntNos; ListType <char> Chars; ListType <Student> Class;

40 Bad Design Problem: Not All Of The Lists Need The Same Capacity
Add An Array (Info) To The Private Data of ListType. template <class InfoType> class ListType { public: private: }; Bad Design Problem: Not All Of The Lists Need The Same Capacity InfoType Info[10]; // Container To Store Data ListType <int> IntNos; ListType <char> Chars; ListType <Student> Class; #

41 Add An ActNo (counter) To The Private Data
template <class InfoType> class ListType { public: private: InfoType * Info; // Container To Store Data }; Better Design Problem: Not All Of The Lists Need The Same Capacity long ActNo; // Actual No Items In Container

42 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

43 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 bool Sorted; // Is Data In Info Sorted?

44 ListType (long NewMax = 10);
Write The Prototype For The Constructor ListType (long int NewMax = 10) template <class InfoType> class ListType { public: private: InfoType *Info; // Container To Store Data long Max, // Capacity Of The Container ActNo; // Actual No Items In Container bool Sorted; // Data Organization }; ListType (long NewMax = 10); Remember: the code for the class methods/functions must be placed in the .hpp file

45 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); }

46 Add The Following To Main
ListType <int> Nos(5); Compile Program

47 Keep It From Breaking Later
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 Keep It From Breaking Later

48 Don't Need To Set ActNo & Sorted TWICE?
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; // FULL  if ActNo <= Max  No Insertions Sorted = true; // EMPTY if ActNo = 0  No Withdrawals } // Sort status really does not matter else { Max = NewMax; ActNo = 0; Sorted = true } Don't Need To Set ActNo & Sorted TWICE?

49 Code The Constructor #4 Good Solution? Not Yet!
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; } Good Solution? Not Yet! This Semester, You Will Concentrate More On Developing "Robust Software" What If NewMax Is Negative?

50 My Constructor #5

51 My Destructor

52 Templated ListType Class Data Insertion/Append Must Start At Info[1]

53 Templated ListType Class
You May Get Sort Code From Internet You May Have To Modify It To Start At Info[1]


Download ppt "OOP-4-Templates, ListType"

Similar presentations


Ads by Google