Inheritance - Point, Shape, Derived Classes

Hey guys

So I am having trouble with a program. I keep trying to do this program, but start feeling overwhelmed and get lost. I am getting lost at the beginning of the program, creating a Point class and doing some operating overloading. My code could be completely wrong (new to programming), so any suggestions to make it better/actually work would be greatly appreciated.

Here is the question:
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).

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

#define Pi 3.14159

//Part A

class Point {
	int x;
	int y;
	
	public:
		Point() { };                           //Default Constructor
		Point(int px, int py) {                //Constructor
			x = px;
			y = py;
		}
		
		void get_point() {
		cout << "X Value: " << x << endl;
		cout << "Y Value: " << y << endl;
		}
		
		friend istream &operator>>(istream &stream, Point &o); // Declare >> overload
	 	friend Point operator+(Point x1, Point x2); // Declare + overload, x
	 	friend Point operator-(Point x1, Point x2); // Declare - overload, x
	 	friend Point operator+(Point y1, Point y2); // Declare + overload, y
	 	friend Point operator-(Point y1, Point y2); // Declare - overload, y
};

// >> Operator Overload, Works
istream &operator>>(istream &stream, Point &p) {
	cout << "X: ";
	stream >> p.x;
	cout << "Y: ";
	stream >> p.y;
	return stream;
}

// + Operator Overload X, Doesn't Work
Point operator+(Point x1, Point x2) {
		Point addx = x1 + x2;
}

// - Operator Overload X, Doesn't Work
Point operator-(Point x1, Point x2) {
		Point addx = x1 - x2;
}

// + Operator Overload Y, Doesn't Work
Point operator+(Point y1, Point y2) {
		Point addx = y1 + y2;
}

// - Operator Overload Y, Doesn't Work
Point operator-(Point y1, Point y2) {
		Point addx = y1 - y2;
}


And here are the following errors:

overload2.cpp:28:45: warning: ‘Point operator+(Point, Point)’ is already a friend of class ‘Point’ [enabled by default]
friend Point operator+(Point y1, Point y2);
^
overload2.cpp:29:45: warning: ‘Point operator-(Point, Point)’ is already a friend of class ‘Point’ [enabled by default]
friend Point operator-(Point y1, Point y2);
^
overload2.cpp: In function ‘Point operator+(Point, Point)’:
overload2.cpp:52:7: error: redefinition of ‘Point operator+(Point, Point)’
Point operator+(Point y1, Point y2) {
^
overload2.cpp:42:7: error: ‘Point operator+(Point, Point)’ previously defined here
Point operator+(Point x1, Point x2) {
^
overload2.cpp: In function ‘Point operator-(Point, Point)’:
overload2.cpp:57:7: error: redefinition of ‘Point operator-(Point, Point)’
Point operator-(Point y1, Point y2) {
^
overload2.cpp:47:7: error: ‘Point operator-(Point, Point)’ previously defined here
Point operator-(Point x1, Point x2) {

I understand why I am getting the errors, such as Point operator+ already being overloaded when given two Point arguments, therefore leading to redefinition when doing it with y. But I don't know how to actually satisfy what they are asking for in the question without getting these errors. Please help!
1
2
3
4
	 	friend Point operator+(Point x1, Point x2); // Declare + overload, x
	 	friend Point operator-(Point x1, Point x2); // Declare - overload, x
	 	friend Point operator+(Point y1, Point y2); // Declare + overload, y
	 	friend Point operator-(Point y1, Point y2); // Declare - overload, y 
those don't need to be friends they already have access to everything in the class. The reason you have to have friend istream &operator>>(istream &stream, Point &o); friend inheritance is because istream normally does not have access to the private members.

You have another problem too the left value will be *this so you only need to pass the right hand as a parameter. So operator + for example would look like(I prefer const reference also but you can do without if you wish):

Point operator+( const Point &rhs );

The last problem is since it is supposed to be a member function and not a friend you are going to want to scope to the class so they should be Point Point::operator....

*Another thing to mention you never return in your operators. You must return the new point.

Here are some links that may help you too:

http://www.cprogramming.com/tutorial/operator_overloading.html
http://en.cppreference.com/w/cpp/language/operators
http://www.learncpp.com/ --chapter 9
http://www.cplusplus.com/doc/tutorial/templates/
Last edited on
Hey giblit,

So I fixed the friend problem, that was embarrassing...
I also fixed the argument of the Point operator+, and that makes sense. Thanks so much for the links, they really helped.
Again, embarrassing with the scope operator problem...
But I am getting two errors, surprisingly, that have nothing to do with the operator overloading I've never seen them before.

Here is the updated code (Please comment if you see anything else embarrassing):
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 <cmath>
using namespace std;

#define Pi 3.14159

//Part A

class Point {
	int x;
	int y;
	
	public:
		int px = x;
		int py = y;
		
		void get_point() {
		cout << "X Value: " << x << endl;
		cout << "Y Value: "	<< y << endl;
		}
		
		friend istream &operator>>(istream &stream, Point &o);
	 	Point operator+(const Point& pt);
	 	Point operator-(const Point& pt);
};

// >> Operator Overload     Works.
istream &operator>>(istream &stream, Point &p) {
	cout << "X: ";
	stream >> p.x;
	cout << "Y: ";
	stream >> p.y;
	return stream;
}

// + Operator Overload X
Point Point::operator+(const Point& pt) {
		Point result = *this;
		result.px =  result.px + pt.px;
		result.py = result.py + pt.py;
		return result;
}

int main()
{
	Point pt1, pt2, pt3, pt4;
	
	pt1.px = -11;
	pt1.py = 14;
	
	pt2.px = -11;
	pt2.py = -11;
	
	pt3.px = 14;
	pt3.py = 14;
	
	pt4.px = 14;
	pt4.py = -11;
	
	return 0;
}


Errors:
overload2.cpp:14:12: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
int px = x;
^
overload2.cpp:15:12: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
int py = y;

Any idea what that means?
Another question, I took out the default constructor and constructor from the point class. Is that a bad thing? How do you know when you need a constructor or not? I took it out because it gave me a ton of errors.
1
2
3
4
	 	friend Point operator+(Point x1, Point x2); // Declare + overload, x
	 	friend Point operator-(Point x1, Point x2); // Declare - overload, x
	 	friend Point operator+(Point y1, Point y2); // Declare + overload, y
	 	friend Point operator-(Point y1, Point y2); // Declare - overload, y 


operator+ has the same signature in lines 1 and 3. operator- has the same signature in lines 2 and 4. Giving parameters different names has no effect on the signature of the function (and, in fact, parameter names are not even required when declaring a function.)


giblit wrote:
those don't need to be friends they already have access to everything in the class.

As declared, they do need to be friends. They are not members of the class, and the class doesn't provide enough functionality in the public interface for them to work without being friends.


Last edited on
1
2
		int px = x;
		int py = y;
what are you trying to do here?

You should either use x and y or px and py as your variables. same with
1
2
		result.px =  result.px + pt.px;
		result.py = result.py + pt.py;


As declared, they do need to be friends. They are not members of the class, and the class doesn't provide enough functionality in the public interface for them to work without being friends.

Yeah, I was trying to get him to declare them as member functions I guess I should have been a little more clearer.

*edit when i asked what you were trying to do. I see what you were trying to do. You were trying to make them a sort of setter. Why not use a constructor?

Point( int px , int py ) : x(px) , y(py){}

http://www.cplusplus.com/doc/tutorial/classes/
Last edited on
I was trying to use px and py for hidden implementation, so that the client programmer can't get direct access to x and y? Is that not a good idea?
should I still implement the...

 
Point( int px , int py ) : x(px) , y(py){}


as you suggested? And if so, how does that affect my...
1
2
		result.px =  result.px + pt.px;
		result.py = result.py + pt.py;
Well you would use your x and y variables when adding the points. That is a basic constructor so say you want to create a point 0 , 50 you it would look like:
Point point1( 0 , 50 ); instead of
1
2
3
Point point1;
point1.x = 0;
point1.y = 50;


What you are trying to do is use x and y as public variables while they are private.
Yeah, I like that format MUCH better.
I think I implemented what you suggested correctly. Anything look wrong?
And how would you suggest testing this? I tried just ding a simple cout statment, but it said it can't convert type Point to and unsigned char*

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
#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){}
		
		void get_point() {
		cout << "X Value: " << x << endl;
		cout << "Y Value: "	<< y << endl;
		}
		
		friend istream &operator>>(istream &stream, Point &o);
	 	Point operator+(const Point& pt);
	 	Point operator-(const Point& pt);
};

// >> Operator Overload     Works.
istream &operator>>(istream &stream, Point &p) {
	cout << "X: ";
	stream >> p.x;
	cout << "Y: ";
	stream >> p.y;
	return stream;
}

// + Operator Overload X
Point Point::operator+(const Point& pt) {
		Point result = *this;
		result.x = result.x + pt.x;
		result.y = result.y + pt.y;
		return result;
}

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
	return 0;
}
Just overload the operator << to output.

something like:

1
2
3
4
5
6
7
8
9
10
11
//remember needs to be a friend to access the private x and y variables
ostream &operator<<( ostream &stm, const Point &p )
{
    return stm << x << ',' << y; //if it is 0 and 50 it will ouput -- 0,50
}

//somewhere in main

Point pt1( 0 , 50 );

cout << pt1 << endl;


One more thing to mention there are compound operators.

 
result.x = result.x + pt.x;
is the same as result.x += pt.x;

Also I don't actually see you using the math library but you have included it.
Last edited on
giblit... my good sir...
You. Are. Incredible! Thank you! It works great!
You are both a gentleman and a scholar.

I included the compound operators, forgot about those!
And the cmath part is for part b... There are four parts, but part a was giving me the most trouble.
In part b, I have to create a shape class, and make functions for calculating area, perimeter/circumference, a bounding box and a display. (Using abs)
In part c, I have to use inheritance to derive a triangle, square and circle class and use default constructors and argument driven constructors.
In part d, I have to instantiate a square of side length 25, triangle of side length 10, 20, 30 (impossible, but have to none-the-less) and a circle of radius 23, and display their information.

Thank you once again for all your help! If you want to stick through the program to the end, stay posted! I am sure to be on here posting more questions!
Topic archived. No new replies allowed.