C++ Inheritance
Inheritance
During programming we come across certain situations where we are required to make two different classes, majority of class members are same and only a few are different from each other or one of the two classes has a few extra members. Suppose you own a mobile company and you wish to release a new model with a few new features in this situation instead of building a design from scratch you simply add a couple of new features to the existing model. This is made possible using inheritance.
Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In C++, the class which inherits the members of another class is called the derived or child class and the class whose members are inherited is called the base or parent class.
Syntax for Inheritance in classes
Where,
derived_className: It is the name of the derived class.
visibility mode: The visibility mode specifies whether the properties of the base class are publicly inherited or privately inherited. It can be public or private.
base_className: It is the name of the base class.
Let us talk about the different types of inheritance in C++.
Single Inheritance
In single inheritance, a class inherits features from only one class. Single inheritance is like asexual reproduction in plants. In asexual reproduction, part of the parent plant is used to generate a new plant. So in single inheritance there is only one base class and one derived class
Example to demonstrate single inheritance.
Multiple Inheritance
Multiple Inheritance in C++ allows a class to inherit from more than one class. Basically, one sub-class is inherited from more than one base class. Think of this as bisexual reproduction in animals where there are at least two or more parents and a single offspring. The offspring is the derived class whereas the parents are the base classes.
Syntax:
Note: The number of base classes will be separated by a comma (‘, ‘) and access mode for every base class must be specified.
Demonstration of Multiple inheritance
Multi-level Inheritance
In this type of inheritance, a derived class is created from another derived class.
Let's say your grandfather gave a watch to your father and your father gave this same watch to you after a couple of years and you pass it on to your children. This is an example of multi-level inheritance
Demonstration of Multilevel Inheritance
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.
Demonstration of hierarchical inheritance.
Hybrid Inheritance
Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.
Demonstration for Hybrid Inheritance
A special case in hybrid inheritance is Multipath inheritance
In the given diagram class 6 is derived from class 3 and class 2 which share the same parent class 1.
A derived class with two base classes and these two base classes have one common base class is called multipath inheritance. Ambiguity can arise in this type of inheritance.
Demonstration of hybrid inheritance
In the above example, both Class B & Class C inherit Class A, they both have single copy of Class A. However Class D inherit both Class B & Class C, therefore Class D have two copies of Class A, one from Class B and another from Class C.
If we need to access the data member an of Class A through the object of Class D, we must specify the path from which a will be accessed, whether it is from Class B or Class C, because the compiler can’t differentiate between two copies of Class A in Class D.
We can avoid this 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 statements below.