void display(){cout<< “(”<< xcoord <<”, ”<<ycoord<<”)”<<endl; } friend Point operator++(Point &); //前缀自增 friend Point operator++(Point &, int); //后缀自增 };
Point operator++( Point &e) { return Point (++e. xcoord, ++e. ycoord); } Point operator++( Point &e, int) { return Point (e. xcoord ++, e. ycoord ++); }
void main() { Point p 1(10, 10), p 2; p 2=p 1++; p 1. display(); p 2=++p 1; p 1. display(); p 2. display(); }
【例8. 3】加法运算符重载 Point: : operator+(Point a) { Point p; //临时对象 p. xcoord= xcoord+ a. xcoord ; p. ycoord= ycoord+ a. ycoord ; return p; //返回临时对象 }