Lecture-9-inheritance-types-cslearnerr.com_-1
Lecture-9-inheritance-types-cslearnerr.com_-1
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
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