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

Lecture-9-inheritance-types-cslearnerr.com_-1

The document explains different types of inheritance in C++, including multi-level, multiple, hierarchical, and hybrid inheritance. Multi-level inheritance involves a class derived from another derived class, while multiple inheritance allows a class to inherit from multiple classes. Hierarchical inheritance has multiple subclasses inheriting from a single base class, and hybrid inheritance combines different types of inheritance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture-9-inheritance-types-cslearnerr.com_-1

The document explains different types of inheritance in C++, including multi-level, multiple, hierarchical, and hybrid inheritance. Multi-level inheritance involves a class derived from another derived class, while multiple inheritance allows a class to inherit from multiple classes. Hierarchical inheritance has multiple subclasses inheriting from a single base class, and hybrid inheritance combines different types of inheritance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Inheritance

MS KANWAL LODHI

cslearnerr.com
Multi-Level Inheritance
cslearnerr.com
Multilevel Inheritance in C++ is the process of deriving a class from another derived class. When
one class inherits another class it is further inherited by another class. It is known as multi-level
inheritance.
For example, if we take Grandfather as a base class then Father is the derived class that has
features of Grandfather and then Child is the also derived class that is derived from the sub-class
Father which inherits all the features of Father.

cslearnerr.com
Multi-Level Inheritance

cslearnerr.com
Example : Multi-Level code

cslearnerr.com
Output
cslearnerr.com
Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more than one
classes. The constructors of inherited classes are called in the same order in which they are
inherited. For example, in the following program, B’s constructor is called before A’s constructor.
A class can be derived from more than one base class.
Eg:
(i) A CHILD class is derived from FATHER and MOTHER class
(ii) A PETROL class is derived from LIQUID and FUEL class.

cslearnerr.com
Syntax :

cslearnerr.com
Example:

cslearnerr.com
Hierarchical Inheritance

In Hierarchical inheritance, more than one sub-class inherits the property of a single base class.
There is one base class and multiple derived classes.
Several other classes inherit the derived classes as well.
Hierarchical structures thus form a tree-like structure.
It is similar to that, mango and apple both are fruits; both inherit the property of fruit.
Fruit will be the Base class, and mango and apple are sub-classes.
Hierarchical Inheritance
cslearnerr.com

The below diagram shows, Class A is a Base


class, B is a subclass inherited from class A,
and C is a subclass it also inherits from class A.
Similarly, if another subclass inherits property
from B class and so on then there will be a
hierarchy, and a tree-like structure is formed.
cslearnerr.com

Example code
Example
cslearnerr.com
cslearnerr.com
Hybrid Inheritance
The inheritance in which the derivation of a class involves more than one form of any
inheritance is called hybrid inheritance.
Basically C++ hybrid inheritance is combination of two or more types of inheritance.
It can also be called multi path inheritance.
The hybrid is combination of single inheritance and multiple inheritance. Hybrid inheritance is
used in a situation where we need to apply more than one inheritance in a program.

cslearnerr.com
cslearnerr.com
Hybrid Inheritance

cslearnerr.com As shown in block diagram class B is derived from class A which


is single inheritance and then Class D is inherited from B and
class C which is multiple inheritance. So single inheritance and
multiple inheritance jointly results in hybrid inheritance.

You might also like