Studytonight is now part of the GUVI universe. Explore GUVI →
🤩 New Cool Developer Tools for you. Explore →
FREE JavaScript Video Series Start Learning →
Signup/Sign In
Tests
MCQs to test your knowledge.
Compilers
Compilers to execute code in browser.
Index
LAST UPDATED: OCTOBER 31, 2020

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,
 breadth;
 //Default Constructor of the Rectangle Class
 public: Rectangle(): length(0.0),
 breadth(0.0) {}
 void getDimensions() {
 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);
 }
};
//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:

C++ single level inheritence

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 : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.

Learn to Code
Learn and practice coding side-by-side.
NEW
C language Course
115+ coding exercises
Javascript Course
85+ coding exercises

AltStyle によって変換されたページ (->オリジナル) /