You are on page 1of 2

#include <iostream>

using namespace std;


class Point2D{
private:
float x, y;
public:
Point2D(float a, float b);
Point2D operator+(Point2D p);
friendostream& operator<<(ostream& o, Point2D p);
istream& operator>>(Point2D &p) {
returncin>>p.x>>p.y;
}
bool operator==(Point2D &rhs) {
return (x=rhs.x&& y == rhs.y);
}
};
Point2D::Point2D(float a=0, float b=0) { // default& 2 arguments
constructor
x=a;
y=b;
}
ostream& operator<<(ostream& o, Point2D p) { // OUTPUT Operator
returno<< "( " <<p.x<< " , " <<p.y<< " )";
}
Point2D Point2D::operator+( Point2D p){ // ADD Operator
Point2D tmp;
tmp.x=x+p.x;
tmp.y=y+p.y;
returntmp;
}
int main()
{
Point2D p1(3.0,4.0), p2(1.0,3.0),p3;
cout<< " p3 = " << p3 <<endl;
p3 = p1 + p2;
cout<< "p1 = " << p1 <<endl;
cout<< "p2 = " << p2 <<endl;
cout<< "p3 = " << p3 <<endl;
cout<< "is p1 == p2 ? " << (p1 == p2) <<endl;
cout<< "is p3 == p3 ? " << (p3 == p3) <<endl;
return 0;
}

The out will be as following:

p3 = ( 0 , 0 )
p1 = ( 3 , 4 )
p2 = ( 1 , 3 )
p3 = ( 4 , 7 )
is p1 == p2 ? 0
is p3 == p3 ? 1

You might also like