Write a C++ program which has a main-driver and creat a polygon class Poly which
ID: 3910463 • Letter: W
Question
Write a C++ program which has a main-driver and creat a polygon class Poly which has an array of n pairs of floats, x[i] and y[i], creat a derived class Triangle, creat a derived class Quadrilateral class (which you may assume is convex and points given clockwise) You need to compute the area for the two derived classes but use inheritance to compute perimeter in all these classes. Constructors, accessors, mutators, and anything else needed should be written too. Make sure to request points at constructor time in the driver. The driver should show a menu like: P for Polygon, T for Triangle, and Q for Quadrilateral. After recieving the data, the driver should print the perimeter and area.
Explanation / Answer
ScreenShot
-------------------------------------------
Program
//Header files
#include<iostream>
#include <iomanip>
using namespace std;
//Base class poly
class poly {
//Member variables
public:
int n;
float *x,*y;
//Constructor
poly(int size) {
n = size;
x=new float[n];
y=new float[n];
}
//Mutators
void setXvertices(float *xVer) {
*x = *xVer;
}
void setYvertices(float *yVer) {
*y = *yVer;
}
//Accessors
float* getXvertices() {
return x;
}
float* getYvertices() {
return y;
}
//Peimeter finding method
double Perimeter() {
double perimeter = 0;
for (int i = 0; i < n; i++) {
perimeter += sqrt(pow((*x+(i + 1) - *x+i), 2) + pow((*y + (i + 1) - *y + i), 2));
}
return perimeter;
}
//Area finding method
double Area() {
double area = 0;
// Calculate value of shoelace formula
int j = n - 1;
for (int i = 0; i < n; i++)
{
area += (*x+j + *x+i) * (*y+j - *y+i);
j = i; // j is previous vertex to i
}
// Return absolute value
return abs(area / 2.0);
}
};
//Child class
class triangle:public poly {
//Member method
public:
//Constructor
triangle(int n) :poly(n) {
n = 3;
}
//Area calculator
double Area() {
//double b= sqrt(pow((*x + 2 - *x + 1), 2) + pow((*y + 2 - *y + 1), 2)))
double apothem= (sqrt(pow((*x + 3 - *x + 2), 2) + pow((*y + 3 - *y + 2), 2)));
double area =( (Perimeter()*apothem)/2);
// Calculate value of shoelace formula
return area;
}
};
//Child class
class quadilateral :public poly {
//Member methods
public:
//Constructor
quadilateral(int n) :poly(n) {
n = 4;
}
//Area calculation
double Area() {
//double b= sqrt(pow((*x + 2 - *x + 1), 2) + pow((*y + 2 - *y + 1), 2)))
double apothem = (sqrt(pow((*x + 2 - *x + 1), 2) + pow((*y + 2 - *y + 1), 2)));
double area = ((Perimeter()*apothem) / 2);
// Calculate value of shoelace formula
return area;
}
};
//Driver method
int main()
{
//Variables for choice and input and array
char ch;
int v;
float *x, *y;
cout << " User menu :" << endl;
//Prompt user for choice
cout << "(P)olygon (T)riangle (Q)uadrilateral Enter your choice:";
cin >> ch;
//If polygon
if (ch == 'p' || ch == 'P') {
//Prompt for number of vertices
cout << "Enter the number of vertices:";
cin >> v;
//Set array that size
x = new float[v];
y = new float[v];
//Object creation
poly p(v);
//Values of vertices
cout << "Enter x values:";
for (int i = 0; i < v; i++) {
cin >> x[i];
}
cout << "Enter y values:";
for (int i = 0; i < v; i++) {
cin >> y[i];
}
//Set vertices array
p.setXvertices(x);
p.setYvertices(y);
//Display area and perimeter
cout << "Perimeter of Polygon=" << setprecision(2) << p.Perimeter() << endl;
cout << "Area of Polygon=" << setprecision(2) << p.Area() << endl;
}
//If triangle
else if (ch == 't' || ch == 'T') {
//set array size
v = 3;
x = new float[v];
y = new float[v];
//object creation
triangle t(v);
//values
cout << "Enter x values:";
for (int i = 0; i < v; i++) {
cin >> x[i];
}
cout << "Enter y values:";
for (int i = 0; i < v; i++) {
cin >> y[i];
}
//set array
t.setXvertices(x);
t.setYvertices(y);
//Display result
cout << "Perimeter of Triangle=" << setprecision(2) << t.Perimeter() << endl;
cout << "Area of Tringle=" << setprecision(2) << t.Area() << endl;
}
//If quadrilateral
else if (ch == 'q' || ch == 'Q') {
//Set array size
v = 4;
x = new float[v];
y = new float[v];
//Object creation
quadilateral q(v);
cout << "Enter x values:";
for (int i = 0; i < v; i++) {
cin >> x[i];
}
cout << "Enter y values:";
for (int i = 0; i < v; i++) {
cin >> y[i];
}
//set values in arrays
q.setXvertices(x);
q.setYvertices(y);
//display result
cout << "Perimeter of Quadrilateral=" << setprecision(2) << q.Perimeter() << endl;
cout << "Area of Quadrilateral=" << setprecision(2) << q.Area() << endl;
}
//Wrong choice
else {
cout << "Wrong choice" << endl;
}
return 0;
}
-------------------------------------------------------------------------
Output
User menu :
(P)olygon
(T)riangle
(Q)uadrilateral
Enter your choice:q
Enter x values:1
2
3
4
Enter y values:5
6
7
8
Perimeter of Quadrilateral=23
Area of Quadrilateral=48
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.