Overloading operators
I have an assignment where I have to add 2 coordinate points by overloading + and * operators.
I have an issue where after I input points for x,y for object A, it doesn't ask me to input points for B it just goes straight to the addition.
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
|
#include <iostream>
using namespace std;
class POINTS
{
private: int x, y; //coordinates
public:
POINTS();
void ReadCoords();
friend POINTS operator + (POINTS A, POINTS B);
void DisplayResult();
~POINTS();
};
POINTS::POINTS()
{
x = 0; y = 0;
}
void POINTS::ReadCoords()
{
cin >> x, y;
}
POINTS operator + (POINTS A, POINTS B)
{
POINTS R; //object of the result
R.x = A.x + B.x;
R.y = A.y + B.y;
return R;
}
void POINTS::DisplayResult()
{
cout << "The addition of coordinates A and B are (" << x << "," << y << ")" << endl;
}
POINTS::~POINTS()
{
}
int main()
{
POINTS A, B, C;
cout << "Enter the coordinates for point A: " << endl;
A.ReadCoords();
cout << "Enter the coordinates for point B: " << endl;
B.ReadCoords();
C = A + B;
C.DisplayResult();
system("PAUSE");
return 0;
}
|
line 22,
cin >> x >> y;
line 36, you dont write
"The addition of coordinates A and B are (" |
because, it may display other objects, e.g. A.DisplayResult();
overload by member function for same class members.
Can you elaborate a bit more? I still don't see what the issue is
Topic archived. No new replies allowed.