0% found this document useful (0 votes)
193 views

Types of Inheritance in C++

The document discusses different types of inheritance in C++ including single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance, hybrid inheritance, and multipath inheritance. Single inheritance allows a subclass to inherit from only one base class. Multiple inheritance allows a subclass to inherit from more than one base class. Multilevel inheritance involves deriving a subclass from another derived subclass. Hierarchical inheritance derives multiple subclasses from a single base class. Hybrid inheritance combines hierarchical and multiple inheritance. Multipath inheritance occurs when a subclass inherits from two base classes that share a common base class, which can cause ambiguities.

Uploaded by

Swati Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views

Types of Inheritance in C++

The document discusses different types of inheritance in C++ including single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance, hybrid inheritance, and multipath inheritance. Single inheritance allows a subclass to inherit from only one base class. Multiple inheritance allows a subclass to inherit from more than one base class. Multilevel inheritance involves deriving a subclass from another derived subclass. Hierarchical inheritance derives multiple subclasses from a single base class. Hybrid inheritance combines hierarchical and multiple inheritance. Multipath inheritance occurs when a subclass inherits from two base classes that share a common base class, which can cause ambiguities.

Uploaded by

Swati Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Types of Inheritance in C++

Objectives

In this ppt, you will:


1. Single Inheritance:
2. Multiple Inheritance:
3. Multilevel Inheritance:
4. Hierarchical Inheritance:
5. Hybrid (Virtual) Inheritance:
6. A special case of hybrid inheritance : Multipath
inheritance:

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:

class subclass_name : access_mode


base_class
{
//body of subclass
};

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:

class subclass_name : access_mode base_class1, access_mode


base_class2, ....
{
//body of subclass
};

7
// C++ program to explain multiple inheritance

#include <iostream>
using namespace std;

// first base class


class Vehicle {
public: // main function
Vehicle() int main()
{ {
cout << "This is a Vehicle" << endl; // creating object of sub class
} will
}; // invoke the constructor of base
classes
// second base class Car obj;
class FourWheeler { return 0;
public: }
FourWheeler()
{ Output
cout << "This is a 4 wheeler Vehicle" << endl; This is a Vehicle
This is a 4 wheeler
} Vehicle
8
};
3. Multilevel Inheritance: I
In this type of inheritance, a derived class is created
from another derived class.

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 :

// first sub class


// C++ program to implement Hierarchical Inheritance class Car: public Vehicle
#include <iostream> {
using namespace std;
};
// base class
class Vehicle // second sub class
{ class Bus: public Vehicle
public: {
Vehicle()
{ };
cout << "This is a Vehicle" << endl;
} // main function
}; int main()
{
// creating object of sub class will
// invoke the constructor of base
class
Car obj1;
Bus obj2;
return 0;}
5. Hybrid (Virtual) Inheritance:
Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance.

Below image shows the combination of hierarchical and multiple


inheritance:
Example :

// C++ program for Hybrid Inheritance


// first sub class
#include <iostream> class Car: public Vehicle
using namespace std; {

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

An ambiguity can arise in this type of 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;

// obj.a = 10; //Statement 1, Error


// obj.a = 100; //Statement 2, Error

obj.ClassB::a = 10; // Statement 3


obj.ClassC::a = 100; // Statement 4

21
obj.b = 20;
obj.c = 30;
obj.d = 40;

cout << "\n A from ClassB : " << obj.ClassB::a;


cout << "\n A from ClassC : " << obj.ClassC::a;

cout << "\n B : " << obj.b;


cout << "\n C : " << obj.c;
cout << "\n D : " << obj.d;
}
Output:
A from ClassB : 10
A from ClassC : 100
B : 20
C : 30
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.

If we need to access the data member a of ClassA through the object of


ClassD, we must specify the path from which a will be accessed, whether
it is from ClassB or ClassC, because compiler

Can’t differentiate between two copies of ClassA in ClassD.

24
There are 2 ways to avoid this ambiguity:

Avoiding ambiguity using scope resolution operator:

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.

obj.ClassB::a = 10; //Statement 3


obj.ClassC::a = 100; //Statement 4

Note : Still, there are two copies of ClassA in ClassD.


Avoiding ambiguity using virtual base class:
#include<iostream.h>
#include<conio.h>
void main()
class ClassA {
{
public: ClassD obj;
int a;
}; obj.a = 10; //Statement
3
class ClassB : virtual public ClassA obj.a = 100; //Statement
{ 4
public:
int b; obj.b = 20;
}; obj.c = 30;
class ClassC : virtual public ClassA obj.d = 40;
{
public: cout<< "\n A : "<< obj.a;
int c; cout<< "\n B : "<< obj.b;
}; cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;
class ClassD : public ClassB, public ClassC
{ }
public:
int d;
};
Output:

A : 100
B : 20
C : 30
D : 40

27
Thank you

You might also like