C++ Hierarchical Inheritance Program

C++C++Beginner
Practice Now

Introduction

In this lab, we will demonstrate how to create a C++ program that showcases the concept of Hierarchical Inheritance. We will write a simple program using classes and objects to understand the topic better.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp(("`C++`")) -.-> cpp/OOPGroup(["`OOP`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") cpp/OOPGroup -.-> cpp/classes_objects("`Classes/Objects`") cpp/OOPGroup -.-> cpp/class_methods("`Class Methods`") cpp/OOPGroup -.-> cpp/access_specifiers("`Access Specifiers`") cpp/OOPGroup -.-> cpp/encapsulation("`Encapsulation`") cpp/OOPGroup -.-> cpp/inheritance("`Inheritance`") subgraph Lab Skills cpp/comments -.-> lab-96145{{"`C++ Hierarchical Inheritance Program`"}} cpp/variables -.-> lab-96145{{"`C++ Hierarchical Inheritance Program`"}} cpp/data_types -.-> lab-96145{{"`C++ Hierarchical Inheritance Program`"}} cpp/operators -.-> lab-96145{{"`C++ Hierarchical Inheritance Program`"}} cpp/function_parameters -.-> lab-96145{{"`C++ Hierarchical Inheritance Program`"}} cpp/classes_objects -.-> lab-96145{{"`C++ Hierarchical Inheritance Program`"}} cpp/class_methods -.-> lab-96145{{"`C++ Hierarchical Inheritance Program`"}} cpp/access_specifiers -.-> lab-96145{{"`C++ Hierarchical Inheritance Program`"}} cpp/encapsulation -.-> lab-96145{{"`C++ Hierarchical Inheritance Program`"}} cpp/inheritance -.-> lab-96145{{"`C++ Hierarchical Inheritance Program`"}} end

Create the main.cpp

We will create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

Write the Shape class

The first thing we need to do is define the Shape class, which will serve as the parent class for both the Rectangle and Triangle classes. In this class, we will create two protected member variables width and height that will store the width and height of the shape.

Then, we will create a public member function setDimensions which will set these dimensions and print a message on the console.

This is the code block for Shape class that should be added to main.cpp:

#include <iostream>
using namespace std;

class Shape {
    //protected member variables are only accessible within the class and its descendant classes
    protected:
        float width, height;
    //public members are accessible everywhere
    public:
        void setDimensions(float w, float h) {
            cout << "Setting the Dimensions using the parent Class: Shape\n";
            cout << "The dimensions are: " << w << " and " << h << "\n\n";

            width = w;
            height = h;
        }
};

Write the Rectangle class

Next, we will create the Rectangle class which will inherit from the Shape class. Here, we will use a method overriding technique to calculate the area of the rectangle using the area() function.

This is the code block for the Rectangle class that should be added to main.cpp:

class Rectangle: public Shape {
    //Method Overriding
    public:
        float area() {
            return (width * height);
        }
};

Write the Triangle class

Now, letโ€™s create the Triangle class which will also inherit from the Shape class. Here, we will use method overriding to calculate the area of the triangle using the area() function.

This is the code block for the Triangle class that should be added to main.cpp:

class Triangle: public Shape {
    //Method Overriding
    public:
        float area() {
            return (width * height / 2);
        }
};

Write the main() function

Now itโ€™s time to write the main() function. Here, we will create objects for both the Rectangle and Triangle classes and set their dimensions.

We will then use the area() function to calculate the area of both the Rectangle and Triangle using their respective objects.

This is the code block for the main() function that should be added to main.cpp:

int main() {

    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << "===== Program to demonstrate the concept of Hierarchical Inheritance in CPP =====\n\n";

    //Declaring the Class objects to access the class members
    Rectangle rectangle;
    Triangle triangle;

    rectangle.setDimensions(5, 3);
    triangle.setDimensions(2, 5);

    cout << "\nArea of the Rectangle computed using Rectangle Class is : " << rectangle.area() << "\n\n\n";
    cout << "Area of the Triangle computed using Triangle Class is: " << triangle.area();

    cout << "\n\n\n";

    return 0;
}

Compile and Run the Program

To execute the program in the terminal, first, navigate to ~/project. Then type the following command to compile the main.cpp file:

g++ main.cpp -o main

After that, execute the program with the following command:

./main

You should see the output on the terminal.

Summary

In this lab, we have learned how to demonstrate the concept of Hierarchical Inheritance in CPP. We created the Shape class, which was the parent class of both Rectangle and Triangle. We then created objects for each of the child classes and used method overriding to find the area of a rectangle and a triangle.

Hopefully, this lab helped you develop a better understanding of how Hierarchical Inheritance works in C++. For any query, feel free to reach out to us via the comments section down below.

Other C++ Tutorials you may like