SOLUTION
#include <iostream>
using namespace std;
class ClosedPolygon
{
public:
ClosedPolygon();
~ClosedPolygon();
double area();
friend istream& operator>>(istream& in, ClosedPolygon& poly);
friend ostream& operator<<(ostream& out, const ClosedPolygon& poly);
private:
void resize(int nsize);
double* x;
double* y;
int size;
};
ClosedPolygon::ClosedPolygon()
{
size = 0;
x = NULL;
y = NULL;
}
ClosedPolygon::~ClosedPolygon()
{
delete[] x;
delete[] y;
}
void ClosedPolygon::resize(int nsize)
{
delete[] x;
delete[] y;
x = new double[nsize];
y = new double[nsize];
size = nsize;
}
double ClosedPolygon::area()
{
double result = 0;
for (int i = 0; i < size - 1; i++)
{
result += (x[i+1] + x[i])*(y[i+1] - y[i]);
}
if (result < 0)
result *= -1;
return result / 2;
}
istream& operator>>(istream& in, ClosedPolygon& poly)
{
cout << "Enter number of points: ";
int size;
in >> size;
poly.resize(size);
cout << "Enter x y pair: ";
for (int i = 0; i < size; i++)
in >> poly.x[i] >> poly.y[i];
return in;
}
ostream& operator<<(ostream& out, const ClosedPolygon& poly)
{
out << "x\ty" << endl;
for (int i = 0; i < poly.size; i++)
cout << poly.x[i] << "\t" << poly.y[i] << endl;
return out;
}
int main()
{
ClosedPolygon poly;
cin >> poly;
cout << "The area is: " << poly.area() << endl;
return 0;
}
// sample run
2. If n points are connected to form a closed polygon as shown below, the area...
Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on page 362, reprinted below 9.9 (Geometry: n-sided regular polygon) In an n-sided regular polygon, all sides have the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular) Design a class named RegularPolygon that contains A private int data field named n that defines the number of sides in the polygon with default value of 3 A...