multiple inheritance mark hennessy dept. computer science nui maynooth c++ workshop 18 th – 22 nd...

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

Upload: phoebe-scott

Post on 02-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

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

Multiple Inheritance

Mark Hennessy

Dept. Computer Science

NUI Maynooth

C++ Workshop

18th – 22nd September 2006

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

class Student

{

private:

std::string id;

public:

std::string get_id();

};

class Employee

{

private:

std::string id;

public:

std::string get_id();

};

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

Example:Syntaxclass TeachingAssistant:public Student, public Employee

{ …

}

Teaching Assistant maintains the attributes and the methods associated with both base classes Student, and Employee

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

Polymorphic AssignmentTeachingAssistant * 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

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

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

{ …}

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

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.

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

class TeachingAssistant

{

};

TeachingAssistant * Mark;

Mark->get_id();

/* Which class? */

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

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”;

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

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();

}

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

string TeachingAssistant::get_id(){

return Employee::get_id();}

string TeachingAssistant::student_id(){

return Student::get_id();}

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

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 {..}

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

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 {..}

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

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