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!