1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159
//Part A
class Point {
public:
int x;
int y;
Point(int px , int py) : x(px) , y(py){}
friend ostream &operator<<(ostream &stream, Point &p);
Point operator+(const Point& pt);
Point operator-(const Point& pt);
};
// >> Operator Overload
ostream &operator<<(ostream &stream, Point &p) {
stream << "(" << p.x << ", " << p.y << ")" << endl;
return stream;
}
// + Operator Overload
Point Point::operator+(const Point& pt) {
Point result = *this;
result.x += pt.x;
result.y += pt.y;
return result;
}
// - Operator Overload
Point Point::operator-(const Point& pt) {
Point result = *this;
result.x -= pt.x;
result.y -= pt.y;
return result;
}
// Part B
class Shape {
public:
virtual double area() = 0;
virtual double circumference() = 0;
//virtual ? boundbox() = 0; // vector?
virtual void display() = 0;
};
// //Part C
class Circle: public Shape {
Point s0, s1;
public:
double distrad, area_c, circum_c;
Circle(); //Default Constructor
Circle (const Point &pt0, const Point &pt1): s0(pt0), s1(pt1) {} //Constructor with point center and radius arguments
double area();
double circumference();
//void boundbox
void display();
};
double Circle::area(){
Point radius = s1 - s0;
distrad = sqrt((radius.x * radius.x) + (radius.y * radius.y));
double area_c = PI * distrad * distrad;
return area_c;
}
double Circle::circumference(){
double circum_c = PI * 2 * distrad;
return circum_c;
}
void Circle::display(){
cout << "Class = Triangle" << endl;
cout << "Area: " << area_c << endl;
cout << "Circumference: " << circum_c << endl;
//cout << "Boundbox: " << << endl; //Haven't defined this yet
}
class Square: public Shape {
Point s0, s1, s2, s3;
public:
double lenght, area_s, circum_s;
Square(); //Default Constructor
Square(const Point &pt2, const Point &pt3, const Point &pt4, const Point &pt5): s0(pt2), s1(pt3), s2(pt4), s3(pt5) {} //Constructor with 4 points
double area();
double circumference();
//void boundbox
void display();
};
double Square::area(){
Point lenght = s1 - s0;
double area_s = (s0.x + s1.x) * (s0.y+s1.y);
return area_s;
}
double Square::circumference(){
Point lenght = s1 - s0;
double circumference_s = (s0.x + s1.x) * 4;
return circum_s;
}
void Square::display(){
cout << "Class = Square" << endl;
cout << "Area: " << area_s << endl;
cout << "Circumference: " << circum_s << endl;
//cout << "Boundbox: " << << endl; //Haven't defined this yet
}
// Part D
int main() {
Point pt0(0, 0); // Point Center
Point pt1(0, 23); // Radius
Point pt2(25, 25); // Top Right Corner
Point pt3(25, 0); // Bottom Right Corner
Point pt4(0,25 ); // Top Left Corner
Point pt5(0, 0); // Bottom Left Corner
Point pt6(0, 10); // Top Vertex, TBD
Point pt7(10, 0); // Bottom Left Vertex, TBD
Point pt8(30, 10); // Bottom Right Vertex, TBD
Circle cobject(pt0, pt1);
cobject.display();
Square sobject (pt2, pt3, pt4, pt5);
sobject.display();
// Triangle tobject(pt6, pt7,pt8);
//tobject.display();
}
Output:
Class = Triangle
Area: 3.40907e+063
Circumference: 8.48057e-307
Class = Square
Area: 0
Circumference: 4.711127
|