Multiple Inheritance Copyright Eliyahu Brutman Multiple Inheritance Lets
Multiple Inheritance © Copyright Eliyahu Brutman
Multiple Inheritance - Lets recall n 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 © Copyright Eliyahu Brutman 2
Multiple Inheritance - Construction and Destruction n Lets recall the construction order and functionality n n Lets see what happens during destruction n n What is the order of “things” Can a destructor be virtual? Does it need to be virtual? When? Class Employee m_salary v. Calc. Salary() Class Sales. Person m_sales v. Calc. Salary() © Copyright Eliyahu Brutman Class Executive m_performance v. Calc. Salary() Class Administrative v. Calc. Salary() 3
Multiple Inheritance - Multiple Inheritance n 4 Sometimes it is desirable for a derived class to inherit features from more than one base class. Class Person Class Student Class Employee m_salary v. Calc. Salary() v. M() Class Metargel v. Calc. Salary() v. M() © Copyright Eliyahu Brutman Class Sales. Person m_sales v. Calc. Salary() Class Executive m_performance v. Calc. Salary() Class Administrative v. Calc. Salary()
Syntax Multiple Inheritance - class Metargel : public Student, public Employee { … } Teaching Assistant maintains the attributes and the methods associated with both base classes Student, and Employee © Copyright Eliyahu Brutman 5
Polymorphic Assignment Metargel * my. Metargel = new Metargel; Employee * E = my. Metargel; //Legal as a Teaching Assistant is-an Employee Student * S = my. Metargel; //Legal as a Teaching Assistant is-a Student © Copyright Eliyahu Brutman Multiple Inheritance - 6
Multiple Inheritance - 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. n 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 n © Copyright Eliyahu Brutman 7
Multiple Inheritance - Name Ambiguity Solution 1: Use a fully qualified function name: Metargel * my. Metargel = new Metargel; cout << “ The Teaching. Assistant is” << my. Metargel -> Employee: : get_id() << “n”; © Copyright Eliyahu Brutman 8
Multiple Inheritance - 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(); } © Copyright Eliyahu Brutman 9
Multiple Inheritance - Virtual Inheritance - Motivation n What if Class student and class Employee both inherit from Person? n How can this be dealt with ? Class Person Class Student Class Employee m_salary v. Calc. Salary() v. M() Class Metargel v. Calc. Salary() v. M() © Copyright Eliyahu Brutman Class Sales. Person m_sales v. Calc. Salary() Class Executive m_performance v. Calc. Salary() Class Administrative v. Calc. Salary() 10
Multiple Inheritance - Replicated Base Classes The same class can not be directly inherited more than once n Base classes might be inherited indirectly more than once due to a class inheritance hierarchy. n In our example suppose the following: n class Employee : public Person {. . } class Student : public Person {. . } class Metargel : public Student, public Employee {. . } n Attributes from the Person class get inherited twice! my. Metargel will have two names © Copyright Eliyahu Brutman 11
Multiple Inheritance - Replicated Inheritance Recall that replicated inheritance does not share the same common grandparent even if they both derive from the same grandparent! n If we have code like this in C++: n class A { … }; class B : public A { … }; class C : public A { … }; /* Derives from A but it is not shared! */ class D : public B, public C { … }; n This gives us the following situation: A* a; B* b; C* c; D* d; b = d; // ok c = d; // ok a = b; // ok a = c; // ok a = d; //error ambiguous © Copyright Eliyahu Brutman A A B C D 12
Multiple Inheritance - Virtual Inheritance n Introducing Virtual Inheritance n Single instance within layout. Class Person Class Student Class Employee m_salary v. Calc. Salary() v. M() Class Metargel v. Calc. Salary() v. M() © Copyright Eliyahu Brutman Class Sales. Person m_sales v. Calc. Salary() Class Executive m_performance v. Calc. Salary() Class Administrative v. Calc. Salary() 13
Multiple Inheritance - Virtual Base Classes n 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 Teaching. Assistant : public Student, public Employee {. . } © Copyright Eliyahu Brutman 14
Multiple Inheritance - Virtual Inheritance D Standard base classes D members appear twice in C n 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 n A B C A part B part C part D part © Copyright Eliyahu Brutman 15
Multiple Inheritance - Virtual Base Classes Metargel * my. Metargel = new Metargel; Student * s = my. Metargel; // Legal due to is-a relationship Person * p = s; // Legal due to is-a relationship Employee * e = my. Metargel; // Legal due to is-a relationship Person * p 1 = e; // Legal due to is-a relationship Person * p 2 = my. Metargel; // Legal only if Virtual Inheritance is used so // that the compiler knows which version of //person to use, error otherwise © Copyright Eliyahu Brutman 16
Multiple Inheritance - General n It is recommended to keep multiple inheritance to minimal usage n Due to complexity, which may arise n Due to misusage n You may use it, but please use it with care n Recommended to use as many as possible abstract classes, and not multiple inheritance n Make sure this really models your problem domain well © Copyright Eliyahu Brutman 17
- Slides: 17