Difference between Single and Multiple Inheritance in C++
Last Updated :
07 Aug, 2023
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.
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;
}
OutputSalary: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.
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;
}
OutputThe 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
|
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. |
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. |
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. |
Single inheritance is a lot close to specialization. | In contrast, multiple inheritance is a lot close to generalization. |
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, ....{};
.
|
Single inheritance is simple in comparison to multiple inheritance. | While multiple inheritance is complex in comparison to single inheritance. |
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). |
Single inheritance constructs an inheritance tree. | Multiple inheritance constructs Inheritance Directed Acyclic Graph (DAG). |
Related Articles:
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Difference Between IPv4 and IPv6 IPv4 and IPv6 are two versions of the system that gives devices a unique address on the internet, known as the Internet Protocol (IP). IP is like a set of rules that helps devices send and receive data online. Since the internet is made up of billions of connected devices, each one needs its own spe
7 min read
Difference between BFS and DFS Breadth-First Search (BFS) and Depth-First Search (DFS) are two fundamental algorithms used for traversing or searching graphs and trees. This article covers the basic difference between Breadth-First Search and Depth-First Search.Difference between BFS and DFSParametersBFSDFSStands forBFS stands fo
2 min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read