Question

IN C++ - most of this is done it's just missing the bolded part... Write a...

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

Homework Answers

Answer #1

`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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Hello. I have an assignment that is completed minus one thing, I can't get the resize...
Hello. I have an assignment that is completed minus one thing, I can't get the resize method in Circle class to actually resize the circle in my TestCircle claass. Can someone look and fix it? I would appreciate it! If you dont mind leaving acomment either in the answer or just // in the code on what I'm doing wrong to cause it to not work. That's all I've got. Just a simple revision and edit to make the resize...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX;...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX; y= initialY; } public boolean equals (Object o){ if (o instanceof Point){ Point other = (Point)o; return (x == other.x && y == other.y); }else{ return false; } } } We haev defined "equals" method for our class using "instanceof". We define and use instances (or objects) of this class in the following scenarios. In each case, specify what is the output. (hint: there...
Java code Problem 1. Create a Point class to hold x and y values for a...
Java code Problem 1. Create a Point class to hold x and y values for a point. Create methods show(), add() and subtract() to display the Point x and y values, and add and subtract point coordinates. Tip: Keep x and y separate in the calculation. Create another class Shape, which will form the basis of a set of shapes. The Shape class will contain default functions to calculate area and circumference of the shape, and provide the coordinates (Points)...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
Create the ArrayList program example in listing 13.1, Battlepoint. Describe the code (in your WORD document)...
Create the ArrayList program example in listing 13.1, Battlepoint. Describe the code (in your WORD document) in the 'if' statement if (targets.indexOf(shot) > -1) { ADD a NEW ArrayList of points called 'misses' ADD code to add a point to the 'misses' list on a miss (if a SHOT does NOT hit a TARGET) ADD code to place an 'M' in the final output map for all shots that were MISSES. ADD code to place an H on the target...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent points in the cartesian plane #include <iostream> using namespace std; class Point { public:     Point()      // default constructor         Point(double x, double y); // another constructor         double x() const; // get function, return _x         double y() const; // get function, return _y private:         double _x, _y; }; (ii) Here is a test driver file for the Point class...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's length, width, and area Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters. Change this program to calculate and display the rectangle's perimeter. Example: In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...