Presentation is loading. Please wait.

Presentation is loading. Please wait.

SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 1 L4: Multiple Inheritance Introduction Problems of Single Inheritance and solutions Problems of Multiple.

Similar presentations


Presentation on theme: "SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 1 L4: Multiple Inheritance Introduction Problems of Single Inheritance and solutions Problems of Multiple."— Presentation transcript:

1 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 1 L4: Multiple Inheritance Introduction Problems of Single Inheritance and solutions Problems of Multiple Inheritance: Name ambiguity Substitutability Common ancestors 5 Solutions to the Problems Virtual Inheritance Inner Classes Chapter 13 of Budd

2 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 2 Multiple Inheritance Where a child class can inherit from more than one parent class directly Supported in C++, Eiffel Not supported in Java, C#

3 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 3 A Quote Multiple inheritance allows several objects to act as base objects... The characteristics of several different object classes can be combined to make up a new object. For example, say we have an object class CAR and an object class PERSON. We could use both of these to define a new object class CAR-OWNER … Sommerville, “Software Engineering”, 4th Edition

4 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 4 Problem of Single Inheritance We can divide the world into an infinite number of hierarchies For complex problems strict hierarchies almost always end up with trade-offs and ambiguities.

5 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 5 Example Object Ordered Char Number Integer Float FractionComplex?

6 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 6 Solutions Make Complex a subclass of Number and override Ordered methods Avoid inheritance, and redefine all methods Use a partial inheritance hierarchy Use multiple inheritance

7 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 7 Problems of Multiple Inheritance Name ambiguity Substitutability Common ancestors

8 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 8 Name Ambiguity The same name can mean different things to different ancestors GraphicalCardDeck CardDeck draw()‏ GraphicalObject draw()‏

9 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 9 Solution A Explicit disambiguation: GraphicalCardDeck gcd; gcd.CardDeck::draw(); gcd.GraphicalObject::draw();

10 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 10 Solution B With different type signatures, can use straight overloading: class GCD : public CardDeck, public GO { public: virtual Card *draw(){ return CardDeck::draw();}; virtual void draw(GraphicsContext &g){ GraphicalObject::draw(g);}; };

11 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 11 Solution B2 class GCD : public CardDeck, public GO { public: using CardDeck::draw; using GraphicalObject::draw; };

12 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 12 Solution C With the same type signatures, we're in trouble: class GCD: public CardDeck, public GO { public: virtual void draw(){ return CardDeck::draw();} virtual void paint(){GO::draw();}; }; GraphicalObject *g = new GraphicalCardDeck(); g->draw();

13 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 13 Solution D class CardDeckParent : public CardDeck { virtual void draw() {cardDeckdraw();}; virtual void cardDeckDraw() {CardDeck::draw();}; }; class GraphicalObjectParent: public GO { virtual void draw() {goDraw();}; virtual void goDraw() {GO::draw();}; }; class GraphicalCardDeck:public CDP,public GOP { virtual void cardDeckDraw() {CDP::cardDeckDraw();}; virtual void goDraw() {GOP::goDraw();}; };

14 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 14 Solution D GraphicalCardDeck *gcd = new GCD(); CardDeck *cd = dynamic_cast (gcd); GraphicalObject *go = dynamic_cast (gcd); cd->draw(); // ok go->draw(); // ok gcd->cardDeckDraw(); // ok gcd->goDraw(); // ok gcd->draw(); // error - ambiguous

15 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 15 Common Ancestors The “diamond of death”: A BC D AA BC D

16 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 16 C++ Example StreamInStream OutStream InOutStream InStream and OutStream inherit and override a method in Stream. InOutStream inherits the same method.

17 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 17 Virtual Inheritance class Stream {...}; class InStream: public virtual Stream {...}; class OutStream: public virtual Stream {...}; class InOutStream: public InStream, public OutStream {...}; The ambiguity occurs in InOutStream, and the solution involves the definitions of InStream and OutStream.

18 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 18 Inner Classes In languages which allow inner classes (Java, C#), we can simulate multiple inheritance after a fashion:

19 SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 19 Java Inner Classes class GraphicalCardDeck extends CardDeck { public void draw() { // draw a card from a deck} private class DrawingClass extends GraphicalObject { public void draw() {// draw a card on screen} } private DrawingClass drawer = new DrawingClass; public GraphicalObject myDrawingObject(){ return drawer;}; }


Download ppt "SFDV4001 / OOP with C++ / Lecture 4 / Polymorphism 1 L4: Multiple Inheritance Introduction Problems of Single Inheritance and solutions Problems of Multiple."

Similar presentations


Ads by Google