IN C most of this is done its just missing the bolded part
IN C++ - most of this is done it\'s just missing the bolded part...
Write a program that creates a class hierarchy for simple geometry.
Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation).
Create a pure abstract base class Shape, which will form the basis of your shapes. The Shape class will contain abstract functions to calculate area and circumference of the shape, plus provide the coordinates (Points) of a rectangle that encloses the shape (a bounding box). These will be overloaded by the derived classes. Create a display() function that will display the name of the class, and all stored information about the class (including area, circumference, and bounding box).
Build the hierarchy by creating the Shape classes Circle, Square, and Triangle. For these derived classes, create default constructors, and constructors whose arguments can initialize the shapes appropriately using the correct number of Point objects (i.e., Circle requires a Point center and a radius, Square requires four Point vertices, while Triangle requires three Point vertices).
In main(), create one instance each of the following: Circle (10, -5) with a radius of 23; Square (5, -5)(-10,7)(4,23)(-6,12); and Triangle(0,0)(10,10)(-15,15). Display the information from each object.
#include <iostream>
using namespace std;
class Point
{
public:
int x, y;
Point()
{
x = 0;
y = 0;
}
Point(int a, int b)
{
x = a;
y = b;
}
Point operator+(Point& obj)
{
Point t;
t.x = x + obj.x;
t.y = y + obj.y;
return t;
}
Point operator-(Point& obj)
{
Point t;
t.x = x - obj.x;
t.y = y - obj.y;
return t;
}
friend ostream& operator<<(ostream& o, Point& p)
{
o << \"X coordinate :\" << p.x << endl;
o << \"Y coordinate :\" << p.y << endl;
return o;
}
};
class Shape
{
public:
void area();
void circumference();
void display();
};
class Circle :public Shape
{
Point p;
int radius;
public:
Circle(Point& p1, int r)
{
p = p1;
radius = r;
}
void area()
{
cout << \"Area of circle is \" << 3.14*radius*radius << endl;
}
void circumference()
{
cout << \"circumference of circle is \" << 3.14 * 2 * radius << endl;
}
void display()
{
cout << \"Area of circle is \" << 3.14*radius*radius << endl;
cout << \"circumference of circle is \" << 3.14 * 2 * radius << endl;
}
};
class Square :public Shape
{
Point p1, p2, p3, p4;
public:
Square(Point& p1, Point& p2, Point& p3, Point& p4)
{
this->p1 = p1;
this->p2 = p2;
this->p3 = p3;
this->p4 = p4;
}
void area()
{
int side = sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
cout << \"Area of Square is \" << side*side << endl;
}
void circumference()
{
int side = sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
cout << \"circumference of Square is \" << 4 * side << endl;
}
void display()
{
int side = sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
cout << \"Area of Square is \" << side*side << endl;
cout << \"circumference of Square is \" << 4 * side << endl;
}
};
class Triangle :public Shape
{
Point p1, p2, p3;
public:
Triangle(Point& a, Point& b, Point& c)
{
p1 = a;
p2 = b;
p3 = c;
}
void circumference()
{
int s1 = sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
int s2 = sqrt((p1.x - p3.x)*(p1.x - p3.x) + (p1.y - p3.y)*(p1.y - p3.y));
int s3 = sqrt((p3.x - p2.x)*(p3.x - p2.x) + (p3.y - p2.y)*(p3.y - p2.y));
cout << \"circumference of Triangle is \" << s1 + s2 + s3 << endl;
}
void area()
{
int area = abs(p1.x*(p2.y - p3.y) + p2.x*(p3.y - p1.y) + p3.x*(p1.y - p2.y)) / 2;
cout << \"Area of Triangle is \" << area << endl;
}
void display()
{
int s1 = sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
int s2 = sqrt((p1.x - p3.x)*(p1.x - p3.x) + (p1.y - p3.y)*(p1.y - p3.y));
int s3 = sqrt((p3.x - p2.x)*(p3.x - p2.x) + (p3.y - p2.y)*(p3.y - p2.y));
cout << \"circumference of Triangle is \" << s1 + s2 + s3 << endl;
int area = abs(p1.x*(p2.y - p3.y) + p2.x*(p3.y - p1.y) + p3.x*(p1.y - p2.y)) / 2;
cout << \"Area of Triangle is \" << area << endl;
}
};
int main(int argc, char const *argv[])
{
Point p1(10, -5);
Circle c(p1, 23);
c.area();
c.circumference();
cout << \"=========================================\ \";
Point p2(5, -5);
Point p3(-10, 7);
Point p4(4, 23);
Point p5(-6, 12);
Square s(p2, p3, p4, p5);
s.area();
s.circumference();
cout << \"=========================================\ \";
Point p6(0, 0);
Point p7(10, 10);
Point p8(-15, 15);
Triangle t(p6, p7, p8);
t.area();
t.circumference();
return 0;
}
Solution
#include <iostream>
#include <cmath>
using namespace std;
class Point
{ double x, y;
public:
Point ()
{ x = 0; y = 0; }
Point (double px, double py)
{ x = px; y = py; }
friend ostream & operator << (ostream &os, const Point &p);
Point &operator+(const Point&rhs);
Point &operator-(const Point&rhs);
void setPoint(double px, double py)
{ x = px; y = py; }
double get_x() const
{ return x; }
double get_y() const
{ return y; }
};
class Shape
{
protected:
double width, height;
public:
Shape ()
{ width = 0; height = 0; }
Shape (double w, double h)
{ width = w; height = h; }
virtual double area() const = 0;
virtual double circumference () const = 0;
};
class Circle: public Shape
{ Point center;
double radius;
public:
Circle (Point p, double r)
{ center = p; radius = r; }
void setRadius (double r)
{ radius = r; }
double getRadius() const
{ return radius;
}
double circumference()
{ return 2*radius*3.14159;
}
double area()
{ return 3.14159*(radius*radius);
}
};
int main()
{
Point p (4,6);
Circle c (p,5);
cout << \"The circle\'s circumference is: \" << c.circumference() <<endl;
return 0;
}





