Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.

Similar presentations


Presentation on theme: "Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006."— Presentation transcript:

1 Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

2 class Student { private: std::string id; public: std::string get_id(); }; class Employee { private: std::string id; public: std::string get_id(); };

3 Example:Syntax class TeachingAssistant:public Student, public Employee { … } Teaching Assistant maintains the attributes and the methods associated with both base classes Student, and Employee

4 Polymorphic Assignment TeachingAssistant * Mark = new TeachingAssistant; Employee * E = Mark; //Legal as a Teaching Assistant is-an Employee Student * S = Mark; //Legal as a Teaching Assistant is-a Student

5 Common Misuse of Multiple Inheritance A common error is to use Multiple Inheritance as composition rather than specialisation (is-a): E.G. The following is incorrect: class car: public Engine, public Transmission, public Wheels { … }

6 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 C++ compiler cannot determine which version to use in the TeachingAssistant class: the get_id() in the Employee class or the get_id () in the Student class.

7 class TeachingAssistant { }; TeachingAssistant * Mark; Mark->get_id(); /* Which class? */

8 Problems with Multiple Inheritance: Name Ambiguity Solution 1: Use a fully qualified function name: TeachingAssistant * Mark = new TeachingAssistant; cout << “ The TeachingAssistant is” << Mark -> Employee::get_id() << “\n”;

9 Problems with Multiple Inheritance: Name Ambiguity Solution 2: Redefine the ambiguous function in the new class and hide the qualified name in a method body: class TeachingAssistant:public Student, public Employee { public: string get_id(); public: string student_id(); }

10 string TeachingAssistant::get_id() { return Employee::get_id(); } string TeachingAssistant::student_id() { return Student::get_id(); }

11 Problems with Multiple Inheritance:  Replicated Base Classes: Base classes might be inherited more than once (indirectly) due to a class inheritance hierarchy causing duplicated attributes.  In our example suppose the following holds: class Employee: public Person {..} class Student: public Person {..} class TeachingAssistant:public Student, public Employee {..}

12 Problems with Multiple Inheritance: Replicated Base Classes: Attributes in the Person class get inherited twice! Eg. A teaching Assistant will have two names. 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 {..}

13 TeachingAssistant * Mark; = new TeachingAssistant; Student * s = Mark; // Legal due to is-a relationship Person * p = s; // Legal due to is-a relationship Employee * e = Mark; // Legal due to is-a relationship Person * p1 = e; // Legal due to is-a relationship Person * p2 = Mark; // Legal only if Virtual Inheritance is used so that the compiler knows which version of person to use, error otherwise


Download ppt "Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006."

Similar presentations


Ads by Google