Area of Rectangle and Triangle
In this, we are going to see how to calculate Area of Rectangle and Triangle using Hierarchical Inheritance in C++ Programming Language.
 


The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include<conio.h>

// defining the class Shape to demonstrate the concept of Hierarchial Inheritence in CPP
class Shape
{
    protected:
    float width, height;
    // public members are accessible everywhere
    public:
    void setDimensions(float w, float h)
    {
        width = w;
        height = h;
    }
};

// Class Rectangle inherites the Shape class
class Rectangle : public Shape
{
    // Method Overriding
    public:
    float area()
    {
        return (width * height);
    }
};

// Class Triangle inherites the Shape class
class Triangle : public Shape
{
    // Method Overriding
    public:
    float area()
    {
        return (width * height / 2);
    }
};

// Defining the main method to access the members of the class
void main()
{
    clrscr();
    int rHeight,width,tHeight,base;
    cout << "Enter the Height and Width for Rectangle: \n";
    cin >>rHeight>>width;
    cout << "Enter the Base and Height for Triangle: \n";
    cin >>base>>tHeight;

    // Declaring the Class objects to access the class members
    Rectangle rectangle;
    Triangle triangle;

    rectangle.setDimensions(rHeight, width);
    triangle.setDimensions(base, tHeight);

    cout << "\nArea of the Rectangle computed using Rectangle Class is : " << rectangle.area() << "\n";
    cout << "Area of the Triangle computed using Triangle Class is: " << triangle.area() << endl;

    getch();
}

//.............Coded by RISHAB NAIR

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

// defining the class Shape to demonstrate the concept of Hierarchial Inheritence in CPP
class Shape
{
    protected:
    float width, height;
    // public members are accessible everywhere
    public:
    void setDimensions(float w, float h)
    {
        width = w;
        height = h;
    }
};

// Class Rectangle inherites the Shape class
class Rectangle : public Shape
{
    // Method Overriding
    public:
    float area()
    {
        return (width * height);
    }
};

// Class Triangle inherites the Shape class
class Triangle : public Shape
{
    // Method Overriding
    public:
    float area()
    {
        return (width * height / 2);
    }
};

// Defining the main method to access the members of the class
int main()
{
    int rHeight,width,tHeight,base;
    cout << "Enter the Height and Width for Rectangle: \n";
    cin >>rHeight>>width;
    cout << "Enter the Base and Height for Triangle: \n";
    cin >>base>>tHeight;

    // Declaring the Class objects to access the class members
    Rectangle rectangle;
    Triangle triangle;

    rectangle.setDimensions(rHeight, width);
    triangle.setDimensions(base, tHeight);

    cout << "\nArea of the Rectangle computed using Rectangle Class is : " << rectangle.area() << "\n";
    cout << "Area of the Triangle computed using Triangle Class is: " << triangle.area() << endl;

    return 0;
}

//.............Coded by RISHAB NAIR

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post