C++ Single Level Inheritance

Beginner

Introduction

In this lab, you will learn how to demonstrate the concept of Single Level Inheritance in the C++ programming language. You will create two classes, a parent class and a child class, where the child class (derived class) inherits the characteristics (properties and methods) of the parent class (base or super class).

Create the main.cpp file

First, create a main.cpp file in the ~/project directory using the following command:

touch ~/project/main.cpp

Write the code

Copy and paste the following code into your main.cpp file:

#include <iostream>
// Including the required libraries

using namespace std;

// Declaring a parent class (base class) called "Shape"
class Shape {
    public:
        float area(float l, float b) { // Method to calculate the Area of the Shape
            return (l * b);
        }

        float perimeter(float l, float b) { // Method to calculate the Perimeter of the Shape
            return (2 * (l + b));
        }
};

// Declaring a child class (derived class) called "Rectangle" which derives or inherits the Shape class
class Rectangle: private Shape {
    private:
        float length,
        breadth;

    public:
        Rectangle(): length(0.0), breadth(0.0) {} // Default constructor of the Rectangle

        void getDimensions() { // Method to get the dimensions of the Rectangle from the user
            cout << "\nEnter the length of the Rectangle: ";
            cin >> length;

            cout << "\nEnter the breadth of the Rectangle: ";
            cin >> breadth;
        }

        // Method to Calculate the perimeter of the Rectangle by using the Shape Class
        float perimeter() {
            // Calls the perimeter() method of class Shape and returns it.
            return Shape::perimeter(length, breadth);
        }

        // Method to Calculate the area of the Rectangle by using the Shape Class
        float area() {
            // Calls the area() method of class Shape and returns it.
            return Shape::area(length, breadth);
        }
};

int main() {
    // Creating an object of the Rectangle class
    Rectangle rect;

    // Welcoming message
    cout << "\n\nWelcome to Single Level Inheritance Program!\n";

    // Message to the user
    cout << "\nRectangle class is derived from the Shape class.\n\n";

    // Getting dimensions of the Rectangle from the user
    rect.getDimensions();

    // Calculating the Perimeter of the Rectangle using the parent Class Shape and displaying the output
    cout << "\nPerimeter of the Rectangle computed using the parent Class Shape: " << rect.perimeter() << "\n";

    // Calculating the Area of the Rectangle using the parent Class Shape and displaying the output
    cout << "Area of the Rectangle computed using the parent Class Shape: " << rect.area() << "\n\n";

    return 0;
}

Compile and Run the code

Next, compile the code using the following command:

g++ main.cpp -o main && ./main

Then, run the code using the following command:

./main

Understand the output

After running the program, you will see the following output:

Welcome to Single Level Inheritance Program!

Rectangle class is derived from the Shape class.

Enter the length of the Rectangle: 5

Enter the breadth of the Rectangle: 10

Perimeter of the Rectangle computed using the parent Class Shape: 30
Area of the Rectangle computed using the parent Class Shape: 50

The program prompts the user to enter the length and breadth of the rectangle. After getting the dimensions, it calculates the perimeter and area using the perimeter() and area() functions from the parent Shape class.

Summary

Congratulations! You have successfully written a C++ program demonstrating the concept of single level inheritance. You can experiment with different shapes and the output will accurately reflect the calculations for any new derived classes.

Other Tutorials you may like