Cap202 - A23 - Jameel Ahmad
Cap202 - A23 - Jameel Ahmad
Cap202 - A23 - Jameel Ahmad
CAP202
Topic: Multiple level inheritance, Hybrid inheritance, Virtual Base class
Submitted by:
JAMEEL AHMAD
Registration no.: 11813086
In partial fulfillment for the requirements of the award of the degree of
New classes can be built from the existing classes. It means that we can add
additional features to an existing class without modifying it. The new class is
referred as derived class or subclass and the original class is known as base classes
or super class.
Types of Inheritance in C++:
➢ Single inheritance
➢ Multilevel inheritance
➢ Multiple inheritance
➢ Hierarchical inheritance
➢ Hybrid inheritance
Multiple inheritance:
A class can inherit properties from more than one class which is known as multiple
inheritances.
Multiple inheritances allow us to combine the features of several existing classes
as a starting point for defining new classes. It is like a child inheriting the physical
features of one parent and the intelligent if another.
This form of inheritance can have several super classes. A class can inherit the
attributes of two or more classes as shown below diagram.
Example of Multiple Inheritance:
Output:
In the above Program we can see there are three classes. Two classes are derived
from the 3rd one. This is called multiple inheritance.
Hybrid inheritance:
Hybrid inheritance is a combination of multiple inheritance and multilevel
inheritance.
There could be situations where we need to apply two or more types of inheritance
to design one inheritance called hybrid inheritance.
If we talk about the flowchart, class A is a parent class for class B and C, whereas
Class B and C are the parent class of D which is the only child class of B and C.
Example of Hybrid Inheritance:
Output:
As shown in program 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.
Conclusion for inheritance:
The mechanism of deriving a new class from an old class is called inheritance, it
provides the concept of Reusability that is the most important concept in C++. All
types of inheritance with its own features and its use to provide users to reusability
concepts strongly, to give save time and reduce the complexity.
Virtual Base Class:
When two or more objects are derived from a common base class, we can prevent
multiple copies of the base class being present in an object derived from those
objects by declaring the base class as virtual when it is being inherited. Such a base
class is known as virtual base class. This can be achieved by preceding the base class
name with the word virtual.
Virtual base classes are used in virtual inheritance in a way of preventing multiple
“instances” of a given class appearing in an inheritance hierarchy when using
multiple inheritances.
As we can see from the figure that data members/function of class A are inherited
twice to class D. One through class B and second through class C. When any data /
function member of class A is accessed by an object of class D, ambiguity arises as
to which data/function member would be called? One inherited through B or the
other inherited through C.
Example for Virtual Base class:
Output:
The class A has just one data member a which is public. This class is virtually
inherited in class B and class C. Now class B and class C becomes virtual base class
and no duplication of data member a is done.