/* */ Click to Join Live Class with Shankar sir Call 9798158723

What is Hierarchical Inheritance ?


Hierarchical Inheritance is a type of inheritance in which a derived class (subclass) is created from more than one base class (superclass).if a single class serves as base class for more than one derived class, it results in hierarchical inheritance.


syntax :

class Parent
{
...;
};
class Child1: Access Parent
{
----;
//child 1 derived from the parent
};
class Child2: Access Parent
{
----;
//child 2 also derived from the same parent class
};


Q1.) Example of Hierarchical Inheritance .
    #include< iostream>
using namespace std;

 
class shape
{
public:
    float length,breadth,radius;
};
class rectangle:public shape
{
    public:
    void getRectangleDetails()
    {
        cout<<"Enter Length: ";
        cin>>length;
        cout<<"Enter Breadth: ";
        cin>>breadth;
    }
    float rectangle_area()
    {
        return length*breadth;
    }
};
 
class circle:public shape
{
    public:
    void getCircleDetails()
    {
        cout<<"Enter Radius: ";
        cin>>radius;
    }
    double circle_area()
    {
        return 3.14*(radius*radius);
    }
};
 
class square:public shape
{
 
public:
    void getSquareDetails()
    {
        cout<<"Enter Side: ";
        cin>>length;
    }
     double square_area()
    {
        return length*length;
    }
};
int main()
{
    rectangle r;
    circle c;
    square s;
    r.getRectangleDetails();
    cout<<"Area of Rectangle   : " << r.rectangle_area()<< endl;
    c.getCircleDetails ();
    cout<<"Area of Circle   : " << c.circle_area()<< endl;
    s.getSquareDetails ();
    cout<<"Area of Square   : " << s.square_area()<< endl;
    return 0;
}
Output
Enter Length: 10
Enter Breadth: 10
Area of Rectangle : 100
Enter Radius: 8
Area of Circle : 201.142857
Enter Side: 23
Area of Square : 529