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

#include<iostream>
using namespace std;
class Shape
{
  public:
    int a;
    int b;
    void get_data(int n,int m)
    {
      a=n;
      b=m;
    }
};
class Rectangle : public Shape
{
  public:
    int rect_area()
    {
      int result = a*b;
      return result;
    }
};
class Triangle : public Shape
{
  public:
    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;
  cin>>length>>breadth;
  r.get_data(length,breadth);
  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;
  cin>>base>>height;
  t.get_data(base,height);
  float n = t.triangle_area();
  std::cout<<"Area of the triangle is : "<<n<<std::endl;
  return 0;
}
To download raw file Click Here

Output

Enter the length and breadth of a rectangle: 
20
30
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


Functions


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