The idea of inheritance closely mirrors real-life scenarios. Similar to how a son acquires the traits (attributes and actions) of his father, and the father, in turn, inherits the attributes of the son's grandfather. Within programming conventions, inheritance takes place when a class inherits the attributes of another class (base).
Now, we will delve into the concept of hierarchical inheritance.
Definition
As its name suggests, class hierarchy involves a single base class and multiple derived classes. Additionally, these derived classes are further inherited by other classes, resulting in a tree-like hierarchy structure.
Here, the base class is referred to as class A, while class B and class C serve as its derived classes.
Class D and Class E inherit from class B, while Class F and Class G inherit from class C, establishing a hierarchical inheritance structure.
Where hierarchical inheritance does is used?
It is employed in scenarios where maintaining a hierarchy is necessary. For example, an organization's database is structured hierarchically. Various departments within an organization, like IT, computer science, Civil, Mechanical, etc., share common attributes such as student name, roll number, year, etc., encapsulated within a class called Student. Consequently, all departments inherit the properties of a student, adhering to the hierarchical inheritance structure.
Syntax
Class Parent
{
statement(s);
};
Class Derived1: public Parent
{
statement(s);
};
Class Derived2: public Parent
{
statement(s);
};
class newderived1: public Derived1
{
statement(s);
};
class newderived2: public Derived2
{
statement(s);
};
Class Parent serves as the foundational class, with Derived1 and Derived2 inheriting from it. Additionally, newderived1 extends Derived1, while newderived2 extends Derived2. The inheritance chain can continue with an arbitrary number of base classes inherited by numerous derived classes.
Code example
#include <iostream>
using namespace std;
class A // Base class
{
public:
int x, y; // data members
voidgetdata() // to input x and y
{
cout<< "Enter value of x and y:\n";
cin>> x >> y;
}
};
class B : public A //B is derived from class base
{
public:
void product()
{
cout<< "\nProduct= " << x * y <<endl; // Perform product
}
};
class C : public A //C is also derived from class base
{
public:
void sum()
{
cout<< "\nSum= " << x + y; // Perform sum
}
};
int main()
{
B obj1; //object of derived class B
C obj2; //object of derived class C
obj1.getdata(); // input x and y
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}
Output
Explanation
We utilize class A as our foundational class containing two attributes, x and y. Within class A, the function getdata is utilized to input values for these attributes. Class B inherits from class A and carries out calculations involving the inherited attributes x and y.
Class C additionally invokes the getdata function from the base class and calculates the product of x and y utilizing the inherited data attributes.