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 <<
"=========================================\n";
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 <<
"=========================================\n";
Point p6(0, 0);
Point p7(10, 10);
Point p8(-15, 15);
Triangle t(p6, p7, p8);
t.area();
t.circumference();
return 0;
}
`Hey,
Note: In case of any queries, just comment in box I would be very happy to assist all your queries
Excutable code:
#include <iostream>
#include <algorithm>
#include <math.h>
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 :"<<p.x<<endl;
os<<"y-coordinate is :"<<p.y<<endl;
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;
Point ll;
Point lr;
Point ul;
Point ur;
public:
Circle(Point& p,double r)
{
p1=p;
radius=r;
}
void area()
{
aCircle= 3.14*(radius*radius);
cout<<"Area of circle is "<<aCircle<<endl;
}
void circumference()
{
cCircle= 3.14*2*radius;
cout<<"circumference of circle is
"<<cCircle<<endl;
}
void display()
{
cout<<"Area of circle is "<<aCircle<<endl;
cout<<"circumference of circle is "<<
cCircle<<endl;
cout<< "Bounding box coordinates is : "<<"lower left:
"<<ll<<" lower right: "<<lr<<" upper left:
"<<ul<<" upper right:"<<ur<<endl;
}
void boundingbox(){
ll.x= p1.x-radius;
ll.y= p1.y-radius;
ur.x= p1.x+radius;
ur.y= p1.y+radius;
lr.x = ur.x;
lr.y = ll.y;
ul.x = ll.x;
ul.y = ur.y;
cout<< "Bounding box coordinates is : \n"<<"lower left:
"<<ll<<" lower right: "<<lr<<" upper left:
"<<ul<<" upper right:"<<ur<<endl;
}
};
class Square:public Shape
{
Point p1;
Point p2;
Point p3;
Point p4;
Point ll;
Point lr;
Point ul;
Point ur;
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 "<<aSquare<<endl;
}
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
"<<cSquare<<endl;
}
void boundingbox(){
ll.x=min(p1.x,min(p2.x,min(p3.x,p4.x)));
ll.y=min(p1.y,min(p2.y,min(p3.y,p4.y)));
ur.x=max(p1.x,max(p2.x,max(p3.x,p4.x)));
ur.y=max(p1.y,max(p2.y,max(p3.y,p4.y)));
lr.x=ur.x;
lr.y=ll.y;
ul.x=ll.x;
ul.y=ur.y;
cout<< "Bounding box coordinates is : \n"<<"lower left:
"<<ll<<" lower right: "<<lr<<" upper left:
"<<ul<<" upper right:"<<ur<<endl;
}
void display()
{
cout<<"Area of Square is "<<aSquare<<endl;
cout<<"circumference of Square is
"<<cSquare<<endl;
cout<< "Bounding box coordinates is : \n"<<"lower left:
"<<ll<<" lower right: "<<lr<<" upper left:
"<<ul<<" upper right:"<<ur<<endl;
}
};
class Triangle:public Shape
{
Point p1;
Point p2;
Point p3;
Point ll;
Point lr;
Point ul;
Point ur;
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
"<<cTriangle<<endl;
}
void area()
{
aTriangle=abs((int)(p1.x*(p2.y-p3.y)+p2.x*(p3.y-p1.y)+p3.x*(p1.y-p2.y)))/2;
cout<<"Area of Triangle is
"<<aTriangle<<endl;
}
void boundingbox(){
ll.x=min(p1.x,min(p2.x,p3.x));
ll.y=min(p1.y,min(p2.y,p3.y));
ur.x=max(p1.x,max(p2.x,p3.x));
ur.y=max(p1.y,max(p2.y,p3.y));
lr.x=ur.x;
lr.y=ll.y;
ul.x=ll.x;
ul.y=ur.y;
cout<< "Bounding box coordinates is : \n"<<"lower left:
"<<ll<<" lower right: "<<lr<<" upper left:
"<<ul<<" upper right:"<<ur<<endl;
}
void display()
{
cout<<"circumference of Triangle is
"<<cTriangle<<endl;
cout<<"Area of Triangle is
"<<aTriangle<<endl;
cout<< "Bounding box coordinates is : \n"<<"lower left:
"<<ll<<" lower right: "<<lr<<" upper left:
"<<ul<<" upper right:"<<ur<<endl;
}
};
int main(int argc, char const *argv[])
{
Point p1(10,-5);
Circle c(p1,23);
c.area();
c.circumference();
c.boundingbox();
cout<<"********************************\n";
c.display();
cout<<"=========================================\n";
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();
s.boundingbox();
cout<<"********************************\n";
s.display();
cout<<"=========================================\n";
Point p6(0,0);
Point p7(10,10);
Point p8(-15,15);
Triangle t(p6,p7,p8);
t.area();
t.circumference();
t.boundingbox();
cout<<"********************************\n";
t.display();
return 0;
}
Sample output:
Kindly revert for any queries
Thanks.
Get Answers For Free
Most questions answered within 1 hours.