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
| #include<iostream> using namespace std; struct Point { int x, y; Point() :x(0), y(0) {} Point(int _x, int _y) :x(_x), y(_y) {} Point operator+(Point& p) { Point result; result.x = this->x + p.x; result.y = this->y + p.y; return result; } friend int operator*(Point &p1, Point &p2); //实现内积 Point operator++(int pos)//有参数后置 { return Point(this->x++, this->y++); } Point operator++()//无参数前置 { return Point(++this->x, ++this->y); } }; int operator*(Point &p1, Point &p2) { return (p1.x*p2.x) + (p1.y*p2.y); } 输出: 20 p3:(3,9) p4:1 p4:(2,3) p4:2
|