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:
CPP
#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.
C++
// Example: #include<iostream> using namespace std; class A { protected : int a; public : void set_A() { cout<< "Enter the Value of A=" ; cin>>a; } void disp_A() { cout<<endl<< "Value of A=" <<a; } }; class B: public A { int b,p; public : void set_B() { set_A(); cout<< "Enter the Value of B=" ; cin>>b; } void disp_B() { disp_A(); cout<<endl<< "Value of B=" <<b; } void cal_product() { p=a*b; cout<<endl<< "Product of " <<a<< " * " <<b<< " = " <<p; } }; main() { B _b; _b.set_B(); _b.cal_product(); return 0; } |
Output
Enter the Value of A= 5 5 Enter the Value of B= 9 9 Product of 5 * 9 = 45
C++
// Example: #include<iostream> using namespace std; class A { protected : int a; public : void set_A( int x) { a=x; } void disp_A() { cout<<endl<< "Value of A=" <<a; } }; class B: public A { int b,p; public : void set_B( int x, int y) { set_A(x); b=y; } void disp_B() { disp_A(); cout<<endl<< "Value of B=" <<b; } void cal_product() { p=a*b; cout<<endl<< "Product of " <<a<< " * " <<b<< " = " <<p; } }; main() { B _b; _b.set_B(4,5); _b.cal_product(); return 0; } |
Product of 4 * 5 = 20
Syntax:
Class DerivedClass_name : access_specifier Base_Class1, access_specifier Base_Class2 { //Class's Body };
Example of Multiple Inheritance:
CPP
#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.
C++
// Example: #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; } |
The value of a is : 10 The value of b is : 20 Product of a and b is : 200
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). |
8. | Single inheritance constructs inheritance tree. | Multiple inheritance constructs Inheritance Directed Acyclic Graph (DAG). |