Hello friends, in this post today, we learn about Inheritance in c++ concept and types of inheritance in c++. Which is very important. so lets start...
Inheritance |
Inheritance
Class can acquire the properties and methods of another class.
Inheritance is the process of deriving a new class from an already existing class.
In other words" Inherit all the properties and methods of class into another class, is called Inheritance."
Inheritance provides the idea of reusability i.e., code once written can be used again and again in number of new classes.
The new class is called derived class and old class is called base class.
The new class can use all or some of the features of the already existing class and the programmer can define his own members to the new class.
Types of Inheritance
In c++, there are 5 types of inheritance.
Types of Inheritance |
Syntax of Inheritance:-
class derived_classname : access_spacifier base_classname
Where class is keyword used to create a class derived_classname of new derived class, access specifiers may be private, public, or protected. If access specifier is not present default mode is private. base_classname is the name of an already existing class. It may be a user defined or a built-in class.
a)-Single Inheritance (parent-child relationship)
In single level inheritance we have just one base class and one derived class.
Single level inheritance |
Here, class B is derived from class A.
b)-Multilevel Inheritance (Grandparent-parent-child relationship)
In multiple inheritances we have one base class and one derived at one level. At the next level the derived class becomes base class for the next class and so on.
Multilevel inheritance |
Here, class C is derived from class B and class B is derived from class A.
c)-Multiple Inheritance (More than one parents-one child relationship)
In multiple inheritance a child can have more than parent i.e., a child can inherit properties from more than one class.
Multiple inheritance |
Here, class C is derived from two classes, class A and class B.
d)-Hierarchical Inheritance (One parent-more than one child relationship)
In this type of inheritance multiple classes share the same base class. That is number of classes inherits the properties of one common base class. The derived classes again may become base class for other classes.
Hierarchical inheritance |
Here, class B, class C are derived from class A.
e)-Hybrid Inheritance(Mixture of inheritance)
It is a combination of any above inheritance types. That is either multiple or multilevel or hierarchical or any other combination.
Hybrid inheritance |
Here, class B and class C are derived from class A and class D is derived from class B and class C.
class A, class B and class C is example of Hierarchical Inheritance and class B, class C and class D is example of Multiple Inheritance so this hybrid inheritance is combination of Hierarchical and Multiple Inheritance.
Advantages of Inheritance:
Reusability of Code
Saves Time and Effort
Faster development, easier maintenance and easy to extend
Capable of expressing the inheritance relationship and its transitive nature which ensures closeness with real world problems
visibility mode in Inheritance
There are basically 3 different types of accessing standards/specifiers/derivation used by which the derived class object access the members of the base class.
A derived class inherits all base class methods with the following exceptions:
Constructors, destructors and copy constructors of the base class.
Overloaded operators of the base class.
The friend functions of the base class.
When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. We hardly use protected or private inheritance but public inheritance is commonly used. While using different type of inheritance, following rules are applied:
public derivation mode
private derivation mode
protected derivation mode
Access control of Inheritance |
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with something new information .
Thank you so much for reading. And take care about yourself and your family.
Comments
Post a Comment