Multiple Inheritance in C++

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Reading time: 25 minutes

Inheritance is the capability of one class to inherit properties from another class. It is a splendid concept of object oriented languages.
Major reasons behind the introduction of inheritance:

  1. capability to express the inheriatnce relationship
  2. idea of reusability
  3. transitive nature of inheritance

A new class can be derived from an existing class. This new derived class is called derived class or subclass and the existing class is called base class or super class.

Types of inheritance:

1. Single inheritance

When a subclass inherits only from one base class, it is known as single inheritance.

2. Multiple inheritance

When a subclass inherits from multiple base classes, it is known as multiple inheritance.

3. Hierarchial inheritance

When many sub classes inherit from a single base class,it is known as hierarchial inheritance.

4. Multilevel inheritance

The transitive nature of inheritance is reflected by this form of inheritance.When a subclass inherits from a class that itself inherits from another class, it is known as multilevel inheritance.

5. Hybrid inheritance

It combines two or more forms of inheritance e.g.,when a sub class inherits from multiple base classes and all of its base class inherit from a single base class, this form of inheritance is known as hybrid inheritance.

Multiple Inheritance

A sub class inheriting from multiple base classes is known as multiple inheritance.

Syntax:

class derived_class_name:vis_mode base1,vis_mode base2,...
{   //members of derived class
};

where vis_mode is the visibility mode and base1,base2, base3 etc. are names of multiple base classes.

Example:

class sub:public SuperA,private SuperB
{   //members 
};

Here, Sub is inheriting from class SuperA publicly and also from class SuperB privately. That is, inheritable members(protected, public) of SuperA will retain their access specifiers i.e., protected remains protected, public remains public in Sub and inheritable members of SuperB will become private in Sub.

Access specifiers control access of members within a class whereas visibility modes control access of inherited members within the subclass.

Example:

class Student
{
    public:
    Student()
    {
        cout<<"My name is Angelina.";
    }
};

class Sports
{
    protected:
    Sports()
    {
        cout<<"I love playing basketball.";
    }
};

class statement: public Student,public Sports
{
    void display()
    { 
        cout<<"Hello";
    }
};

Access Specifiers

Visibility Modes

The visibility mode (private or public or protected) in the definition of the derived class specifies whether the features of the base class are privately derived or publicly derived or protected derived. The visibility modes basically control the access-specifier to be for inheritable members of base-class, in the derived class.

  • The public visibility mode

The public members of the base class become the public members of the derived class, and the protected members of the base class become the protected members of the derived class. Private members of the base class can't be accessed by the derived class.

  • The private visibility mode

The public and protected members of the base class become the private members of the derived class.

  • The protected visibility mode

The public and protected members of the base class become protected members of the derived class.

The Dreaded Diamond

It refers to a class structure in which a particular class appears more than once in a class's inheritance hierarchy. To prevent two copies of the same class being included in the derived class we declare the base class as virtual when it is inherited. This is accomplished by putting the keyword virtual before the base class's name when it is inherited.

Example:

#include<iostream.h> 
#include<stdlib.h>
class Base{  public:
              int a;
          };
class D1 : virtual public Base   //D1 inherits Base as virtual
{  public:
     int b;
};

class D2 : virtual public Base
{  public:
     int c;
};
class D3 : public D1, public D2    //only one copy of base
{  public:
    int total;
};
int main()
{ D3 ob;
  ob.a = 25;                    //unambiguous
  ob.b = 50;
  ob.c = 75;
  ob.total=ob.a+ob.b+ob.c;     //unambiguous
  cout<<ob.total<<endl;
  return 0;
 }

The only difference between normal base class and a virtual base class is when an object inherits the base more than once.If virtual base classes are used , then only one base class is present in the object. Otherwise multiple copies will be found.

Question

The problem of the dreaded diamond is solved by:

virtual base classes
scope operator
can't be solved
none
When two or more objects are derived from a common base class, you can prevent multiple copies of base class from being present in an object derived from those objects by declaring the base class as virtual when it is inherited.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.