multiple inheritance mark hennessy dept. computer science nui maynooth c++ workshop 18 th – 22 nd...
TRANSCRIPT
Multiple Inheritance
Mark Hennessy
Dept. Computer Science
NUI Maynooth
C++ Workshop
18th – 22nd September 2006
class Student
{
private:
std::string id;
public:
std::string get_id();
};
class Employee
{
private:
std::string id;
public:
std::string get_id();
};
Example:Syntaxclass TeachingAssistant:public Student, public Employee
{ …
}
Teaching Assistant maintains the attributes and the methods associated with both base classes Student, and Employee
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
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
{ …}
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.
class TeachingAssistant
{
};
TeachingAssistant * Mark;
Mark->get_id();
/* Which class? */
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”;
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();
}
string TeachingAssistant::get_id(){
return Employee::get_id();}
string TeachingAssistant::student_id(){
return Student::get_id();}
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 {..}
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 {..}
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