Updated March 23, 2023
Introduction to Hierarchical Inheritance in C++
In real life, inheritance is a way of passing or possessing the characteristics or features of legacy to the new. In technical terms of C++ and the object-oriented concept, it is a way of passing the features from parent class to base, child or subclass. The class whose properties are extracted i.e. the features are inherited are known as Parent class or superclass. Hierarchical inheritance is a kind of inheritance where more than one class is inherited from a single parent or base class. Especially those features which are common in the parent class is also common with the base class.
Syntax:
class Parent_classname
{
Common_properties;
methods;
};
class derived_class1:visibility_mode parent_classname
{
Common_properties;
methods;
};
class derived_class2:visibility_mode parent_classname
{
Common_ properties;
methods;
};
.
.
.
.
class derived_classN:visibility_mode parent_classname
{
Common_properties;
methods;
};
According to the syntax, all the common features in the parent class get extracted or inherited by the child class and the methods in the child class vice-versa also happens. Therefore, it can be concluded that n-number of child class or base class can inherit the properties of parent class and the vice-versa can also happen. Also, it is not necessary that only common features can be inherited. Any other feature can also be inherited.
Real-life examples:
- Programming languages are derived from languages.
- Smart tv, LED TV all these Tv series are derived from normal youtube tv sets.
How Hierarchical Inheritance works in C++?
Hierarchical Inheritance is a part of the inheritance and has its own feature which is somewhat designed in a way that classes are inheriting properties from parent and base class or child class also inherits some common properties from the parent class. When many classes try to get the properties or features from the parent class then hierarchical inheritance automatically comes as a savior.
Examples of Hierarchical Inheritance in C++
Given below are the examples of Hierarchical Inheritance in c++:
Example #1
Code:
#include <iostream>
using namespace std;
class X
{
public:
int a, b;
void getdata ()
{
cout << "\nEnter value of a and b:\n"; cin >> a >> b;
}
};
class Y : public X
{
public:
void product()
{
cout << "\nProduct= " << a * b;
}
};
class Z: public X {
public:
void sum()
{
cout << "\nSum= " << a + b;
}
};
int main()
{
Y obj1;
Z obj2;
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}
Output:
Explanation: From the above program and output we can infer how actually hierarchical inheritance works in terms of C++.
Class X is the single base or parent class that has its own properties as well as some common properties as the base class and methods as well. Therefore, the base class will surpass its properties to the child class. Class Y is the subclass which in turn will inherit the properties from parent class X, class Z will also work in a similar fashion. Derived classes Y and Z will have object creation which will inherit and hold the properties from the parent class which is class X objects of their respective classes will behave with the defined methods and variables. Output shows the product and sum of the values being given as an input.
Example #2
Code:
#include <iostream>
using namespace std;
class Side
{
protected:
int l;
public:
void set_values (int x)
{
l=x;
}
};
class Square: public Side
{
public:
int sq()
{
return (l *l);
}
};
class Cube:public Side
{
public:
int cub()
{
return (l *l*l);
}
};
int main ()
{
Square s;
s.set_values (10);
cout << "The square value is::" << s.sq() << endl;
Cube c;
c.set_values (20);
cout << "The cube value is::" << c.cub () << endl;
return 0;
}
Output:
Explanation:
In Example 2 side is the parent class which is common for all the sub or child classes both the triangle class and cube class has side as its properties where the method of triangle gets its computation of getting the length using sides and same is the case with cube side is used to compute the computation of value of the cube with the help of child class which will have its side derived from the parent class.
Example #3
Code:
#include <iostream>
using namespace std;
class Sprts
{
public:
Sprts ()
{
cout << "This is a Sport" << endl;
}
};
class football: public Sprts
{
};
class cricket: public Sprts
{
};
int main ()
{
football obj1;
cricket obj2;
return 0;
}
Output:
Explanation:
In this example first a subclass is created then another subclass is created and simultaneously constructors are passed in the main class which in turn will invoke and pass the property to the constructor of the base class.
Conclusion
Unlike other inheritance properties, hierarchical inheritance has the capacity to handle and share the properties between the base class and the parent class that can be either common property or uncommon property depends on the feature which is needed for inheritance.
Recommended Articles
This is a guide to Hierarchical Inheritance in C++. Here we discuss the introduction, how hierarchical inheritance works in C++?. You may also have a look at the following articles to learn more –