Point Type Class Getter/Setter Issue

I'm working with two classes (pointType and circleType). Right now, I'm just testing to make sure the functions work properly and it appears something is wrong with the setPoint function and maybe the print function as well.

my apologies for the length
This is pointType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Class pointType

#ifndef H_PointType
#define H_PointType

class pointType
{
public:
    void setPoint(double x, double y);
    void print() const;
    double getX() const;
    double getY() const;
	//pointType(double x = 0.0, double y = 0.0);
	pointType(double , double);
    pointType();

protected:
    double xCoordinate;
    double yCoordinate;
};

#endif



pointTypeImp.cpp
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

#include <iostream>
#include "pointType.h"
#include "circleType.h"
using namespace std;

    void pointType::setPoint(double x, double y)
	{
		xCoordinate = x;
		yCoordinate = y;
	}
	
    void pointType::print() const
	{
		cout << "( " << xCoordinate << ", " << yCoordinate << ")";
	}
	
    double pointType::getX() const
	{
		return xCoordinate;
	}
	
    double pointType::getY() const
	{
		return yCoordinate;
	}
  
	pointType::pointType(double x, double y)
	{
		setPoint(x,y);
	}

	 pointType::pointType()
	{
		xCoordinate = 0.0;
		yCoordinate = 0.0;
	}

circleType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Class circleType

#ifndef H_CircleType
#define H_CircleType

#include "pointType.h"
 
class circleType: public pointType
{
public:
	void print() const;
	void setRadius(double r);
	double getRadius() const;
	double getCircumference() const;
	double getArea() const;
	circleType();

	
protected:
	double radius;
};

#endif

circleTypeImp.cpp
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
#include <iostream>
#include "pointType.h"
#include "circleType.h"
using namespace std;

	void circleType::print() const
	{
		cout << radius << endl;
	}
	
	void circleType::setRadius(double r)
	{
		radius = r;
	}
	
	double circleType::getRadius() const
	{
		return radius;
	}
	
	double circleType::getCircumference() const
	{
		return 2 * radius * 3.14159;
	}
	
	double circleType::getArea() const
	{
		return 3.14159*(radius *radius);
	}
	
	circleType::circleType( )
	{
		radius = 0.0;
	}

and finally main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "pointType.h"
#include "circleType.h"
using namespace std;

int main()
{
	circleType circle;
	double radius, xCoord, yCoord;
	
	cout << "Enter the X and Y coordinates for the center of the circle." << endl;
	cin >> xCoord >> yCoord;
	circle.setPoint(xCoord, yCoord);
	circle.print();
		
	return 0;
}
You give the circle its point and not its radius, meanwhile the circle's print function ignores the point and only displays the radius.
Ooooooooooh lol you're absolutely right. Thank you!
Must... refrain..... from being a dick....


I was just in a huge debate on the style of these kinds of classes. I said that this type of design (with a bunch of useless getters/setters) was terrible, but other people disagreed.

I'm not saying you should change your code, but if you are interested in the discussion, here is a link to it:

http://www.cplusplus.com/forum/beginner/142434/
Declare circle print function virtual.
@Disch: unless you want to use immutable point and circle classes, these classes are perfect candidates for getters and setters. They don't control themselves, they just maintain their state for you. They're utility classes.
You're probably right, but I was given the two header files and told to define and implement them. Thank you for the suggestions. I'm still pretty new to this an I appreciate all the feedback :)
Hi,

Also note that Circle should not inherit from Point. Inheritance implies a "IS A" relationship: a Circle is not a point. More formally it violates the Liscov Substitution Principle (Google that). I learnt that from ne555.

Instead, Circle should have a member variable that is of type Point. This is a "HAS A" type relationship an is also known as aggregation.

The other type of relationship is "USES A", that happens when an object of a particular type is passed as a parameter to to a function.


I agree with Disch about the unnecessary getters / setters. Getters may not be so bad, but public setters mean that everything may as well be public !! This is a common problem for beginners, and is improved with knowledge about initialisation lists in constructors.

Note that the Boost library stores a point as a vector, which is a private member variable. I learnt that from Cubbi.

Hope all goes well :+)
@Disch: unless you want to use immutable point and circle classes, these classes are perfect candidates for getters and setters. They don't control themselves, they just maintain their state for you. They're utility classes.


I'm not going to get into it again. I'll just say that I'm very happy that every point/vector class I've ever used had public x/y/z members.
Have fun debugging mysterious NaN and INF values in your vectors :)
LB wrote:
Have fun debugging mysterious NaN and INF values in your vectors :)


You're mistreating the problem. If NaN/INF are a problem, they're going to be a problem everywhere... not just in your vectors. So why butcher the vector class to address a problem that isn't with the vector class?

If you don't want NaN/INF, then don't use doubles.

1
2
3
4
5
6
7
8
9
class NotQuiteADouble
{
    // a class the behaves like a double, but does not allow NaN/INF
};


sf::Vector<NotQuiteADouble>  foo;

foo.x = 5.0;  // yay!   public!  No risk of NaN! 
Topic archived. No new replies allowed.