Question: Please code in C++: I mostly need help with the bolded session, Im not sure how to handle the bounding box : Write a program

Please code in C++: I mostly need help with the bolded session, Im not sure how to handle the bounding box :

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.

Below is my code:

#include

#include

using namespace std;

class Point

{

public:

double x;

double y;

Point()

{

x=0;

y=0;

}

Point(double m,double n)

{

x=m;

y=n;

}

friend ostream& operator<<(ostream& os,Point& p)

{

os<<"x-coordinate is :"<

os<<"y-coordinate is :"<

return os;

}

Point operator+(Point& obj)

{

Point p;

p.x=x+obj.x;

p.y=y+obj.y;

return p;

}

Point operator-(Point& obj)

{

Point p;

p.x=x-obj.x;

p.y=y-obj.y;

return p;

}

};

class Shape

{

public:

void area();

void circumference();

void display();

void boundingbox();

};

class Circle:public Shape

{

Point p1;

double radius;

double aCircle;

double cCircle;

public:

Circle(Point& p,double r)

{

p1=p;

radius=r;

}

void area()

{

aCircle= 3.14*(radius*radius);

cout<<"Area of circle is "<

}

void circumference()

{

cCircle= 3.14*2*radius;

cout<<"circumference of circle is "<

}

void display()

{

cout<<"Area of circle is "<

cout<<"circumference of circle is "<< cCircle<

}

};

class Square:public Shape

{

Point p1;

Point p2;

Point p3;

Point p4;

double aSquare;

double cSquare;

public:

Square(Point& a,Point& b,Point& c,Point& d)

{

p1=a;

p2=b;

p3=c;

p4=d;

}

void area()

{

double s=sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );

aSquare= s*s;

cout<<"Area of Square is "<

}

void circumference()

{

double s=sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );

cSquare= 4*s;

cout<<"circumference of Square is "<

}

void display()

{

cout<<"Area of Square is "<

cout<<"circumference of Square is "<

}

};

class Triangle:public Shape

{

Point p1;

Point p2;

Point p3;

double aTriangle;

double cTriangle;

public:

Triangle(Point& a,Point& b,Point& c)

{

p1=a;

p2=b;

p3=c;

}

void circumference()

{

double s1=sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );

double s2=sqrt( (p1.x-p3.x)*(p1.x-p3.x) + (p1.y-p3.y)*(p1.y-p3.y) );

double s3=sqrt( (p3.x-p2.x)*(p3.x-p2.x) + (p3.y-p2.y)*(p3.y-p2.y) );

cTriangle = s1+s2+s3;

cout<<"circumference of Triangle is "<

}

void area()

{

aTriangle=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 "<

}

void display()

{

cout<<"circumference of Triangle is "<

cout<<"Area of Triangle is "<

}

};

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;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!