Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.

Similar presentations


Presentation on theme: "Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012."— Presentation transcript:

1 Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

2 Contents Inheritance Revisited Containment: Has-a Relationships Abstract Base Classes Revisited Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 http://mediaplayer.pearsoncmg.com/_ph_cc_ecs960544_set.title.Cpp_Inheritance_and_Access __/aw/streaming/ecs_carrano_dapscpp_6/CI4_C_Inheritance_and_Polymorphism.m4v

3 Inheritance Revisited Inheritance: Relationships among timepieces Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

4 Inheritance Revisited Multiple inheritance Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

5 Inheritance Revisited The class PlainBox Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

6 Inheritance Revisited Derived class MagicBox inherits members of base class PlainBox, redefines and adds members Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 New Redefined

7 Inheritance Revisited The class MagicBox Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

8 Inheritance Revisited Instance of derived class has all behaviors of its base class Derived class inherits private members from base class – But cannot access them directly Derived class’s methods can call base class’s public methods Clients of a derived class can invoke base class’s public methods Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

9 Inheritance Revisited Early, or static, binding: Compiler determines which version of a method to invoke Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

10 Inheritance Revisited Late binding: appropriate version of method decided at execution time Polymorphic method has multiple meanings Virtual method: can be overridden Method virtual in base class, virtual in any derived class Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

11 Public, Private, and Protected Sections of a Class Virtual method tables when cardBoxPtr points to (a) an instance of PlainBox Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

12 Public, Private, and Protected Sections of a Class Access to public, private, and protected sections of a class by a client and a derived class Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

13 Public, Private, and Protected Sections of a Class Virtual method tables when cardBoxPtr points to (b) an instance of MagicBox Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

14 Access Categories of a Class Public members can be used by anyone. Private members can be used only by methods of the class. Guideline: In general, class’s data members should be private Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

15 Kinds of Inheritance Public inheritance – Public and protected members of base class remain, public and protected members of derived class Protected inheritance – Public and protected members of base class are protected members of derived class. Private inheritance – Public and protected members of base class are private members of the derived class. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

16 Is-a and As-a Relationships A magic box is a plain box Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

17 Is-a and As-a Relationships Use public inheritance only when is-a relationship exists between two classes You can use instance of derived class – anywhere you can use instance of base class If relationship between 2 classes not is-a, – should not use public inheritance If class needs access to protected members of another class or if you need to redefine methods in that class, – You can form an as-a relationship Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

18 Containment: Has-a Relationships A pen has a or contains a ball Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

19 Execution Order of Constructors and Destructors Constructors execute in following order: 1.Its base class constructor executes. 2.Constructors of its member objects execute in the declaration order. 3.The body of its constructor executes. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

20 Execution Order of Constructors and Destructors Destructors execute in the opposite order: 1.The body of its destructor executes. 2.Destructors of its member objects execute in the declaration order. 3.Its base class destructor executes. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

21 Abstract Base Classes Revisited CDP and DVDP have an abstract base class Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

22 Abstract Base Classes Revisited CDP and DVDP are derived from GDP Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

23 Abstract Base Classes Revisited A class that contains at least one pure virtual method is an abstract base class An abstract base class has descendants but no instance Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

24 Abstract Base Classes Revisited An abstract class that is an interface for the ADT box Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

25 Abstract Base Classes Revisited UML class diagram of the family of box classes Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

26 Abstract Base Classes Revisited By definition is a class that contains at least one pure virtual method. Used only as the basis for derived classes – Thus defines a minimum interface for its descendants. Has no instances. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

27 Abstract Base Classes Revisited Should, in general, omit implementations – Except for the destructor methods that provide access to private data members. That is, virtual methods in an abstract base class usually should be pure. Must implement any virtual method that is not pure, – Thereby providing a default implementation if derived class chooses not to supply its own. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012


Download ppt "Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012."

Similar presentations


Ads by Google