Point Class - Geometry Based Programs

Write your question here.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.

Below is what I have so far. I'm having troubles understanding how to get area from pointers. Can I just get the difference between points and multiply them? I thought pointers pointed to an address in the memory in hexadecimal? A little confused on where to go from here.Thanks everyone!
Thanks

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
  #include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Point{
    public:
        double x, y;

        ostream &operator >> (ostream &os, const Point &p);
        Point &operator+(const Point&rhs);
        Point &operator-(const Point &rhs);

    private:

 };

 class Shape{
    public:
        double area();
        double circumference ();
        void boundingBox(Point &top, Point &left, Point &right, Point &bottom);
 };

 int main()
{
   

    return 0;
 }
Last edited on
A point is not a pointer.

You are not asked to calculate the area of points. The boundingBox function reads points from the shapes but those could be calculated inside the function. For instance the Circle class only needs a center point and a radius for you to be able to calculate the area and the corner points of the bounding box.
Last edited on
Hi Peter!

That makes sense. Now, if I were to make a function for the bounding area for the circle, would I have something like

void boundingCircle(Point &center, Point &radius)

I am unsure how to approach this problem and I have until wednesday to complete it.

Thanks!
They're asking for a bounding box (rectangle), not a bounding circle.

The instructions are not totally clear to me. They don't say what coordinates in the bounding box that they want but I guess they mean the corner coordinates. It seems a bit redundant to use four points (one for each corner) because half of the values would be duplicated (x1, y1) (x2, y1) (x1, y2) (x2, y2). You wouldn't lose any information if you only provided the top-left and bottom-right corners (x1, y1) (x2, y2).
Last edited on
Yea, I understand that. I just don't understand how it would know what shape to pick without an input of coordinates or something. The question is oddly written
Each shape class should have it's own version of the bounding box function.

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.
* Don't they mean overridden?


The coordinates and size of each shape is passed in through the constructor when the object is created.

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.
Last edited on
Ok, I understand now. However, i'm having issues writing my point class up. May you help me with that?
Last edited on
i'm having issues writing my pointer class

Don't you mean your Point class? I see nothing having to do with pointers.
What problems are you having? Please be specific.

Line 8: Why are x and y public?

Line 10: You're overload the input operator >>. You can input to a const object.

Line 18: Do you mean to inherit Shape from Point?

Line 20-21: area() and circumference() should be pure virtual. area and circumference have no meaning for Shape.

Lines 27-29: Your main() has no logic.

And yes, I meant my point class. Sorry, i've been running on minimal sleep lol And, I am aware that my main has no logic, but I want to get my classes all figured out first before I write my main. X and Y need to be private then. My question, is how do I denote x and y as a coordinate, then relate that to shapes in my logic later on? That's what i'm more or less stumped on. As for virtual, I'll need to read up on that, I haven't heard about it
X and Y need to be private then

That depends on whether Shape inherits Point. If Shape inherits Point, then X and Y can be protected to allow access from Shape and classes derived from Shape. In reviewing the instructions, they say nothing about inheriting Point, so I would leave it separate.

how do I denote x and y as a coordinate

X and Y are a Point. Wherever you would use X and Y, use a Point instance.
For example, if you derive a Circle class from Shape, the center if the circle is a Point.

Point needs a constructor that takes X and Y.

Problem statement item 1a,
Overload the >> operator to print point values

The >> operator is the wrong direction. If you're going to print X and Y, you want to overload the << operator. This is assuming this is a simple typo and your instructor is not being an ahole by intentionally telling you to overload the wrong operator.

then relate that to shapes in my logic later on?

Try defining your Circle class. As I pointed out earlier, a Circle has a Point (center) and a radius.


Alright, so this is what I did so far. I made a circle class, although I'm still trying to make a point form x and y (I know you said it is the point), but how would I incorporate that? (I feel like i'm over thinking something) Code is below
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
#include <iostream>
#include <math.h>

using namespace std;
// Class to represent points.
class Point{
    public:
        istream &operator << (istream &is, const Point &p);
        Point &operator+(const Point&rhs);
        Point &operator-(const Point&rhs);

        void setPoint_x(double x){
            Point_x = x;
        }
        double getPoint_x(){
            return Point_x;
        }
        void setPoint_y(double y){
            Point_y = y;
        }
        double getPoint_y(){
            return Point_y;
        }
    private:
         double x, y;

class Shape {
  protected:
    int width, height, radius;
  public:
        double area();
        double circumference ();
};

class Circle: public Shape {
public:
    circle(double x, double y, double r);

        void setRadius(double r){
            Radius = r
        }
        double getRadius()const{
            return Radius;
        }
        void setCircumference(){
            double c;

            c = (2*Radius*3.14159)

            return c;
        }
};
int main () {
  
  return 0;
}
Line 2: Correct header is <cmath>

Line 7: Point has no constructor which takes an x and y value.

Line 8: Should be an ostream.

Line 13,19: Point_x (Point_y) are undefined. You're trying to set the member variables x, y. You want to name your argument something different.

Line 16,22: Point_x (Point_y) are undefined. You want the member variables x, y.

Line 26: Point is missing };

Line 29: A Shape does not have a radius. A radius is an attribute of a Circle.

Line 27: Shape has no constructor. Shapre should have a constructor that initializes width and height.

Line 31-32: area() and circumference() should be virtual.

Line 37: Circle's constructor should be capitalized. Don't pass x and y to Circle's constructor. Pass a Point for the center.

Line 40,48: radius should not be capitalized.

Line 45: You trying to return a double, but setCircumference is type void.
Line 45: The definition of circumference must match the function you're overloading in Shape.

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
#include <iostream>
#include <cmath>
using namespace std;

// Class to represent points.
class Point
{   double x, y;
public:
    Point ()        //  Default constructor
    {   x = 0; y = 0;   }
    
    Point (double px, double py)
    {   x = px;  y = py;    }
    
    friend ostream & operator << (ostream &os, const Point &p);
    Point &operator+(const Point&rhs);
    Point &operator-(const Point&rhs);

    void setPoint (double px, double py)
    {   x = px; y = py; }
    
     double get_x() const
     {  return x;   }
 
     double get_y()
     {  return y;   }
};         

class Shape 
{
protected:
    double  width, height;
public:
    Shape ()                    //  Default constructor
    {   width = 0; height = 0;  }
    
    Shape (double w, double h)
    {   width = w; height = h;  }
    virtual double area() = 0;  //  Pure virtual
    virtual double circumference () = 0;
};

class Circle: public Shape 
{   Point   center;
    double  radius;
    
public:
    Circle (Point p, double r)
    {   center = p; radius = r; }

    void setRadius (double r)
    {   radius = r;     }
    
    double getRadius() const
    {   return radius;
    }
    
    double circumference()
    {   return 2*radius*3.14159;
    }
};

int main () 
{     
    return 0;
}






Awesome, that clears things up. Especially in the Shape class with the virtual functions. Now i'm working on the main function trying to display the circumference and radius for a circle but i'm getting errors. When writing it out,

1
2
3
4
5
6
7
8
int main()
{
Point (x,y); //Do I put the values here?

cout << "The Circle's Circumference is: " << double circumference() << endl; //Not understanding which one to output. Care to clarify?

return 0;
}
Line 3: Yes, you put values there for x and y. Point also needs to be named.
 
  Point p (2,3);


Line 5: Get rid of the keyword double.

You haven't instantiated a Circle.

1
2
//  Line 4
  Circle c (p, 5);  //  Using p from line 3 and a radius of 5. 


Line 5: You need to cout the circumference of c.
 
  cout << c.circumference() << endl;


Last edited on
I did the changes accordingly and I am getting an error

1
2
3
4
5
6
7
int main()
{
   Point p (4,6);
   Circle c (p,5); // Error here: cannot declare variable 'c' to be of abstract type 'Circle'
   cout << "The circle's circumference is: " << c.circumference() <<endl;
   return 0;
}


I went and double checked everything and can't find why?
What compiler are you using, because when I ran your code (GCC 4.9.2) I also got some additional error messages that helped identify the problem:
1
2
3
 error: cannot declare variable 'c' to be of abstract type 'Circle'
  because the following virtual functions are pure within 'Circle':|
    virtual double Shape::area()|


So you need to define area() for Circle, otherwise Circle inherits area() from Shape which is a pure virtual function and hence Circle is also deemed to be an abstract class. Some other points:
1. get_y(), circumference(), area() should be const qualified
2. pi should be a const global variable instead of handwritten every time
3. Shape needs a virtual destructor http://stackoverflow.com/questions/270917/why-should-i-declare-a-virtual-destructor-for-an-abstract-class-in-c


I'm not sure I completely follow. This is the new code I have

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
#include <iostream>
#include <cmath>

using namespace std;
// Class to represent points.
class Point
{   double x, y;
public:
    Point ()        //  Default constructor
    {   x = 0; y = 0;   }

    Point (double px, double py)
    {   x = px;  y = py;    }

    friend ostream & operator << (ostream &os, const Point &p);
    Point &operator+(const Point&rhs);
    Point &operator-(const Point&rhs);

    void setPoint(double px, double py)
    {   x = px; y = py; }

     double get_x() const
     {  return x;   }

     double get_y() const
     {  return y;   }
};

class Shape
{
protected:
    double  width, height;
public:
    Shape ()                    //  Default constructor
    {   width = 0; height = 0;  }

    Shape (double w, double h)
    {   width = w; height = h;  }
    virtual double area() const = 0;  //  Pure virtual
    virtual double circumference () const = 0;
};
class Circle: public Shape
{   Point   center;
    double  radius;

public:
    Circle (Point p, double r)
    {   center = p; radius = r; }

    void setRadius (double r)
    {   radius = r;     }

    double getRadius() const
    {   return radius;
    }

    double circumference()
    {   return 2*radius*3.14159;
    }
    double area()
    {   return 3.14159*(radius*radius);
    }
};

int main()
{
   Point p (4,6);
   Circle c (p,5);
   cout << "The circle's circumference is: " << c.circumference() <<endl;
   return 0;
}
Line 57,60: area() and circumference() are const in Shape, but are not const in Circle. The signatures must match for the functions in Circle to overload those in Shape.

Last edited on
Thank you! It finally compiles now, and I checked and all of the numbers are correct! Now i'm working on the square function. I have one question about the last one, what is the "c" variable and where does it come from? Let's say I still use the center point, can I use "c" again, if not, what do I use? I just don't know what that "c" is
c is the Circle object that you created on the line above.
 
Circle c (p,5);
Topic archived. No new replies allowed.