Inheritance Hi, in this blog we are discussing inheritance in the C++ programming language. In this we will see what is inheritance and what are the different types of inheritance and implementation of each inheritance concept and then we will see about diamond shape problem. Diamond shape problems will come in multiple inheritance so let’s start with the concept. Inheritance concepts come in C++ and Java. But here I am taking the inheritance for C++. How can one class inherit the other class by using the scope resolution operator followed by the parent class name. Here is the syntax for inheriting the class.
For Free Demo classes Call: 8237077325
Registration Link:Click Here!
Syntax: Class baseclass_name : access_specifier superclass_name This the general syntax of inheriting parent class by the base class access specifier are the access permission to the members of class and class members. There are different types of inheritance. Let’s see the different types of inheritance.
- Single Inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
Here will see all the inheritance one by one –
For Free Demo classes Call: 8237077325
Registration Link:Click Here!
1.Single Inheritance: There will be only one parent class and one child class where the child will be inheriting the methods from the parent class. Technically the parent class is called a base class or super class whereas a child class is called a derived class. Here we will see how we can represent it in pictorial form.
Here in the single inheritance we have two classes which are parent class and child class and child class inheriting the properties from the parent class. We will see how that can be possible for a child class to inherit the properties of the parent class by creating objects. We know to access the members of the particular class we need to create objects of that respective class. So here if one class is inheriting the members of other class then by creating the object of base class we can access the member of the parent class there is no need to create the object for the parent class if that is inheriting by the child class then create the object of that child class and access the members of the parent class.This was about the single inheritance.
2.Multiple Inheritance: Single class will inherit the methods from two parent classes. Below is the representation of the inheritance pictorial. Here we are looking into multiple inheritance. One class inheriting more than one class is called multiple inheritance here how this works. Here in this multiple inheritance single class is inheriting the properties (members) from two classes we can see in the picture. By creating the object of the child class we can access the members of both the parent class. But the problem with this multiple inheritance is the Diamond shape problem.
What is the Diamond shape problem?
This is a problem with accessing the members of both classes. If both classes have the same named members that time system gets confused about which member should be accessed. Example: Class A, class B, class C. Class C inherits both the class A and B now let’s have the member called a in both the class so when we are accessing the member a from Class C the system get confused about which class member should be accessed. This ambiguity is called the diamond shape problem because multiple inheritance is not preferred. 1.Multilevel Inheritance: The Grandchild class will inherit the parent class and also the grandparent class. Find the representation below. In this inheritance the base class inherits the members of parent class and also super parent class. This means by creating the object of base class we can access the members of parent class and also super parent class hence there is no need to create the object for all classes to access the members. How this works will see…! Here we have three class called class A, class B and class C, here class B is inheriting the class A, whereas class C is inheriting the class B now class C is indirectly inheriting the class A so by creating the object to only class C we can inherit class A and class B members this is about multilevel inheritance.
For Free Demo classes Call: 8237077325
Registration Link:Click Here!
So we saw about inheritance and its drawbacks in this blog. I hope everyone is clear with the inheritance and its implementation.
Author:-
Chetna Malagi
Call the Trainer and Book your free demo Class
Call now!!! | SevenMentor Pvt Ltd.
© Copyright 2021 | Sevenmentor Pvt Ltd.