Overloading functions, manipulating shape points

I have an assignment at school, and the first portion of it is to overload the << function to print out a defined point, then overload + and - to add and subtract from the point. My add to x works just fine, but for some reason y... doesn't.

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

using namespace std;
 
class Point
{
public:
	double p_x, p_y;
 
	Point(double x=0.0, double y=0.0): p_x(x), p_y(y)
	{
	}
	
	friend ostream& operator<< (ostream &out, const Point &point);
    
	friend Point operator+(const Point &p1, Point &p2);
		
	int getX()
	{
		return p_x;
	}
	
	int getY()
	{
		return p_y;
	}
	
};
 
ostream& operator<< (ostream &out, const Point &point)
{

	out << "Point(" << point.p_x << ", " << point.p_y << ")";
 
	return out;
}

Point operator+(const Point &pX1, Point &pX2)
{
	return Point(pX1.p_x + pX2.p_x);
}

Point operator+(Point &pY1, Point &pY2)
{
	return Point(pY1.p_y + pY2.p_y);
}
 
int main()
{
	setprecision(2);
	Point point1(2.0, 3.5);
	Point point2(6.0, 7.5);
	Point addPointx = point1.getX() + point2.getX();
	Point addPointy = point1.getY() + point2.getY();
 
	cout << point1 << " " << point2 << '\n';
	cout << "Both points added together become" << endl;
	cout << "Point(" << addPointx.getX() << ", " << addPointy.getY() << ")" << '\n';
	return 0;
}


for some reason addPointy.getY() just outputs 0. How do I get it to stop doing this? What have I done wrong?

**EDIT
My p_x and p_y WERE in private, and that is why I have friend functions, but for some reason it kept telling me (even though my x and y code are identical) that just p_y was private, and I couldn't execute. So just to see if it worked I moved it out.
Last edited on
Line 21: why does getX() return an int, when p_x is a double?

Line 24: Ditto for getY().

Line 46: This function is not a friend, so it can't access private members of Point. Note that its signature is different from line 17. More to the Point, why do you have two implementations?
Line 55 is not doing what you expect.

Line 55 is effectively this:
1
2
  Point addPointy = Point (point1.getY() + point2.getY() );
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ first argument to constructor (x)

That invokes Point's constructor with Point(10,0).
When you do the getY() at line 59, you of course, get 0.


Last edited on
Line 46: This function is not a friend, so it can't access private members of Point. Note that its signature is different from line 17. More to the Point, why do you have two implementations?


My instructions said to keep calculations for x and y separate as a hint. I thought by doing this, that is how I was keeping them separate... Not quite sure how else to separate them
I thought I was going to figure out, and attempted this:

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

 using namespace std;
 
class Point
{
public:
	double p_x, p_y;
 
	Point(double x=0.0, double y=0.0): p_x(x), p_y(y)
	{
	}
	
	friend ostream& operator<< (ostream &out, const Point &point);
    
	friend Point operator+(const Point &p1, Point &p2);
	
	friend Point operator-(const Point &p1, Point &p2);
		
	double getX()
	{
		return p_x;
	}
	
	double getY()
	{
		return p_y;
	}
	
};
 
ostream& operator<< (ostream &out, const Point &point)
{

	out << "Point(" << point.p_x << ", " << point.p_y << ")";
 
	return out;
}

Point operator+(const Point &p1, Point &p2)
{
	return Point(p1.p_x + p2.p_x);
	return Point(p1.p_y + p2.p_y);
}

/*Point operator+(Point &pY1, Point &pY2)
{
	return Point(pY1.p_y + pY2.p_y);
}
*/

Point operator-(const Point &p1, Point &p2)
{
	return Point(p1.p_x - p2.p_x);
	return Point(p1.p_y - p2.p_y);
}

/*Point operator-(Point &pY1, Point &pY2)
{
	return Point(pY1.p_y - pY2.p_y);
}
*/
 
int main()
{
	setprecision(2);
	Point point1(2.0, 3.5);
	Point point2(6.0, 7.5);
	Point addPoint_x = point1.getX() + point2.getX();
	Point addPoint_y = point1.getY() + point2.getY();
//	Point subPoint_x = point1.getX() - point2.getX();
//	Point subPoint_y = point1.getY() - point2.getY();
 
	cout << "Point 1: " << point1 << "\n" << "Point 2: " << point2 << '\n';
	cout << "Both points added together become" << endl;
	cout << addPoint_x << addPoint_y << '\n';
//	cout << "Both points subbed together become" << endl;
//	cout << subPoint_x << subPoint_y << '\n';
	return 0;
}


I am at least getting the numbers to add up right, but I really don't understand why it isn't addressing the 2nd point in the addition, and just throws my y calculation in the x spot.
Last edited on
I explained this before (line 55 below is now line 71):
AbstractionAnon wrote:

Line 55 is effectively this:
1
2
  Point addPointy = Point (point1.getY() + point2.getY() );
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ first argument to constructor (x)

That invokes Point's constructor with Point(10,0).
When you do the getY() at line 59, you of course, get 0.


Because the getY values are added together, to form a single value Point's constructor is invoked with ONE argument (X). The constructor for Point defaults the second argument to 0.
Last edited on
Instead of this constructor:
1
2
3
	Point(double x=0.0, double y=0.0): p_x(x), p_y(y)
	{
	}
Create two:
1
2
3
4
5
6
7
	Point(double x, double y): p_x(x), p_y(y)
	{
	}

	Point(): p_x(), p_y()
	{
	}
So either you provide all values or none. Now if you compile with this two constructors you will get compiler errors that may make it more clear what AbstractionAnon is trying to tell you.
closed account (LA48b7Xj)
I would add the points like this...
1
2
3
4
Point operator+(const Point &p1, const Point &p2)
{
    return Point(p1.getX() + p2.getX(), p1.getY() + p2.getY());
}


So there is no need for it to be a friend or member.

For members functions that don't modify data members you should define them as const

1
2
3
4
5
6
7
8
9
int getX() const
{
     return p_x;
}
	
int getY() const
{
	return p_y;
}
Last edited on
AbstractionAnon's 2nd post made it a lot more clear, I am pretty new and I guess fuctions/classes are pretty fuzzy on exactly how to navigate still.

so I made the line

Point addPoints ((point1.getX() + point2.getX()) , (point1.getY() + point2.getY()));

and it works! I might have too many parentheses, I dunno, but they help my brain read it better.
Topic archived. No new replies allowed.