Types of Inheritance in C++
Types of Inheritance in C++
Objectives
2
1. Single Inheritance:
In single inheritance, a class is allowed to inherit
from only one class. i.e. one sub class is inherited
by one base class only.
3
Syntax:
4
Example :
// C++ program to explain // sub class derived from a single base
// Single inheritance classes
#include <iostream> class Car: public Vehicle{
using namespace std;
};
// base class
class Vehicle {
// main function
public:
int main()
Vehicle()
{ {
cout << "This is a Vehicle" << endl; // creating object of sub class will
} // invoke the constructor of base classes
}; Car obj;
return 0;
Output
}
This is a Vehicle
5
2. Multiple Inheritance:
Multiple Inheritance is a feature of C++ where a class
can inherit from more than one classes. i.e one sub
class is inherited from more than one base classes.
6
Syntax:
7
// C++ program to explain multiple inheritance
#include <iostream>
using namespace std;
9
// C++ program to implement Multilevel Inheritance // first sub_class derived from class
#include <iostream> vehicle
using namespace std; class fourWheeler: public Vehicle
{ public:
// base class fourWheeler()
class Vehicle {
{ cout<<"Objects with 4 wheels are
public: vehicles"<<endl;
Vehicle() }
{ };
cout << "This is a Vehicle" << endl; // sub class derived from the derived
} base class fourWheeler
}; // main function class Car: public fourWheeler{
int main() public:
{ Car()
//creating object of sub class will {
//invoke the constructor of base cout<<"Car has 4 Wheels"<<endl;
classes }
Car obj; };
return 0;
} 10
Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
4. Hierarchical Inheritance:
In this type of inheritance, more than one sub class is
inherited from a single base class. i.e. more than one
derived class is created from a single base class.
Example :
// base class };
class Vehicle
{ // second sub class
public: class Bus: public Vehicle, public Fare
Vehicle() {
{
cout << "This is a Vehicle" << endl; };
}
}; // main function
int main()
//base class {
class Fare // creating object of sub class will
{ // invoke the constructor of base class
public: Bus obj2;
Fare() return 0;
{ }
cout<<"Fare of Vehicle\n";
}
};
Output
This is a Vehicle
Fare of Vehicle
16
6. A special case of hybrid
inheritance : Multipath inheritance:
A derived class with two base classes and these two base classes have one
common base class is called multipath inheritance.
17
Example
// C++ program demonstrating ambiguity in Multipath
// Inheritance
#include <conio.h>
#include <iostream.h>
class ClassA {
public:
int a;
};
18
class ClassB : public ClassA {
public:
int b;
};
class ClassC : public ClassA {
public:
int c;
};
19
class ClassD : public ClassB, public ClassC {
public:
int d;
};
20
void main()
{
ClassD obj;
21
obj.b = 20;
obj.c = 30;
obj.d = 40;
23
In the above example, both ClassB & ClassC inherit ClassA, they both
have single copy of ClassA.
However ClassD inherit both ClassB & ClassC, therefore ClassD have
two copies of ClassA, one from ClassB and another from ClassC.
24
There are 2 ways to avoid this ambiguity:
Using scope resolution operator we can manually specify the path from which data
member a will be accessed, as shown in statement 3 and 4, in the above example.
A : 100
B : 20
C : 30
D : 40
27
Thank you