Hierarchical Inheritance in C++ Programming


Inheritance is a basic object oriented feature in which one class acquires and inherit the properties of another class. All the properties of the Base Class ( also known as the Parent Class or Super class ) are present in the Derived Class ( also known as the Child Class or Sub class ). Hierarchical Inheritance is one base class and more then derived class.

Example :
      Rectangle, Circle, Square are derived from Shape base class.

In this case, the base class is shape, and it has three derived classes: rectangle, circle, and square. Each of these derived classes has its own specific properties and methods, but they all share the common attributes of shape, such as length, breadth, and radius.

  • The program begins with the shape class, which has three public attributes: length, breadth, and radius. These attributes will be inherited by the derived classes.
  • The first derived class is rectangle, which is declared using the keyword class rectangle:public shape. This means that rectangle inherits from the shape class.
  • The rectangle class has two methods: getRectangleDetails() and rectangle_area().getRectangleDetails() prompts the user to enter the length and breadth of the rectangle, and then stores those values in the length and breadth attributes. rectangle_area() calculates and returns the area of the rectangle using the formula length * breadth.
  • The second derived class is circle, which is declared using the keyword class circle:public shape. This means that circle also inherits from the shape class.
  • The circle class has two methods: getCircleDetails() and circle_area().getCircleDetails() prompts the user to enter the radius of the circle, and then stores that value in the radius attribute. circle_area() calculates and returns the area of the circle using the formula 3.14 * (radius * radius) .
  • The third derived class is square, which is declared using the keyword class square:public shape. This means that square also inherits from the shape class.
  • The square class has two methods: getSquareDetails() and square_area().getSquareDetails() prompts the user to enter the length of one side of the square, and then stores that value in the length attribute. square_area() calculates and returns the area of the square using the formula length * length.
  • In the main() function, objects of each of the three derived classes are created: rectangle r, circle c, and square s.
  • The user is prompted to enter the necessary details for each shape using the appropriate methods for each object: r.getRectangleDetails(),c.getCircleDetails(), and s.getSquareDetails().
  • The program then calculates and displays the area of each shape using the appropriate methods for each object: r.rectangle_area(),c.circle_area(), and s.square_area().

Source Code

#include<iostream>
using namespace std;
//Hierarchical Inheritance in C++ Programming
 
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: 12.4
Enter Breadth: 23.1
Area of Rectangle   : 286.44
Enter Radius: 14
Area of Circle   : 615.44
Enter Side: 3
Area of Square   : 9
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples