Inheritance in C++
Inheritance is an important feature of C++. Basically, Inheritance is a process to transfer properties and behavior of one class (base/parent class) to another class(derived/child class). It is one of the mostly used feature that's plays an important role in object oriented programming language.
To understand inheritance more clearly, we can co-relate it with real world. As one object acquires all the properties and behavior of the parent object, inheritance also works on this factor. In such way, we can reuse, extends or modify the attributes and methods of any class.
Syntax of derived class
Some new terms are used here:
Base class: the class being inherited by another class.
Derived class: the class that inherits from another class.
Visibility mode: The visibility mode specifies whether the features of the base class are publicly inherited or privately inherited. It can be public or private.
Always remember that in C++, the default mode of visibility is private.
Types Of Inheritance
C++ supports five types of Inheritance:
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Now, we will discuss about these inheritance and its differences one by one with simple examples.
Single Inheritance
It is called single inheritance because we can derive only one new class that inherit the properties and behaviour of the base class. It means every parent class has single child class, which object can access the attributes and methods of the base class.
Roll_no. is 32
age is 20
your fee is 5034.50
Computer Science
Here, Info is a base class and Student is a child class. Public access specifier allow object S to access the method of base class.
Multiple Inheritance
Multiple inheritance can be defined as a class that acquires or inherits the properties and behaviour of the more than one class. It means a derived class has multiple base class.
Enter the value of a1 and a2:
10
20
Enter the value of b1 and b2:
30
40
Enter the value of c1 and c2:
50
60
value of a1 is :10 and a2 is :20
value of b1 is :30 and b2 is:40
value of c1 is :50 and c2 is:60
Multilevel Inheritance
In the Multilevel inheritance, a class derived from another derived class. If a class B is derived from class A and class C derived from class B, like the given figure, then it is called multilevel inheritance. You can also derive class C, and then derive its derived and so on.
We can implement above class hierarchy of multilevel inheritance using following syntax:
Syntax:
In multi-level inheritance a class derived from another derived class
Here we have given some example to understand the concept of Multilevel inheritance.
Enter the value of a:
5
Enter the value of b:
6
Enter the value of c:
8
The value of a is: 5
The value of b is: 6
The value of c is: 6
Addition of variables: 19
Hierarchical Inheritance
In the Hierarchical inheritance, more than one classes derived from only single class. If a class B and class C is derived from class A, like below figure, then it is called hierarchical inheritance. Because there are more than one derived classes of a single base class thus both classes inherits the features of class A.
We can implement above class hierarchical inheritance using following stntax:
Syntax:
In hierarchical inheritance more than on classes derived from only one base class.
Here, we have given some example to understand the concept of Hierarchical inheritance.
Enter the value of a1 and a2:
2
3
Enter the value of b1 and b2:
4
5
Enter the value of a1 and a2:
10
15
Enter the value c1 and c2:
6
7
The value of a1 is:2 and a2 is:3
value of b1 is:4 and b2 is:5
value of a1 is:10 and a2 is:15
value of c1 is:6 and c2 is:7