好的,以下是一个面向对象C++实现的平面点类:
#include <iostream>
class Point {
public:
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
friend Point operator+(const Point &a, const Point &b) {
return Point(a.x + b.x, a.y + b.y);
}
friend std::ostream &operator<<(std::ostream &os, const Point &p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
};
int main() {
Point a(1, 2), b(3, 4), c;
c = a + b;
std::cout << c << std::endl;
return 0;
}