Presentation is loading. Please wait.

Presentation is loading. Please wait.

16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

Similar presentations


Presentation on theme: "16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8."— Presentation transcript:

1 16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8 Presentation Copyright 1999, Franklin, Beedle & Associates Students who purchase and instructors who adopt Computing Fundamentals with C++, Object-Oriented Programming and Design by Rick Mercer are welcome to use this presentation as long as this copyright notice remains intact.

2 16-2 Chapter 16: Object-Oriented Software Development Inheritance and Polymorphism  Chapter Objectives  Recognize generalization that may be implemented with inheritance  Derive classes from new ones  Override member functions and add new features to derived classes  Apply object-oriented design heuristics for inheritance  Understand and use polymorphism

3 16-3 Problem Specification The college library has requested a system that supports a small set of library operations. The librarian allows a student to borrow certain items, return those borrowed items, and pay fees. Late fees and due dates have been established at the following rates: Late fee Length of Borrow Late fee Length of Borrow books: $0.50 per day 14 days books: $0.50 per day 14 days video tape:$5.00 one day late plus $1.50 2 days video tape:$5.00 one day late plus $1.50 2 days for each additional day for each additional day CD Roms$2.50 per day 7 days CD Roms$2.50 per day 7 days The due date is set when the borrowed item is checked out. A student with more than 7 borrowed items, any one late item, or late fees greater than $25.00 may not borrow anything new.

4 16-4 An OO Methodology - a review  Identify classes that model (shape) the system as a natural and sensible set of abstractions.  Determine the purpose, or main responsibility of each class. The responsibilities of a class are what an instance of the class must be able to do (member functions) and what each object must know about itself (data members).  Determine the helper classes for each. To help complete its responsibility, a class typically delegates responsibility to one or more other objects. These are called helpers.

5 16-5 Identify the classes  The team considers these as possible key abstractions that model one solution — librarian — student — book — video — cdRom — 7 borrowed books

6 16-6 Some classes have some things in common  The team notices that books, CDRoms, and videos have some common responsibilities — they are all items that can be borrowed  They have these common attributes  know due date  compute due date  determine late fee  know borrower  check self out  check self in

7 16-7 The Inheritance Relationship  When a set of classes have some commonalties, a potential inheritance hierarchy exists  The inheritance relationship — lets a base class capture common operations and data for a set of classes that differ in some way — allows extension of the base class by adding operations adding operations redefining operations of the base class redefining operations of the base class adding new data members adding new data members

8 16-8 When is the inheritance relationship appropriate?  Object-Oriented Design Heuristic: If two or more classes have common data and behavior, then those classes should inherit from a common base class that captures those data and methods

9 16-9 An inheritance hierarchy lendable is the abstract class never instantiated concrete classes

10 16-10 Why not just have one class?  Some of the behavior differs — Compute due date — Compute late fee  Data differs — books have ISBNs — videos may have the studio identifier  First the abstract class lendable next slide

11 16-11 Draw a picture showing some relationship not very formal

12 16-12 CRH Card for an abstract class

13 16-13 Designing C++ Class Definitions class lendable { // first draft public: //... //... bool isOverdue(); bool isOverdue(); bool isAvailable(); bool isAvailable(); void checkSelfIn(); void checkSelfIn(); void checkSelfOut(string borrowersID); void checkSelfOut(string borrowersID);private: // TBA // TBA};

14 16-14 Should we add computeDate as a public member?  Object-Oriented Design Heuristic Do not put implementation details into the public interface of a class.  Public or private? What about protected? — Protected members — are not part of the interface — they are known in the base class and can be called by any derived class  Second draft also considers const

15 16-15 protected: and const class lendable { // second raft public: //... //... void checkSelfIn(); void checkSelfIn(); void checkSelfOut(string borrowersID); void checkSelfOut(string borrowersID); bool isOverdue() const; bool isOverdue() const; bool isAvailable() const; bool isAvailable() const;protected: date computeDueDate() const; date computeDueDate() const; double computeLateFee() const; double computeLateFee() const;private: // TBA // TBA};

16 16-16 Avoid selection  Both protected functions will be called from the base class members checkSelfIn and checkSelfOut — computeDueDate and computeLateFee  But which one gets called? — could write code like this explicit case analysis if(lendableIsBook()) if(lendableIsBook()) book::computeDueDate() book::computeDueDate() else if(lendableIsVideo()) else if(lendableIsVideo()) video::computeDueDate() video::computeDueDate() else else cdRom::computeDueDate() cdRom::computeDueDate()

17 16-17 Avoid explicit case analysis  Object-Oriented Design Heuristic Don't add a data member that indicates the class's type. If you have to make a decision based on different classes of objects, implement an inheritance hierarchy.  Let polymorphism take over — polymorphism: the ability for different classes of objects to respond to the same message in different ways

18 16-18 Containers can store collections of inheritance linked objects

19 16-19 Why pure virtual?  These member should be converted to pure virtual functions. — The compiler ensures that each and every derived class implements the member — Assuming the designer makes them pure virtual as shown in draft four next slide

20 16-20 Derived classes must implement Pure virtual functions class lendable { // third draft public: //... //... void checkSelfIn(); void checkSelfIn(); void checkSelfOut(string borrowersID); void checkSelfOut(string borrowersID); bool isOverdue() const; bool isOverdue() const; bool isAvailable() const; bool isAvailable() const;protected: virtual Date computeDueDate() const = 0; virtual Date computeDueDate() const = 0; // derived class must implement these // derived class must implement these virtual double computeLateFee() const = 0; virtual double computeLateFee() const = 0;private: // TBA // TBA};

21 16-21 Data members and constructors  Add the appropriate data members  Add constructors to initialize data  Make sure you add a default constructor if you have a constructor with one or more arguments  The final draft of lendable next slide

22 16-22 class lendable { public: lendable(string initID); lendable(string initID); bool isOverdue() const; bool isOverdue() const; bool isAvailable() const; bool isAvailable() const; string lendableID() const; string lendableID() const; string borrowersID() const; string borrowersID() const; double lateFee() const; double lateFee() const; Date dueDate() const; // needed by derived classes Date dueDate() const; // needed by derived classes//--modifiers void checkSelfIn(); void checkSelfIn(); void checkSelfOut(string borrowersID); void checkSelfOut(string borrowersID); //--pure virtual functions protected: virtual Date computeDueDate() const = 0; virtual Date computeDueDate() const = 0; virtual double computeLateFee() const = 0; virtual double computeLateFee() const = 0; private: string my_ID; string my_ID; Date my_dueDate; Date my_dueDate; bool my_availability; bool my_availability; string my_borrowersID; string my_borrowersID;};

23 16-23 The lendable constructor has an initializer list more efficient  Consider the following okay constructor lendable::lendable(string initID) lendable::lendable(string initID) { // less efficient { // less efficient my_ID = initID; my_ID = initID; my_dueDate = emptyDate; my_dueDate = emptyDate; my_availability = true; my_availability = true; my_borrowersID = emptyID; my_borrowersID = emptyID; }  Now with an initializer list lendable::lendable(string initID) lendable::lendable(string initID) : my_ID(initID), : my_ID(initID), my_dueDate(emptyDate), my_dueDate(emptyDate), my_availability(true), my_availability(true), my_borrowersID(emptyID) my_borrowersID(emptyID) { // More efficient initialization already occurred { // More efficient initialization already occurred } }

24 16-24 Need initializer to call base constructor only way to do it  Derived classes typically call base class constructor — Initializes common data — performs other important initialization book::book(string initID, string initAuthor, string initAuthor, string initTitle) string initTitle) : lendable (initID), // call base constructor : lendable (initID), // call base constructor my_author(initAuthor), my_author(initAuthor), my_title(initTitle) my_title(initTitle) { // The initialization list took care of everything. // Remember, the lendable constructor was also called // Remember, the lendable constructor was also called // to initialize the data members that are common // to initialize the data members that are common // to all derived classes. // to all derived classes. // This is the only way to call a base constructor // This is the only way to call a base constructor}

25 16-25 Polymorphism in action void lendable::checkSelfOut(string borrowersID) { my_dueDate = computeDueDate(); my_dueDate = computeDueDate(); // polymorphism in action. At runtime, the // polymorphism in action. At runtime, the // system knows which computeDueDate // system knows which computeDueDate // implementation to use. // implementation to use. my_availability = false; my_availability = false; my_borrowersID = borrowersID; my_borrowersID = borrowersID;}  Which computeDate is called?

26 16-26 Inheritance and Polymorphism  Two other major features of the object- oriented paradigm: — inheritance: the ability to derive a new class from an existing class. — polymorphism: the ability for different types of objects to respond to the same message in a different way.

27 16-27 Deriving new classes class derived-class-name : public ancestor-class-name { public: new function-heading-1 ; overridden function-heading -1 ; new function-heading-2 ; overridden function-heading -2 ; private:additional-data-members } ;

28 16-28 Example class book : public lendable { public://--constructor book(string initID, string initAuthor, book(string initID, string initAuthor, string initTitle); string initTitle); //--virtual functions that must be implemented Date computeDueDate() const; Date computeDueDate() const; double computeLateFee() const; double computeLateFee() const; //--additional accessors string author(); string author(); string title(); string title(); private: string my_author; // Additional data members string my_author; // Additional data members string my_title; string my_title;};

29 16-29 Another Example class video : public lendable { public://--constructor video(string initID, string initTitle); video(string initID, string initTitle); //--virtual functions that must be implemented Date computeDueDate() const; Date computeDueDate() const; double computeLateFee() const; double computeLateFee() const; //--additional accessors string title(); string title(); private: string my_title; string my_title;};

30 16-30 See and demo these files need lendable.h lendable.cpp Demo testleli.cpp, testlend.cpp  Output on the next two slides — Show a collection of pointers to the base class to have a "polymorphic list"

31 16-31 Output from test driver TEST BOOK: The lendable QA76.1M46 is not overdue. Late fee = $0.00 Due date: January 01 1900 It is available. -------------------------------------------- The lendable QA76.1M46 is overdue. Late fee = $7.00 Due date: April 08 1998 It is not available. 555-55-5555 has it. -------------------------------------------- The lendable QA76.1M46 is not overdue. Late fee = $0.00 Due date: January 01 1900 It is available. --------------------------------------------

32 16-32 More Output TEST VIDEO: The lendable MGM10023 is not overdue. Late fee = $0.00 Due date: January 01 1900 It is available. -------------------------------------------- The lendable MGM10023 is overdue. Late fee = $2.00 Due date: April 20 1998 It is not available. 555-55-5555 has it. -------------------------------------------- The lendable MGM10023 is not overdue. Late fee = $0.00 Due date: January 01 1900 It is available. --------------------------------------------


Download ppt "16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8."

Similar presentations


Ads by Google