Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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 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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

6. Develop policies to help employees achieve work-life balance.

Answered: 1 week ago

Question

2. What type of team would you recommend?

Answered: 1 week ago