Using C write a program to calculate the area and perimeter

Using C++, write a program to calculate the area and perimeter of four simple plane figures: a rectangle, a circle, a square and a triangle.

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 of the 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, such as “PlaneFigure” might be a good choice). Other classes (for example, “Rectangle”, “Circle”, “Square” and “Triangle”) must be derived from the “PlaneFigure” 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.

Solution

// C++ code

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;

class PlaneFigure
{
protected:
double x,y,z;
public:
virtual void getData()=0;
virtual void showArea()=0;
virtual void showPerimeter()=0;
};

class triangle : public PlaneFigure
{
public:
void getData(void)
{
cout<<\"\ \ =====Data Entry for Triangle=====\ \ \";
cout<<\"Enter 3 sides : \";
cin>>x>>y >>z;
}
void showArea(void)
{
double aot;
double s = (x+y+z)/2;
aot = sqrt(s*(s-x)*(s-y)*(s-z));
cout<<\"Area of Triangle is \"<< aot << endl;
}
void showPerimeter(void)
{
cout<<\"Perimeter of Triangle is \"<<(x+y+z) << endl << endl;
}
};

class rectangle : public PlaneFigure
{
public:
void getData(void)
{
cout<<\"\ \ =====Data Entry for Rectangle=====\ \ \";
cout<<\"Enter length of two sides : \";
cin>>x>>y;
}
void showArea(void)
{
double aor;
aor = x * y;
cout<<\"Area of Rectangle is \"<<aor << endl;
}
void showPerimeter(void)
{
cout<<\"Perimeter of rectangle is \"<<(2*(x+y)) << endl << endl;
}
};

class circle : public PlaneFigure
{
public:
void getData(void)
{
cout<<\"\ \ =====Data Entry for Circle=====\ \ \";
cout<<\"Enter radius of circle : \";
cin>>x;
}
void showArea(void)
{
double aoc;
aoc = 3.14 *x*x;
cout<<\"Area of circle is \"<<aoc << endl;
}
void showPerimeter(void)
{
cout<<\"Perimeter of circle is \"<<(2*3.14*x) << endl << endl;
}
};

class square : public PlaneFigure
{
public:
void getData(void)
{
cout<<\"\ \ =====Data Entry for Square=====\ \ \";
cout<<\"Enter length of side : \";
cin>>x;
}
void showArea(void)
{
double aos;
aos = x*x;
cout<<\"Area of square is \"<<aos << endl;
}

void showPerimeter(void)
{
cout<<\"Perimeter of square is \"<<(4*x) << endl << endl;
}
};


int main()
{
triangle tri;
rectangle rect;
circle cir;
square sq;
PlaneFigure *list[4];
list[0]=&tri;
list[1]=&rect;
list[2]=&cir;
list[3]=&sq;

int choice;
while(1)
{
cout<<\"\ =====MEASURES OF DIFFERENT PlaneFigure=====\ \";
cout<<\"\ Choose your choice\ \";
cout<<\"1)Triangle\ \";
cout<<\"2)Rectangle\ \";
cout<<\"3)Circle\ \";
cout<<\"4)Triangle\ \";
cout<<\"5)Exit\ \";
cout<<\"Enter your choice: \";
cin>>choice;
switch(choice)
{
case 1 : list[0]->getData();
list[0]->showArea();
list[0]->showPerimeter();
break;
case 2 : list[1]->getData();
list[1]->showArea();
list[1]->showPerimeter();
break;
case 3 : list[2]->getData();
list[2]->showArea();
list[2]->showPerimeter();
break;
case 4 : list[3]->getData();
list[3]->showArea();
list[3]->showPerimeter();
break;
case 5 :
return 0;
default: cout<<\"\ \ Invalid choice\ Try again\ \";
}
}
}


/*
output:

=====MEASURES OF DIFFERENT PlaneFigure=====

Choose your choice
1)Triangle
2)Rectangle
3)Circle
4)Square
5)Exit
Enter your choice: 1


=====Data Entry for Triangle=====

Enter 3 sides : 2 3 4
Area of Triangle is 2.90474
Perimeter of Triangle is 9


=====MEASURES OF DIFFERENT PlaneFigure=====

Choose your choice
1)Triangle
2)Rectangle
3)Circle
4)Square
5)Exit
Enter your choice: 2


=====Data Entry for Rectangle=====

Enter length of two sides : 3 4
Area of Rectangle is 12
Perimeter of rectangle is 14


=====MEASURES OF DIFFERENT PlaneFigure=====

Choose your choice
1)Triangle
2)Rectangle
3)Circle
4)Square
5)Exit
Enter your choice: 3


=====Data Entry for Circle=====

Enter radius of circle : 4
Area of circle is 50.24
Perimeter of circle is 25.12


=====MEASURES OF DIFFERENT PlaneFigure=====

Choose your choice
1)Triangle
2)Rectangle
3)Circle
4)Square
5)Exit
Enter your choice: 4


=====Data Entry for Square=====

Enter length of side : 2
Area of square is 4
Perimeter of square is 8


=====MEASURES OF DIFFERENT PlaneFigure=====

Choose your choice
1)Triangle
2)Rectangle
3)Circle
4)Square
5)Exit
Enter your choice: 5

*/

Using C++, write a program to calculate the area and perimeter of four simple plane figures: a rectangle, a circle, a square and a triangle. The program should
Using C++, write a program to calculate the area and perimeter of four simple plane figures: a rectangle, a circle, a square and a triangle. The program should
Using C++, write a program to calculate the area and perimeter of four simple plane figures: a rectangle, a circle, a square and a triangle. The program should
Using C++, write a program to calculate the area and perimeter of four simple plane figures: a rectangle, a circle, a square and a triangle. The program should
Using C++, write a program to calculate the area and perimeter of four simple plane figures: a rectangle, a circle, a square and a triangle. The program should

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site