× back Inheritance Why and when to use inheritance? Syntax Modes of inheritance
Next Topic → ← Previous Topic

Inheritance

Why and when to use inheritance?

Syntax

                
class <derived_class_name> : <access-specifier>
<base_class_name> 
{
    //body
}
                
            

Here

Modes of inheritance

                
// C++ Implementation to show that a derived class doesn't inherit access to private data members.
// However, it does inherit a full parent object.

class A 
{
    public:
    int x;

    protected:
    int y;

    private:
    int z;
};

class B : public A 
{
    // x is public 
    // y is protected 
    // z is not accessible from B
};

class C : protected A 
{
    // x is protected 
    // y is protected 
    // z is not accessible from C
};

class D : private A  // 'private' is default for classes
{
    // x is private 
    // y is private 
    // z is not accessible from D
};
                
            

The below table summarizes the above three modes and shows the access specifier of the members of the base class in the subclass when derived in public, protected and private modes:

Types of Inheritance

  1. Single inheritance
  2. Multilevel inheritance
  3. Multiple inheritance
  4. Hierarchical inhertance
  5. Hybird inheritance

Single Inheritance

  • In single inhertance, a class is allowed to inherit from only one class. i.e. one subclass is inherited by one base class only.

Example code

                       
#include<iostream>
using namespace std;
class Beverage
{
    public : 
    void drinks()
    {
        cout << "All drinks are available" << endl;
    }
};

class Coffee : public Beverage
{
    public:
    void mocha()
    {
        cout << "Mocha tastes the best";
    }
};

int main()
{
    Coffee c;
    c.drinks();
    c.mocha(); // using sub class we are accessing member function of base class.
    return 0;
}
                       
                   

Multilevel Inheritance

  • In this type of inheritance, a derived class is created from another derived class.

Example code ↓

                       
#include <<ostream>
using namespace std;
class A
{
public:
    int x;
    A()
    {
        x = 10;
    }
};
class B : public A
{
public:
    int y;
    B()
    {
        y = 20;
    }
};
class C : public B
{
public:
    int sum;
    C()
    {
        sum = x + y;
    }
};
int main()
{
    C c_one;
    cout << "The sum is = " << c_one.sum; // by creating derived class object the constructor of parent class are called.
    return 0;
}
                       
                   

Multiple inheritance

  • Multiple inheritance is a feature of C++ where a class can inherit from more than one class. i.e. one subclass is inherited from more than one base class.
                       
#include <iostream>
using namespace std;
class A
{
public:
    int x;
    A()
    {
        x = 10;
    }
};
class B
{
public:
    int y;
    B()
    {
        y = 20;
    }
};
class C : public A, public B
{
public:
    int sum;
    C()
    {
        sum = x + y;
    }
};
int main()
{
    C c_one;
    cout << "The sum is = " << c_one.sum; 
    return 0;
}
                       
                   

Hierarchical inheritance

  • In this type of inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class.
                       
#include <iostream>
using namespace std;
class A
{
public:
    int x, y, z;
    A()
    {
        x = 10;
        y = 10;
        z = 10;
    }
};
class B : public A
{
public:
    int sum;
    B()
    {
        sum = x + y + z;
    }
};
class C : public A
{
public:
    int mult;
    C()
    {
        mult = x * y * z;
    }
};
int main()
{
    B b_one;
    C c_one;
    cout << "The sum is = " << b_one.sum << endl;
    cout << "The multiplication is = " << c_one.mult;
    return 0;
}
                       
                   

Hybrid inheritance

  • Hybrid inheritance is implemented by combining more than one type ofinheritance. For example: Combining Hierarchical inheritance and multiple inheritance.
                       
#include <iostream>
using namespace std;
class A
{
public:
    int x, y;
    A()
    {
        x = 10;
        y = 10;
    }
};
class B : public A
{
public:
    int sum;
    B()
    {
        sum = x + y;
    }
};
class C
{
public:
    int z;
    C()
    {
        z = 20;
    }
};
class D : public B, public C
{
public:
    int total_sum;
    D()
    {
        total_sum = sum + z;
    }
};
int main()
{
    D d_one;
    cout << "The total sum is = " << d_one.total_sum << endl;
    return 0;
}
                       
                   

Multipath inheritance

  • A derived class with two base classes and these two base classes have one common base class is called multipath inheritance. Ambiguity can arise in this type of inhertiance.
  • Abmiguity: A situation or statement that is unclear because it can be understood in more than one way

There are two ways to avoid this ambiguity:

  1. Avoiding ambiguity using the scope resolution operator: Using the scope resolution operator we can manually specify the path from which data member will be accessed. See the example below ↓
                       
// C++ program demonstrating ambiguity in Multipath Inheritance
  
#include <iostream>
using namespace std;
  
class ClassA {
public:
    int a;
};
  
class ClassB : public ClassA {
public:
    int b;
};
  
class ClassC : public ClassA {
public:
    int c;
};
  
class ClassD : public ClassB, public ClassC {
public:
    int d;
};
  
int main()
{
    ClassD obj;
  
    // obj.a = 10;                  // Statement 1, Error
    // obj.a = 100;                 // Statement 2, Error
  
    obj.ClassB::a = 10; // Statement 3
    obj.ClassC::a = 100; // Statement 4
  
    obj.b = 20;
    obj.c = 30;
    obj.d = 40;
  
    cout << " a from ClassB  : " << obj.ClassB::a;
    cout << "\n a from ClassC  : " << obj.ClassC::a;
  
    cout << "\n b : " << obj.b;
    cout << "\n c : " << obj.c;
    cout << "\n d : " << obj.d << '\n';
}
                       
                   

Output ↓

                    
obj.ClassB::a = 10;
abj.ClassC:: a = 100;
                    
                
  • In the above example, both ClassB and ClassC inherit ClassA, they both have a single copy of ClassA. However Class-D inherits both ClassB and ClassC, therefore Class-D has two copies of ClassA, one from ClassB and another from ClassC.
  • If we need to access the data member of ClassA through the object of Class-D, we must specify the path from which a will be accessed, whether it is from ClassB or ClassC, because compiler can’t differentiate between two copies of ClassA in Class-D.
  • Note: Still, there are two copies of ClassA in ClassD.
  1. Avoiding ambiguity using the virtual base class
  • A virtual base class in C++ is a base class that is used to avoid the problem of multiple inheritance ambiguity. It is a base class that is declared as virtual in the inheritance hierarchy to ensure that only one instance of the shared base class is inherited by the derived classes.
                       
#include<iostream>
  
class ClassA
{
  public:
    int a;
};
  
class ClassB : virtual public ClassA
{
  public:
    int b;
};
  
class ClassC : virtual public ClassA
{
  public:
    int c;
};
  
class ClassD : public ClassB, public ClassC
{
  public:
    int d;
};
  
int main()
{
    ClassD obj;
  
    obj.a = 10;       // Statement 3
    obj.a = 100;      // Statement 4
  
    obj.b = 20;
    obj.c = 30;
    obj.d = 40;
  
    cout << "\n a : " << obj.a;
    cout << "\n b : " << obj.b;
    cout << "\n c : " << obj.c;
    cout << "\n d : " << obj.d << '\n';
}
                       
                   

Output ↓

                    
a : 100
b : 20
c : 30
d : 40
                    
                

Reference ↓