Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. (50 marks) Using C++, write a pro gram to calculate the area and perimeter of

ID: 3890633 • Letter: 1

Question

1. (50 marks) Using C++, write a pro gram to calculate the area and perimeter of four simple plane figures: a rectangle, a circle, a square and a triangle. 1 of 3 The program should begin by explaining itself to the user. It must then prompt the user for appropriate input (i.e: ask the user to select a rectangle, circle, square or triangle and then ask for necessary information based on the user's selection). Next, the program must calculate and output the perimeter and area ofthe appropriate figure and then ask the user if he or she would like to find the perimeter and area of another figure. We will assume all plane figures are measured in centimeters. The program MUST take advantage of the concepts of Inheritance and Polymorphism. A base class must be defined (you can call it whatever you like, but better has a meaningful name, suchas "PlaneFigure" might be a good choice). Other classes (for example, “Rectangle", "Circle", "Square" and "Triangle") must be derived from the "P laneFigure” base class. Also, two methods must be defined for the "PlaneFigure" class: "perimeter" and "area". These methods are to be overridden for each derived class Your program should be well documented and easy to read.

Explanation / Answer

Concepts of Inheritance and Polymorphism - Inheritance basically means that one class can inherit (literally!) variable and functions of another class. This ensures a relation between them. For given question, class PlaneFigure can be understood as a more generic class. Every plane figure can be an object of this class and each object will have its own area and perimeter. The sub-classes (rectangle, circle, square, triangle) define the specific types of plane figures and they inherit the functions area() and perimeter() from super class 'PlaneFigure' but define them uniquely by overriding them (meaning defining an already defined function).

Polymorphism is used here when you call a function which is declared in multiple classes but is uniquely defined for each. For example, all the 4 sub classes have area() function, but all have it defined uniquely (for circle it returns pi*r^2 and for square it returns 4*side. This is the ability of C++ to understand that each object has different view of area although it is named the same for all. Same with function perimeter. So when I call area from a square object it acts differently than when called from circle object.

Here is the C++ code that achieves the required task:

#include<iostream>
#include<math.h>    // To use sqrt() function

using namespace std;

// Constant PI declared to be used later
const float PI =3.1415926535898;

class PlaneFigure{
    /************************************************
    * The base class PlaneFigure.
    * Members :
    * area()     : returns area of plane figure
    * perimeter(): returns perimeter of plane figure
    *************************************************/

public:
    float area();
    float perimeter();
};

class Rectangle: public PlaneFigure{
    /*****************************************************
    * Class Rectangle derives from base class PlaneFigure.
    * The constructor takes 2 sides as parameters and
    * creates the rectangle object with given sides
    * Members :
    * length     : length of rectangle object
    * breadth    : breadth of rectangle object
    * area()     : returns area of rectangle object
    * perimeter(): returns perimeter of rectangle object
    ******************************************************/
    float length;
    float breadth;
    public:
    Rectangle(float l, float b)
    {
        length = l;
        breadth = b;
    }
    float area()
    {
        return length*breadth;
    }
    float perimeter()
    {
        return 2*(length+breadth);
    }
};

class Circle: public PlaneFigure{
    /*****************************************************
    * Class Circle derives from base class PlaneFigure.
    * The constructor takes 1 radius as parameters and
    * creates the circle object with given radius
    * Members :
    * radius     : radius of circle object
    * area()     : returns area of circle object
    * perimeter(): returns perimeter of circle object
    ******************************************************/
    float radius;
public:
    Circle(float r)
    {
        radius = r;
    }
    float area()
    {
        return PI*radius*radius;
    }
    float perimeter()
    {
        return 2*PI*radius;
    }
};

class Square: public PlaneFigure{
    /*****************************************************
    * Class Square derives from base class PlaneFigure.
    * The constructor takes 1 side as parameter and
    * creates the square object with given side
    * Members :
    * side       : side of circle object
    * area()     : returns area of square object
    * perimeter(): returns perimeter of square object
    ******************************************************/
    float side;
public:
    Square(float s)
    {
        side = s;
    }
    float area()
    {
        return side*side;
    }
    float perimeter()
    {
        return 4*side;
    }
};

class Triangle: public PlaneFigure{
    /*****************************************************
    * Class Triangle derives from base class PlaneFigure.
    * The constructor takes 3 sides as parameters and
    * creates the triangle object with given sides
    * Members :
    * side1     : side number 1 of triangle object
    * side2     : side number 2 of triangle object
    * side3     : side number 3 of triangle object
    * area()     : returns area of triangle object
    * perimeter(): returns perimeter of triangle object
    ******************************************************/
    float side1;
    float side2;
    float side3;
public:
    Triangle(float a, float b, float c)
    {
        side1 = a;
        side2 = b;
        side3 = c;
    }
    float area()
    {
        float s = (side1+side2+side3)/2;
        float a = sqrt(s*(s-side1)*(s-side2)*(s-side3));
        return a;
    }
    float perimeter()
    {
        return side1+side2+side3;
    }
};

int main()
{
    int ch;             // Variable to store choice of user in program
    char cont = 'y';    // Variable to store choice of continuing after each run
    while(cont == 'y') // Run program till user wants
    {
        cout<<" Welcome to Geometry Master. The following program helps you in "
            <<"finding the area and perimeter of a rectangle, square, circle and "
            <<"triangle based on your input. "
            <<"Please enter your choice : "
            <<"1. Rectangle 2. Square 3. Circle 4. Triangle ";
        cin>>ch;
        switch(ch)              // Switch case over choice of user.
        {
            case 1:{            // For rectangle
                float l, b;
                cout<<"Enter the length of rectangle : ";
                cin>>l;
                cout<<"Enter the breadth of rectangle : ";
                cin>>b;
                Rectangle rec(l, b);
                cout<<"Area of given rectangle      : "<<rec.area()<<" cm^2 ";
                cout<<"Perimeter of given rectangle : "<<rec.perimeter()<<" cm ";
                cout<<" Do you want to calculate for another figure ? (y/n) :";
                cin>>cont;
                continue;
            }
            case 2:{            // For square
                float s;
                cout<<"Enter the side of square : ";
                cin>>s;
                Square square(s);
                cout<<"Area of given square      : "<<square.area()<<" cm^2 ";
                cout<<"Perimeter of given square : "<<square.perimeter()<<" cm ";
                cout<<" Do you want to calculate for another figure ? (y/n) :";
                cin>>cont;
                continue;
            }
            case 3:{            // For circle
                float r;
                cout<<"Enter the radius of circle : ";
                cin>>r;
                Circle circle(r);
                cout<<"Area of given circle      : "<<circle.area()<<" cm^2 ";
                cout<<"Perimeter of given square : "<<circle.perimeter()<<" cm ";
                cout<<" Do you want to calculate for another figure ? (y/n) :";
                cin>>cont;
                continue;
            }
            case 4:{            // For triangle
                float s1, s2, s3;
                cout<<"Enter the side 1 of triangle : ";
                cin>>s1;
                cout<<"Enter the side 2 of triangle : ";
                cin>>s2;
                cout<<"Enter the side 3 of triangle : ";
                cin>>s3;
                Triangle triangle(s1, s2, s3);
                cout<<"Area of given square      : "<<triangle.area()<<" cm^2 ";
                cout<<"Perimeter of given square : "<<triangle.perimeter()<<" cm ";
                cout<<" Do you want to calculate for another figure ? (y/n) :";
                cin>>cont;
                continue;
            }
            default:{
                cout<<"Wrong choice! Please re-enter."<<endl;
            }
        }
    }
    cout<<" Bye!"<<endl;
    return 0;
}