Difference between Single and Multiple Inheritance in C++

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.