设计描述坐标点的Cpoint类,私有变量x和y代表一 点的坐标值。编写程序实现:利用构造函数 传递参数, 并设其默认参数值为60和75,利用成员函数display输出这一默认值:利用公有成员函数setpoint将坐标值改力(80,150),利用成员函数diplay 输出修改后的坐标。
时间: 2023-05-19 12:05:10 浏览: 138
好的,我可以回答这个问题。以下是Cpoint类的设计和实现代码:
```c++
class Cpoint {
private:
int x;
int y;
public:
Cpoint(int x = 60, int y = 75) : x(x), y(y) {}
void setpoint(int x, int y) {
this->x = x;
this->y = y;
}
void display() {
std::cout << "x: " << x << ", y: " << y << std::endl;
}
};
```
使用默认参数值创建一个Cpoint对象:
```c++
Cpoint point;
point.display(); // 输出:x: 60, y: 75
```
将坐标值改为(80,150):
```c++
point.setpoint(80, 150);
point.display(); // 输出:x: 80, y: 150
```
希望这个回答对你有帮助。
相关问题
设计描述坐标点的 Cpoint 类,私有变量 x 和 y 代表一个点的坐标值。编写程序实现:利用构造函数传递参数,并设其默认参数值为60和75,利用成员函数 display 输这一默认值;利用公有成员函数 setpoint 将坐标值改为(80.150),利用成员函数 display 输出修改后的坐标值。.
好的,我可以回答这个问题。以下是 Cpoint 类的设计描述和实现代码:
```cpp
// Cpoint 类的设计描述
class Cpoint {
private:
int x;
int y;
public:
// 构造函数,传递参数并设默认值
Cpoint(int x = 60, int y = 75) {
this->x = x;
this->y = y;
}
// 公有成员函数,设置坐标值
void setpoint(int x, int y) {
this->x = x;
this->y = y;
}
// 成员函数,输出坐标值
void display() {
cout << "x = " << x << ", y = " << y << endl;
}
};
```
使用该类的示例代码如下:
```cpp
int main() {
// 创建 Cpoint 对象,使用默认值
Cpoint p1;
p1.display(); // 输出:x = 60, y = 75
// 修改坐标值并输出
p1.setpoint(80, 150);
p1.display(); // 输出:x = 80, y = 150
return 0;
}
```
希望这个回答能够帮到你!
实现一个三维坐标的cpoint类
好的,以下是一个简单的cpoint类的实现,用于表示三维坐标:
```c++
class cpoint {
public:
cpoint() : x(0), y(0), z(0) {}
cpoint(float x, float y, float z) : x(x), y(y), z(z) {}
float getX() const { return x; }
float getY() const { return y; }
float getZ() const { return z; }
void setX(float x) { this->x = x; }
void setY(float y) { this->y = y; }
void setZ(float z) { this->z = z; }
private:
float x, y, z;
};
```
这个类有三个私有成员变量,分别表示x、y、z坐标。构造函数用于初始化这些变量。getX、getY和getZ方法用于获取相应坐标的值。setX、setY和setZ方法用于设置相应坐标的值。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)