Hello! I need to write a program to overload +,-,*,++, and - operators to add two points, subtract two points, multiply a point by a constant k, auto increment and auto decrement the coordinates of a point by one. I already have the distance of the two points but I am having trouble making the functions to add the two points. How would I do this? This is what I have so far. Thank you!

Sample I/O:
Enter the coordinates of point A: 2 3
Enter the coordinates of point B: 5 7
A(2,3) + B(5,7) = C(7,10)

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include using namespace std; class POINT {private: int x,y; public: void read (char vertex); friend void Display (POINT p, POINT q); friend float Distance (POINT p, POINT q); friend float Add (POINT p, POINT q); friend void Display (float AB); }; int main () { POINT A,B; A.read('A'); B.read('B'); float Sum = Add(A,B); cout <> x >> y; } float Add (POINT p, POINT q) { return (p.x+q.x); } float Distance (POINT p, POINT q) { return sqrt(pow(p.x-q.x,2.)+pow(p.y-q.y,2.)); } void Display (float AB) { cout << AB << endl; } void Display (POINT p, POINT q) { cout << "A(" << p.x << "," << p.y << ") + B(" << q.x << "," << q.y << ")= "; cout << "The distance from A(" << p.x << "," << p.y << ") to B(" << q.x << "," << q.y << ") is "; }``````

 ``1234567891011121314151617181920212223242526272829303132`` ``````class POINT { private : int x, y; public : // ... // ... friend POINT operator+ ( const POINT& point, const POINT& point2 ); // ... }; POINT operator+ ( const POINT& point, const POINT& point2 ) { POINT temp; temp.x = point.x + point2.x; temp.y = point.y + point2.y; return temp; } //... // ... int main () { POINT a, b, c; a.read( 'A' ); // say 2 3 b.read( 'B' ); // say 5 7 c = a + b; // will contain 7 10 }``````

EDIT

now, it's up to you how you will display the members of c
Last edited on
A less friendly approach has:
 ``123456`` ``````T operator+ ( const T & lhs, const T & rhs ) { T temp{ lhs }; temp += rhs; // requires T& T::operator+= ( const T& ) return temp; }``````
Topic archived. No new replies allowed.