Chp~13 - C++ PROGRAMMING Chapter 13 Polymorphism Problems...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C++ PROGRAMMING Chapter 13 Polymorphism Problems with Single Inheritance Listing 13.1. If horses could fly ... Percolating Upward Casting Down Listing 13.2. Casting down . Adding to Two Lists Multiple Inheritance Listing 13.3. Multiple inheritance . Declaring Multiple Inheritance The Parts of a Multiply Inherited Object Figure 13.1. Constructors in Multiply Inherited Objects Listing 13.4. Calling multiple constructors . Ambiguity Resolution Inheriting from Shared Base Class Figure 13.2. Listing 13.5. Common base classes . Virtual Inheritance Figure 13.3. Listing 13.6. Illustration of the use of virtual inheritance . Declaring Classes for Virtual Inheritance Problems with Multiple Inheritance Mixins and Capabilities Classes Abstract Data Types Listing 13.7. Shape classes . Pure Virtual Functions Listing 13.8. Abstract Data Types . Abstract Data Types Implementing Pure Virtual Functions Listing 13.9. Implementing pure virtual functions . Complex Hierarchies of Abstraction Listing 13.10. Deriving ADTs from other ADTs . Which Types Are Abstract? The Observer Pattern A Word About Multiple Inheritance, Abstract Data Types, and Java Summary Q&A Quiz , Exercises Chapter 13 Polymorphism YesterChapter, you learned how to write virtual functions in derived classes. This is the fundamental building block of polymorphism: the capability to bind specific, derived class objects to base class pointers at runtime. ToChapter, you learnWhat multiple inheritance is and how to use it. What virtual inheritance is. What abstract data types are. What pure virtual functions are. Problems with Single Inheritance Suppose you've been working with your animal classes for a while and you've divided the class hierarchy into Birds and Mammals . The Bird class includes the member function Fly() . The Mammal class has been divided into a number of types of Mammal s, including Horse . The Horse class includes the member functions Whinny() and Gallop() . Suddenly, you realize you need a Pegasus object: a cross between a Horse and a Bird . A Pegasus can Fly() , it can Whinny() , and it can Gallop() . With single inheritance, you're in quite a jam.You can make Pegasus a Bird , but then it won't be able to Whinny() or Gallop() . You can make it a Horse , but then it won't be able to Fly() . Your first solution is to copy the Fly() method into the Pegasus class and derive Pegasus from Horse . This works fine, at the cost of having the Fly() method in two places ( Bird and Pegasus ). If you change one, you must remember to change the other. Of course, a developer who comes along months or years later to maintain your code must also know to fix both places....
View Full Document

Page1 / 38

Chp~13 - C++ PROGRAMMING Chapter 13 Polymorphism Problems...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online