定义一个point(点)类,公有成员两数至少包括 set point、move point:该类的私有数据成员有点的位置和颜色,调用所有定义的公有函数,输出点的位置和颜色
时间: 2023-05-30 19:07:44 浏览: 64
以下是一个可能的实现:
```cpp
#include <iostream>
using namespace std;
class Point {
public:
Point(int x = 0, int y = 0, string color = "black") {
this->x = x;
this->y = y;
this->color = color;
}
void setPoint(int x, int y) {
this->x = x;
this->y = y;
}
void movePoint(int dx, int dy) {
x += dx;
y += dy;
}
void print() {
cout << "Point: (" << x << ", " << y << "), color: " << color << endl;
}
private:
int x, y;
string color;
};
int main() {
Point p(10, 20, "red");
p.print();
p.setPoint(30, 40);
p.print();
p.movePoint(5, -10);
p.print();
return 0;
}
```
输出:
```
Point: (10, 20), color: red
Point: (30, 40), color: red
Point: (35, 30), color: red
```
阅读全文