Skip to content
Related Articles

Related Articles

Difference between Single and Multiple Inheritance in C++
  • Last Updated : 16 Oct, 2019

Single Inheritance:
Single inheritance is one in which the derived class inherits the single base class either publicly, privately or protectedly. In single inheritance, the derived class uses the features or members of the single base class. These base class members can be accessed by derived class or child class according to the access specifier specified during inheriting the parent class or base class.

Syntax:

Class DerivedClass_name : access_specifier Base_Class
{
    //Class's Body 
}; 

Example of Single Inheritance:




#include<iostream>
using namespace std;
class A {
public:
    int k = 1000;
    float salary = 80000;
};
class B : public A {
public:
    float bonus = 8000;
    void ts()
    {
        cout<<"Total salary.."<<(salary + bonus)<<endl;
    }
};
int main()
{
    B b1;
    cout<<"Salary:"<<b1.salary<<endl;
    cout<<"Bonus:"<<b1.bonus<<endl;
    b1.ts();
    return 0;
}


Output:



Salary: 80000
Bonus: 8000
total salary..88000 

In the this example, A is the base class or parent class and B is the derived class or child class.

Multiple Inheritance:
Multiple inheritance is one in which the derived class acquires two or more base classes. In multiple inheritance, the derived class are allowed to use the joint features of the inherited base classes. Every base class is inherited by the derived class by notifying the separate access specifier for each of them. The base class members can be accessed by derived class or child class according to the access specifier specified during inheriting the parent class or base class.

Syntax:

Class DerivedClass_name : access_specifier Base_Class1, access_specifier Base_Class2
{
    //Class's Body 
}; 

Example of Multiple Inheritance:




#include<iostream>
using namespace std;
class A {
public:
    float salary = 80000;
};
  
class B {
public:
    float bonus = 8000;
};
  
class C : public A, public B {
public:
    void ts()
    {
        cout<<"Total salary.."<<(salary + bonus)<<endl;
    }
};
int main()
{
    C b1;
    b1.ts();
    return 0;
}


Output:

Total salary..88000 

In the this example, A and B are the base classes and C is the derived class.

Difference between Single and multiple Inheritance:

S.NO Single inheritance Multiple inheritance
1. Single inheritance is one in which the derived class inherits the single base class. Whereas multiple inheritance is one in which the derived class acquires two or more base classes.
2. In single inheritance, the derived class uses the features of the single base class. While in multiple inheritance, the derived class uses the joint features of the inherited base classes.
3. Single inheritance requires small run time as compared to multiple inheritance due to less overhead. While multiple inheritance requires more run time time as compared to single inheritance due to more overhead.
4. Single inheritance is a lot of close to specialization. In contrast, multiple inheritance is a lot of close to generalization.
5. Single inheritance is implemented as Class DerivedClass_name : access_specifier Base_Class{};. While multiple inheritance is implemented as Class DerivedClass_name : access_specifier Base_Class1, access_specifier Base_Class2, ….{}; .
6. Single inheritance is simple in comparison to the multiple inheritance. While multiple inheritance is complex in comparison to the single inheritance.
7. Single inheritance can be implemented in any programming language. C++ supports multiple inheritance but multiple inheritance can’t be implemented in any programming language(C#, Java doesn’t support multiple inheritance).

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :