Open In App
Related Articles

Difference between Single and Multiple Inheritance in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Single Inheritance

Single inheritance is one in which the derived class inherits the single base class either public, private, or protected access specifier. In single inheritance, the derived class uses the features or members of the single base class. These base class members can be accessed by a derived class or child class according to the access specifier specified while inheriting the parent class or base class.

single inheritance in c++

 

Syntax

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

Example: Program to illustrate Single Inheritance

C++

// C++ program to illustrate 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 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 is 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 the derived class or child class according to the access specifier specified during inheriting the parent class or base class.

mulitple inheritance in c++

Syntax

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

Example: Program to illustrate Multiple Inheritance

In the below C++ program, A and B are the base classes and C is the derived class.

C++

// C++ program to illustrate Multiple Inheritance
  
#include <iostream>
using namespace std;
class A {
protected:
    int a;
  
public:
    void get_a(int n) { a = n; }
};
  
class B {
protected:
    int b;
  
public:
    void get_b(int n) { b = n; }
};
  
class C : public A, public B {
public:
    void display()
    {
        cout << "The value of a is : " << a << endl;
        cout << "The value of b is : " << b << endl;
        cout << "Product of a and b is : " << a * b;
    }
};
  
int main()
{
    C c;
    c.get_a(10);
    c.get_b(20);
    c.display();
  
    return 0;
}

                    

Output
The value of a is : 10
The value of b is : 20
Product of a and b is : 200

Difference between Single Inheritance and Multiple Inheritance

S.NOSingle InheritanceMultiple 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 a small run time as compared to multiple inheritance due to less overhead.While multiple inheritance requires more run time as compared to single inheritance due to more overhead.
4.Single inheritance is a lot close to specialization.In contrast, multiple inheritance is a lot close to generalization.
5.

Single inheritance is implemented as

Class DerivedClass_name : access_specifier Base_Class{};

.

Multiple inheritance is implemented as

Class DerivedClass_name : access_specifier Base_Class1, access_specifier Base_Class2, ….{};

.

6.Single inheritance is simple in comparison to multiple inheritance.While multiple inheritance is complex in comparison to 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).
8.Single inheritance constructs an inheritance tree.Multiple inheritance constructs Inheritance Directed Acyclic Graph (DAG).

Related Articles:



Last Updated : 07 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads