Dark Mode On/Off

# C++ Single Level Inheritance Program

Hello Everyone!

In this tutorial, we will learn how to demonstrate the concept of Single Level Inheritance, in the C++ programming language.

To understand the concept of Inheritance and various Access modifiers in CPP, we will recommend you to visit here: C++ Inheritance, where we have explained it from scratch.

Code:

``````#include <iostream>

using namespace std;

//Class Shape to compute the Area and Perimeter of the Shape it derives
class Shape {
public:
float area(float l, float b) {
return (l * b);
}

public:
float perimeter(float l, float b) {
return (2 * (l + b));
}
};

//Rectangle class inherites or is derived from the parent class: Shape.
class Rectangle: private Shape {
private: float length,

//Default Constructor of the Rectangle Class
public: Rectangle(): length(0.0),

void getDimensions() {
cout << "\nEnter the length of the Rectangle: ";
cin >> length;

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

//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.
}

//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.
}
};

//Defining the main method to access the members of the class
int main() {

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

//Declaring the Class objects to access the class members
Rectangle rect;
cout << "\nClass Rectangle inherites the Shape Class or Rectangle class is derieved from the Shape class.\n\n";

cout << "\nCalling the getDimensions() method from the main() method:\n\n";

rect.getDimensions();
cout << "\n\n";

cout << "\nPerimeter of the Rectangle computed using the parent Class Shape is : " << rect.perimeter() << "\n\n\n";
cout << "Area of the Rectangle computed using the parent Class Shape is: " << rect.area();

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

return 0;
}``````

Output: We hope that this post helped you develop a better understanding of the concept of Inheritance and Access Modifiers in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )