Hey Guys,
So, just for full disclosure, here is the full question I am working on:
a. 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). (Completed)
b. Create a base class Shape which will form the basis of your shapes.
The Shape class will contain 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 as necessary. Create a display()
function that will display the name of the class plus all stored
information about the class (including area, circumference, and
bounding box).
c. 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).
d. In main(), create one instance each of the following: a Circle with a
radius of 23, a Square with sides 25, and a Triangle with sides 10,
20, 30. Define all of them so that the origin (0,0) is somewhere
within each object. Display the information from each object.
I completed part a, with help of course, and it seems to be working well. For part b, I am trying to figure out where I overload the functions? Do I over load the functions (area, circumference, etc.) in the base class (part b), or do I overload the functions in the derived classes (part c, which is not included yet)?
Here is my code thus far:
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
|
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159
//Part A
class Point {
int x;
int y;
public:
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 Works
ostream &operator<<(ostream &stream, Point &p) {
stream << "(" << p.x << ", " << p.y << ")" << endl;
return stream;
}
// + Operator Overload Works
Point Point::operator+(const Point& pt) {
Point result = *this;
result.x += pt.x;
result.y += pt.y;
return result;
}
// - Operator Overload Works
Point Point::operator-(const Point& pt) {
Point result = *this;
result.x -= pt.x;
result.y -= pt.y;
return result;
}
// Part B
class Shape {
public:
void area(int radius);
void area(int length, int breadth);
void area(double height, double breadth);
void circumference(int radius);
void circumference(int length, int breadth);
void circumference(double height, double breadth);
void boundbox(int radius);
void boundbox(int length, int breadth);
void boundbox(double height, double breadth);
void display();
};
// Part D
int main() {
Point pt0(0, 0); // Point Center
Point pt1(0, 23); // Radius
Point pt2(14, 14); // Top Right Corner
Point pt3(14, -11); // Bottom Right Corner
Point pt4(-11, 14); // Top Left Corner
Point pt5(-11, -11); // Bottom Left Corner
//Point pt6(,); // Top Vertex, TBD
//Point pt7(,); // Bottom Left Vertex, TBD
//Point pt8(,); // Bottom Right Vertex, TBD
// Circle cobject(pt0, pt1);
// cobject.display();
//
// Square sobject(pt2, pt3, pt4, pt5); //side length of 25
// sobject.display();
//
// Triangle tobject(pt6, pt7, pt8); //side length of 10, 20 30
// tobject.display();
return 0;
}
|
I am pretty new to programming, so any suggestions would be greatly appreciated. Please point out any other places where I could improve my code!