设计一个组合类Line,其数据成员有point类定义的对象start与end,成员函数包括构造函数、析构函数、求线段对象长度、线段平移等
时间: 2024-05-31 13:08:22 浏览: 77
class Point {
private:
double x, y;
public:
Point(double x = 0, double y = 0) : x(x), y(y) {}
double getX() const { return x; }
double getY() const { return y; }
};
class Line {
private:
Point start, end;
public:
Line(const Point& start, const Point& end) : start(start), end(end) {}
~Line() {}
double length() const {
double dx = start.getX() - end.getX();
double dy = start.getY() - end.getY();
return sqrt(dx * dx + dy * dy);
}
void translate(double dx, double dy) {
start = Point(start.getX() + dx, start.getY() + dy);
end = Point(end.getX() + dx, end.getY() + dy);
}
};
阅读全文