1 / 39

Today’s Objectives

12-Jul-2006. Today’s Objectives. Announcements Homework #4 is due on Monday, 17-Jul Homework #5 is posted. It’s due on 24-Jul, and no late projects will be accepted Review Quiz #3 UML sequence diagrams Review of overloading Templates (Ch. 14) Review of function templates

Télécharger la présentation

Today’s Objectives

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. 12-Jul-2006 Today’s Objectives • Announcements • Homework #4 is due on Monday, 17-Jul • Homework #5 is posted. It’s due on 24-Jul, and no late projects will be accepted • Review Quiz #3 • UML sequence diagrams • Review of overloading • Templates (Ch. 14) • Review of function templates • Class templates • Non-type parameters • Bonus Lab #7

  2. Review Quiz #3

  3. UML Sequence Diagrams

  4. Sequence Diagrams (Deitel, 63, 382; Stevens, 117) UML Sequence Diagram • Shows the interaction between objects in your program • One of two “UML interaction diagrams” • The other UML interaction diagram is called a “collaboration diagram” • The sequence diagram does a better job of showing the timing of the interactions

  5. book : Book myLibrary : Library myList : LibraryItemList *pItem : LibraryItem Library user Sequence Diagrams (Deitel, 63, 382; Stevens, 117) UML Sequence DiagramShowing the interaction in the DemoLibrary program book("Java How to Program",4) book addLibraryItem(item) push_back(item) new LibraryItem(item) pItem

  6. book : Book myLibrary : Library myList : LibraryItemList *pItem : LibraryItem Library user Sequence Diagrams (Deitel, 63, 382; Stevens, 117) UML Sequence DiagramShowing the interaction in the DemoLibrary program book("Java How to Program",4) Objects in the program book addLibraryItem(item) push_back(item) “Actor” that uses the program new LibraryItem(item) pItem

  7. book : Book myLibrary : Library myList : LibraryItemList *pItem : LibraryItem Library user Sequence Diagrams (Deitel, 63, 382; Stevens, 117) UML Sequence DiagramShowing the interaction in the DemoLibrary program book("Java How to Program",4) book addLibraryItem(item) push_back(item) new LibraryItem(item) Lifeline of the object pItem Time passes as we move from top to bottom.

  8. book : Book myLibrary : Library myList : LibraryItemList *pItem : LibraryItem Library user Sequence Diagrams (Deitel, 63, 382; Stevens, 117) UML Sequence DiagramShowing the interaction in the DemoLibrary program book("Java How to Program",4) book addLibraryItem(item) Call to a member function push_back(item) new LibraryItem(item) pItem Return value from the function

  9. Problem with the DemoLibrary

  10. book : Book myLibrary : Library myList : LibraryItemList *pItem : LibraryItem Library user DemoLibrary Problem UML Sequence DiagramShowing the interaction in the DemoLibrary program book("Java How to Program",4) book addLibraryItem(item) push_back(item) new LibraryItem(item) The pointer that is loaded into the “myData” array of myList points to a base-class object, but we want it to point to a derived-class object. Then we can use polymorphism in the “printList” member function. pItem

  11. book : Book myLibrary : Library myList : LibraryItemList *pItem : Book Library user DemoLibrary Problem UML Sequence DiagramShowing the interaction in the DemoLibrary program book("Java How to Program",4) book addLibraryItem(book) push_back(book) new Book(book) One way to fix the problem is to use overloaded member functions in these places. Then we’ll know which derived class to allocate space for. pItem

  12. Review (Deitel, 283) Review: Function Overloading • Using the same name for two or more different functions • To perform operations that are similar, but that must be implemented differently for different inputs double maximum( double x, double y ){ return ( x > y ) ? x : y; } int maximum( int x, int y ){ return ( x > y ) ? x : y; } double maximum( double x, double y, double z ){ double temp = ( x > y ) ? x : y; return ( temp > z ) ? temp : z; }

  13. Review (Deitel, 283) Review: Function Signatures • Function’s name + parameter types • Overloaded functions must have different signatures • Return types can be the same or different signature double maximum( double x, double y ){ return ( x > y ) ? x : y; } int maximum( int x, int y ){ return ( x > y ) ? x : y; } double maximum( double x, double y, double z ){ double temp = ( x > y ) ? x : y; return ( temp > z ) ? temp : z; } signature signature

  14. DemoLibrary Problem Function Overloading in DemoLibrary • Member functions can also be overloaded • Overloaded member functions must have different signatures class Library{ public: void addLibraryItem( Book& book ){ myList.push_back(book); } void addLibraryItem( Journal& journal ){ myList.push_back(journal); } //...

  15. DemoLibrary Problem Function Overloading in DemoLibrary class LibraryItemList{ public: void push_back( Book &book ){ if( sz < capacity ){ LibraryItem *pItem = new Book(book); myData[sz] = pItem; sz++; } else cout << "FULL\n"; } void push_back( Journal &journal ){ if( sz < capacity ){ LibraryItem *pItem = new Journal(journal); myData[sz] = pItem; sz++; } else cout << "FULL\n"; } //... Now we can use polymorphism because the base-class pointer is pointing to a derived-class object

  16. Templates Chapter 14

  17. Templates (Deitel, 749) Reusability • An important design goal of software engineering • Advantages • Saves time • Better to use code that is already tested

  18. Templates (Deitel, 749) Templates • An important feature in C++ • Supports software reuse • We can write one class or function, and then use it with many data types • Generic programming • Using class templates • Many class templates are available in the Standard Template Library (STL)

  19. Templates (Deitel, 283) Review: Inappropriate Overloading • These two functions have different signatures, so overloading is allowed • However, the implementation is the same • A better approach is to use a function template double maximum( double x, double y ){ return ( x > y ) ? x : y; } int maximum( int x, int y ){ return ( x > y ) ? x : y; }

  20. Templates (Deitel, 283) Review: Function Templates • Allow us to write a single function definition that can be used with any data type • The program logic inside the function must be the same for every data type • Begins with “template < typename T >” • The parameters are placeholders for the data type – the compiler will make the substitution later template < typename T > //use either “class” or “typename” Tmaximum( T x, T y ){ return ( x > y ) ? x : y; }

  21. Templates (Deitel, 283) Review: Using a Function Template #include <iostream> using namespace std; template < typename T > Tmaximum( T x, T y ){ return ( x > y ) ? x : y; } int main(){ int a = 1, b = 2; cout << maximum(a,b) << endl; double x = 3.0, y = 4.0; cout << maximum(x,y) << endl; } The compiler automatically detects the datatype of the argument and substitutes that datatype at every location of ‘T’

  22. Templates Using Function Templatesin DemoLibrary • Function templates can also be used inside a class • For example, in LibraryItemList, we used overloaded member functions • Each has a different derived-class data type as parameter • The code inside these member functions is almost identical except for the data type of the parameters • This is a good place to use a function template.

  23. Templates Using Function Templatesin DemoLibrary class LibraryItemList{ public: void push_back( Book &book ){ if( sz < capacity ){ LibraryItem *pItem = new Book(book); myData[sz] = pItem; sz++; } else cout << "FULL\n"; } void push_back( Journal &journal ){ if( sz < capacity ){ LibraryItem *pItem = new Journal(journal); myData[sz] = pItem; sz++; } else cout << "FULL\n"; } 1. Start with the overloaded member functions The code is almost the same, except for the derived-class data type

  24. Templates Using Function Templatesin DemoLibrary class LibraryItemList{ public: void push_back( Book&book ){ if( sz < capacity ){ LibraryItem *pItem = new Book(book); myData[sz] = pItem; sz++; } else cout << "FULL\n"; } 2. Delete all but one of the overloaded member functions

  25. Templates Using Function Templatesin DemoLibrary class LibraryItemList{ public: template < typename Derived > void push_back( Book&book ){ if( sz < capacity ){ LibraryItem *pItem = new Book(book); myData[sz] = pItem; sz++; } else cout << "FULL\n"; } 3. Add this line

  26. Templates Using Function Templatesin DemoLibrary class LibraryItemList{ public: template < typename Derived > void push_back( Book &book ){ if( sz < capacity ){ LibraryItem *pItem = new Book(book); myData[sz] = pItem; sz++; } else cout << "FULL\n"; } 4. Find all the locations of the derived-class data type

  27. Templates Using Function Templatesin DemoLibrary class LibraryItemList{ public: template < typename Derived > void push_back( Derived &item ){ if( sz < capacity ){ LibraryItem *pItem = new Derived(item); myData[sz] = pItem; sz++; } else cout << "FULL\n"; } 5. Substitute the word “Derived” for the derived class data type

  28. Templates Using LibraryItemList with any Data • So far, our LibraryItemList class will do a good job of containing data of the type LibraryItem • What happens if we want to store something else? • Brute force solution – Write a different class for every type of data we might want to store – RentalItemList, CustomerList, etc. What’s the disadvantage of that approach?

  29. Templates (Deitel, 754) Class Templates • Better approach • Write a class template • A class template can store any kind of data • Instead of making a list of LibraryItems, we can make a list of “anything” • The behavior for all the data types will be the same and we only need to write the code once • Called “generic programming”

  30. “typename” or “class” “Object” or any word – the compiler will replace this word with an actual data type Templates (Deitel, 754) Class Templates • Allow us to use one class implementation for many different data types • Syntax template <typename Object> class List{ private: Object *pMyData;

  31. Templates (Deitel, 754) Using a Class Templatein DemoLibrary 1. Start with the class that stores a specific data type class LibraryItemList { public: LibraryItemList( int cap = 32 ):CAPACITY(cap),sz(0){ myData = new LibraryItem*[CAPACITY]; } ~LibraryItemList(){ for(int i=0; i<sz; ++i) delete myData[i]; delete[] myData; } int size(){ return sz; } bool empty(){ return sz == 0; } void printList(){ if( empty() ) cout << "List is empty\n"; else{ for( int i=0; i<sz; ++i ) cout << myData[i]->toString() << '\n'; } } private: const int CAPACITY; int sz; LibraryItem **myData; };

  32. Templates (Deitel, 754) Using a Class Templatein DemoLibrary 2. Add this line template < typename Base > class LibraryItemList { public: LibraryItemList( int cap = 32 ):CAPACITY(cap),sz(0){ myData = new LibraryItem*[CAPACITY]; } ~LibraryItemList(){ for(int i=0; i<sz; ++i) delete myData[i]; delete[] myData; } int size(){ return sz; } bool empty(){ return sz == 0; } void printList(){ if( empty() ) cout << "List is empty\n"; else{ for( int i=0; i<sz; ++i ) cout << myData[i]->toString() << '\n'; } } private: const int CAPACITY; int sz; LibraryItem **myData; };

  33. Templates (Deitel, 754) Using a Class Templatein DemoLibrary 3. Find all the locations of the data type of the elements in the list template < typename Base > class LibraryItemList { public: LibraryItemList( int cap = 32 ):CAPACITY(cap),sz(0){ myData = new LibraryItem*[CAPACITY]; } ~LibraryItemList(){ for(int i=0; i<sz; ++i) delete myData[i]; delete[] myData; } int size(){ return sz; } bool empty(){ return sz == 0; } void printList(){ if( empty() ) cout << "List is empty\n"; else{ for( int i=0; i<sz; ++i ) cout << myData[i]->toString() << '\n'; } } private: const int CAPACITY; int sz; LibraryItem **myData; };

  34. Templates (Deitel, 754) Using a Class Templatein DemoLibrary 4. Substitute “Base” for the data type of the elements in the list template < typename Base > class LibraryItemList { public: LibraryItemList( int cap = 32 ):CAPACITY(cap),sz(0){ myData = new Base*[CAPACITY]; } ~LibraryItemList(){ for(int i=0; i<sz; ++i) delete myData[i]; delete[] myData; } int size(){ return sz; } bool empty(){ return sz == 0; } void printList(){ if( empty() ) cout << "List is empty\n"; else{ for( int i=0; i<sz; ++i ) cout << myData[i]->toString() << '\n'; } } private: const int CAPACITY; int sz; Base **myData; };

  35. Templates (Deitel, 754) Using an External Definition of a Member Function in a Template template < typename Base > class LibraryItemList { public: LibraryItemList( int cap = 32 ):CAPACITY(cap),sz(0){ myData = new Base*[CAPACITY]; } ~LibraryItemList(){ for(int i=0; i<sz; ++i) delete myData[i]; delete[] myData; } int size(){ return sz; } bool empty(){ return sz == 0; } void printList(); private: const int CAPACITY; int sz; Base **myData; }; template < typename Base > void LibraryItemList<Base>::printList(){ if( empty() ) cout << "List is empty\n"; else{ for(int i=0; i<sz; ++i) cout << myData[i]->toString();} } 1. Put this line above every function definition 2. Add “Base” wherever the data type should appear

  36. Templates (Deitel, 754) Caution! • The entire class template must go into the header file! • In C++ a common technique is to put the declaration of a class in the header file and the definition in a cpp file, but it does not work with class templates

  37. Templates (Deitel, 754) Instantiating a List with a Template Class • Include it in your program #include "List.h" • Instantiate a list with the default size List<LibraryItem> myLibraryItemList; • Instantiate a list with a specific size List<LibraryItem> smallList(10); Name of the class Type of data in it Name of the list object Size of the list

  38. Templates (Deitel, 754) Non-Type Parameters • The parameters of a class template can be non-type parameters Example template < typename Base, int CAP = 32 > class List{ public: List(int cap=CAP):capacity(cap),sz(0) { myData = new Base*[capacity]; } ~List() { for( int i=0; i<sz; ++i ) delete myData[i]; delete [] myData; } bool insert( Base* pItem ); private: int sz; int capacity; Base **myData; };

  39. References Deitel, H. M., and P. J. Deitel, C++ How to Program, Fifth Edition. Upper Saddle River, NJ: Prentice Hall, 2005. Stevens, P., with R. Pooley, Using UML, Software Engineering with Objects and Components, Update Edition. Essex, England: Addison Wesley, 2000.

More Related