Find Area of Rectangle using Hierarchical Inheritance in C++

Example Program:

Here is the simple program for printing the area of rectangle using hierarchical inheritance in C++.

Source Code

using namespace std;
class Shape
    int a;
    int b;
    void get_data(int n,int m)
class Rectangle : public Shape
    int rect_area()
      int result = a*b;
      return result;
class Triangle : public Shape
    int triangle_area()
      float result = 0.5*a*b;
      return result;
int main()
  Rectangle r;
  Triangle t;
  int length,breadth,base,height;
  std::cout<<"Enter the length and breadth of a rectangle: "<<std::endl;
  int m = r.rect_area();
  std::cout<<"Area of the rectangle is : "<<m<<std::endl;
  std::cout<<"Enter the base and height of the triangle: "<<std::endl;
  float n = t.triangle_area();
  std::cout<<"Area of the triangle is : "<<n<<std::endl;
  return 0;
To download raw file Click Here


Enter the length and breadth of a rectangle: 
Area of the rectangle is : 600
Enter the base and height of the triangle: 

Basic Programs

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


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

Learn All in Tamil © Designed & Developed By Tutor Joes