C++ Mutliple Inheritance

Sometimes a class could be derived from more than one base class, and it is called multiple inheritance.  For example, a class HomeOffice may be derived from both classes of Home and Work.  The following diagram corresponds to the class hierarchy:

Part of the declarations and definitions of the classes are as follows:

/*****************************************************/
/*   declaration of the base class Home                                           */
/*****************************************************/
class Home
{
   protected:
     char* homePhone;
     char* homeAddress;

   public:
     Home(int hp, char* ha);
     ~Home();
     void print();
};

/*****************************************************/
/*   declaration of the base class Work                                            */
/*****************************************************/
class Work
{
   protected:
     char* workPhone;
     char* workFax;
     char* workAddress;

   public:
     Work(int wp, int wf, char* wa);
     ~Work();
     void print();
};

/******************************************************/
/*  declaration of the derived class HomeOffice                               */
/******************************************************/
class HomeOffice : public Home, public Work
{
   public:
     HomeOffice(const char* const hp, const char* const ha,
                          const char* const wp, const char* const wf,
                          const char* const wa);
     void print();
};

/******************************************************/
/* implementation of the constructor of derived class HomeOffice */
/******************************************************/
HomeOffice::HomeOffice(const char* const hp, const char* const ha,
                                           const char* const wp, const char* const wf,
                                           const char* const wa)
       : Home(hp, ha), Office(wp, wf, wa)
{}
 

Improper use of multiple inheritance might introduce ambiguities. 

what properties class D actually inherits from its superclasses B and C.

3 Possibilities:

Soltion 1: Some existing programming languages solve this special inheritance graph by deriving D with the properties of A plus the properties of B and C without the properties they have inherited from A. Consequently, D cannot introduce naming conflicts with names of class A.

Soltion 2:However, if B and C add properties with the same name, D runs into a naming conflict.

Soltion 3:Another possible solution is, that D inherits from both inheritance paths. In this solution, D owns two copies of the properties of A: one is inherited by B and one by C.

 

 

 

© Lynne Grewe