HIERARCHICAL INHERITANCE IN C++
Introduction:
Inheritance is a concept closely aligned with reality, in the same way that a son inherits his father's qualities (behavior and traits) and the father inherits his grandfather's qualities. According to programming conventions, inheritance happens when a class takes on the characteristics of a base class.
Hierarchical Inheritance in C++:
Inheritance, a component of object-oriented programming, allows a derived class (child class) to inherit the attributes, functions, and data members of the Basic class (parent class). For instance, a child receives characteristics from both of their parents.
Multiple subclasses inherit only a single parent class property using hierarchical inheritance. There are several derived classes, along with one basic class. The derived classes are also inherited by a number of additional classes. As a result, hierarchical structures resemble trees. Mango and apple share a similar trait in that they are both fruits and possess the same qualities. Mango as well as apple are subclasses of fruit, which will serve as the Parent class.
Class A represents the parent class, B is a subclass that inherits from class A, whereas C is also a subclass that also inherits from class A, as seen in the figure below.
![HIERARCHICAL INHERITANCE IN C++](https://static.tutorialandexample.com/cpp/hierarchical-inheritance-in-cpp1.png)
Similar to this, a hierarchy as well as an architecture like a tree can arise if a further subclass inherits properties from the B class, and so on. The figure below illustrates this.
![HIERARCHICAL INHERITANCE IN C++](https://static.tutorialandexample.com/cpp/hierarchical-inheritance-in-cpp2.png)
In this instance, the subclasses O and P inherit properties from class B, while the subclasses Q and R inherit properties from class C. O and P belong to parent class B, while Q and R belong to parent class C. The class declaration's visibility mode is provided by access_specifier. The members of the parent class are passed down to the derived class through inheritance from the base class.
Syntax:
Class Parent_class {
// Declarations statements;
};
Class Derived_class_A: public Parent_class {
// Declarations statements;
};
Class Derived_class_B: public Parent_class {
// Declarations statements;
};
class Newly_derived_class_A: public Derived_class_A {
// Declarations statements;
};
class Newly_derived_class_B: public Derived_class_B {
// Declarations statements;
};
Example 1:
# include<iostream>
using namespace std;
class Parent_class_A
{
public:
void show_parent_class_A () {
cout << " Parent class A " << endl;
}
};
class class_B : public Parent_class_A
{
public:
void show_class_B () {
cout << " Sub class B " << endl;
}
};
class class_C : public Parent_class_A
{
public:
void show_class_C () {
cout << " Sub class C " << endl;
}
};
int main () {
class_B b;
cout << " Inheriting from class B : " << endl;
b.show_class_B();
b.show_parent_class_A();
class_C c;
cout << " Inheriting from class C : " << endl;
c.show_class_C ();
c.show_parent_class_A ();
return 0;
}
Output:
![HIERARCHICAL INHERITANCE IN C++](https://static.tutorialandexample.com/cpp/hierarchical-inheritance-in-cpp3.png)
Explanation: The superclass A in the code shown above is also referred to as the parent class, while the subclasses B as well as C are referred to as the child classes. Class A transmits properties to classes B and C.
Example 2:
# include <iostream>
using namespace std;
class parent_class_A {
public:
int a, b;
void data () {
cout << " Enter the value of a and b : \n ";
cin >> a >> b;
}
};
class class_B : public parent_class_A {
public:
void multiplication () {
cout << " \n Multiplication of a and b is : " << a * b << endl;
}
};
class class_C : public parent_class_A {
public:
void addition () {
cout << " \n Addition of a and b is : \n " << a + b;
}
};
int main ()
{
class_B obj1;
class_C obj2;
obj1.data ();
obj1.multiplication ();
obj2.data ();
obj2.addition ();
return 0;
}
Output:
![HIERARCHICAL INHERITANCE IN C++](https://static.tutorialandexample.com/cpp/hierarchical-inheritance-in-cpp4.png)
Explanation: Our basic class is called class A, and it consists of the two data components, a and b. Additionally, it uses the function data () to input the numerical values of the data components. Class B uses the inherited data elements a and b to execute the product after inheriting parent class A. Additionally, Class C uses the inherited data elements to perform the multiplication of a and b and initiates the Base data () method.
Conclusion:
In contrast with other inheritance features, hierarchical inheritance can manage and share properties among the parent class as well as the base class. Depending on the features required for inheritance, these properties may be common or uncommon.