Multiple inheritance

Graphical shapes are a typical example of objects allowing for a tree-shaped taxonomy. Sometimes, however, we wish to define a class not from a single base class, but by deriving it from multiple base classes, by employing multiple inheritance.
class student { ... };
  class assistant { ... };
  
  class student_assistant
  		: public student, public assistant {
  public:
  student_assistant( int id, int sal ) 
  		: student(id), assistant(sal) {}
  };
  

slide: Multiple inheritance

In slide 2-multi-1, one of the classical examples of multiple inheritance is depicted, defining a student_assistant by inheriting from student and assistant.

Dynamic binding for instances of a class derived by multiple inheritance works in the same way as in the case of single inheritance. However, ambiguities between member function names must be resolved by the programmer.


class person { };
  class student : virtual public person { ... }
  class assistant : virtual public person { ... }
  
  class student_assistant
  	: public student, public assistant { ... };
  

slide: Virtual base classes

When using multiple inheritance, one may encounter situations where the classes involved are derived from a common base class, as illustrated in slide 2-multi-2.

To ensure that {\em student_assistant} contains only one copy of the person class, both the student and assistant classes must indicate that the person is inherited in a virtual manner. Otherwise, we may not have a declaration of the form

  person* p = new student_assistant(20,6777,300);
  
since the compiler would not know which person was meant (that is, how to apply the conversion from {\em student_assistant} to person).