Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.

Similar presentations


Presentation on theme: "© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0."— Presentation transcript:

1 © Copyright Eliyahu Brutman Programming Techniques Course Version 1.0

2 © Copyright Eliyahu Brutman Chapter 9 – Multiple Inheritance Version 1.0

3 © Copyright Eliyahu Brutman Multiple Inheritance - 3 Lets recall What we have seen so far Polymorphic inheritance  For defining generic interface  Accessing the concrete implementations via this interface  The compiler implements code, which extracts during Run-Time the needed calling address  Through a virtual pointer Which usually point at the actual object  And through a virtual table Which holds the addresses of the virtual functions

4 © Copyright Eliyahu Brutman Multiple Inheritance - 4 Construction and Destruction Lets recall the construction order and functionality What is the order of “ things ” Lets see what happens during destruction What is the order of “ things ” Can a destructor be virtual? Does it need to be virtual? When? Class Employee Class SalesPersonClass ExecutiveClass Administrative m_salary v. CalcSalary() m_salesm_performance

5 © Copyright Eliyahu Brutman Multiple Inheritance - 5 Multiple Inheritance Sometimes it is desirable for a derived class to inherit features from more than one base class. How is this represented in memory? What is the layout of the instance? Can you think of problems using this? Class Employee Class SalesPersonClass ExecutiveClass Administrative m_salary v. CalcSalary() m_salesm_performance Class Student v. M() Class Metargel v. CalcSalary() v. M() Class Person

6 © Copyright Eliyahu Brutman Multiple Inheritance - 6 Syntax class Metargel : public Student, public Employee { … } Teaching Assistant maintains the attributes and the methods associated with both base classes Student, and Employee

7 © Copyright Eliyahu Brutman Multiple Inheritance - 7 Polymorphic Assignment Metargel * myMetargel = new Metargel; Employee * E = myMetargel; //Legal as a Teaching Assistant is-an Employee Student * S = myMetargel; //Legal as a Teaching Assistant is-a Student

8 © Copyright Eliyahu Brutman Multiple Inheritance - 8 Problems with Multiple Inheritance Name Ambiguity: Similar names can be used for different operations in the base classes  e.g. an employee might have an id_number and a student might also have an id_number field. Both base classes might have a similar get_id() method. The compiler cannot determine which version to use in the Metargel class: the get_id() in the Employee class or the get_id () in the Student class. A common misuse of multiple inheritance is using it as composition rather than specialization (is-a): The following is incorrect: class car: public Engine, public Transmission, public Wheels

9 © Copyright Eliyahu Brutman Multiple Inheritance - 9 Name Ambiguity Solution 1: Use a fully qualified function name: Metargel * myMetargel = new Metargel; cout << “ The TeachingAssistant is” << myMetargel -> Employee::get_id() << “\n”;

10 © Copyright Eliyahu Brutman Multiple Inheritance - 10 Name Ambiguity Solution 2: Redefine the ambiguous function in the new class and hide the qualified name in a method body: class Metargel : public Student, public Employee { public: string get_id(); public: string student_id(); } string Metargel ::get_id() { return Employee::get_id(); } string Metargel ::student_id() { return Student::get_id(); }

11 © Copyright Eliyahu Brutman Multiple Inheritance - 11 Virtual Inheritance - Motivation What if Class student and class Employee both inherit from Person? How can this be dealt with ? Class Employee Class SalesPersonClass ExecutiveClass Administrative m_salary v. CalcSalary() m_salesm_performance Class Student v. M() Class Metargel v. CalcSalary() v. M() Class Person

12 © Copyright Eliyahu Brutman Multiple Inheritance - 12 Replicated Base Classes The same class can not be directly inherited more than once Base classes might be inherited indirectly more than once due to a class inheritance hierarchy. In our example suppose the following: class Employee : public Person {..} class Student : public Person {..} class Metargel : public Student, public Employee {..} Attributes from the Person class get inherited twice! myMetargel will have two names

13 © Copyright Eliyahu Brutman Multiple Inheritance - 13 Replicated Inheritance Recall that replicated inheritance does not share the same common grandparent even if they both derive from the same grandparent! If we have code like this in C++: class A { … }; class B : public A { … }; class C : public A { … };/* Derives from A but it is not shared! */ class D : public B, public C { … }; This gives us the following situation: A BC A D A* a; B* b; C* c; D* d; b = d; // ok c = d; // ok a = b; // ok a = c; // ok a = d; //error ambiguous

14 © Copyright Eliyahu Brutman Multiple Inheritance - 14 Virtual Inheritance Introducing Virtual Inheritance Single instance within layout. Class Employee Class SalesPersonClass ExecutiveClass Administrative m_salary v. CalcSalary() m_salesm_performance Class Student v. M() Class Metargel v. CalcSalary() v. M() Class Person

15 © Copyright Eliyahu Brutman Multiple Inheritance - 15 Virtual Base Classes To merge any common base classes into one single copy the inheritance should be written as virtual: class Employee: virtual public Person {..} class Student: virtual public Person {..} class TeachingAssistant : public Student, public Employee {..}

16 © Copyright Eliyahu Brutman Multiple Inheritance - 16 Virtual Inheritance Standard base classes D members appear twice in C Virtual base classes class A : public virtual D { … } Avoid duplication of base class members Require additional pointers so that D part of A, B parts of object can be shared C AB D A part D part C part B part

17 © Copyright Eliyahu Brutman Multiple Inheritance - 17 Virtual Base Classes Metargel * myMetargel = new Metargel; Student * s = myMetargel; // Legal due to is-a relationship Person * p = s; // Legal due to is-a relationship Employee * e = myMetargel;// Legal due to is-a relationship Person * p1 = e; // Legal due to is-a relationship Person * p2 = myMetargel; // Legal only if Virtual Inheritance is used so // that the compiler knows which version of //person to use, error otherwise

18 © Copyright Eliyahu Brutman Multiple Inheritance - 18 Multiple Inheritance - General It is recommended to keep multiple inheritance to minimal usage Due to complexity, which may arise Due to misusage You may use it, but please use it with care Recommended to use as many as possible abstract classes, and not multiple inheritance Make sure this really models your problem domain well


Download ppt "© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0."

Similar presentations


Ads by Google