Geometry Based Programs

Hello,

I am stuck on this assignment question and am unsure as to how to get started.
Could anyone help me get started?

This is the question,

1. Write a program that creates a class hierarchy for simple geometry.

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

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

4. 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 just need help on getting started as it says this is one program. How would I be able to individually run each program from the CMD screen?

Thanks
1
2
3
4
5
6
class Point {
	double x, y;

public:
	// Constructor and operators go here
};
If you read through the assignment carefully, you can figure out your class declarations. I'll get you started:
Start with a Point class to hold x and y values of a point.
1
2
3
4
class Point {
public:
    double x, y;
};

Overload the >> operator to print point values
ostream &operator >> (ostream &os, const Point &p);

and the + and – operators to add and subtract point coordinates
So class Point also contains:
1
2
Point &operator+(const Point &rhs);
Point &operator-(const Point &rhs);

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
1
2
3
4
5
class Shape {
public:
    double area();
    double circumference();
};

plus provide the coordinates (Points) of a rectangle that encloses the shape (a bounding box).
So Shape should also contain something like:
void boundingBox(Point &top, Point &left, Point &right, Point &bottom);
Question to ask yourself: how will you compute the bounding box?

These will be overloaded by the derived classes as necessary.
Oh, so area(), circumference(), and boundingBox() should all be virtual.

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).
So Shape also contains
void display(ostream &os);
Note that I'm passing the stream to display() rather than simply dumping the output to stdout. By passing the stream, the function becomes much more flexible.
Question: does display() have to be virtual? A non-virtual function could print the area, circumference and bounding box, since those methods are virtual. But it can't print the name. Maybe class Shape should have a name member? These are all things to consider.

You can keep going like this through the rest of the assignment. When you're done, return the beginning and do it again: reading through the assignment and seeing if what you've sketched out for the header file will do everything the assignment requires. When you think you have everything done, just fill in the code.
Topic archived. No new replies allowed.