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

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Inheritance, Polymorphism, and Virtual Functions
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Abstraction, Inheritance, and Polymorphism in Java.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 12: Adding Functionality to Your Classes.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
13-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.
1 OO Design with Inheritance C Sc 335 Rick Mercer.
Object-Oriented Programming Chapter Chapter
Object Oriented Programming
ISBN Object-Oriented Programming Chapter Chapter
Coming up: Inheritance
OOP Review CS 124.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
1 OO Design with Inheritance C Sc 335 Rick Mercer.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
A First Book of C++ Chapter 12 Extending Your Classes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming Concepts
Polymorphism.
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
OO Design with Inheritance
Chapter 11 Generic Collections
Polymorphism Polymorphism
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
OO Design with Inheritance
CISC/CMPE320 - Prof. McLeod
Computing Fundamentals with C++
Fundaments of Game Design
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
CIS 199 Final Review.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
OO Design with Inheritance
OO Design with Inheritance
Lecture 6: Polymorphism
Computer Science II for Majors
Presentation transcript:

16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 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.

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

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 $ days video tape:$5.00 one day late plus $ 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.

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.

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

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

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

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

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

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

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

16-12 CRH Card for an abstract class

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

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

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 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()

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

16-18 Containers can store collections of inheritance linked objects

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

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

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

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

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

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}

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?

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.

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

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

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

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"

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

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