现在有一个类,该类有圆心坐标(x,y)和半径r,成员数据采用私有访问权限,同时该类有两个共有成员函数,分别用来设置成员数据和输出成员数据,在主函数中分别采用指针方式和引用方式来调用该类的公有函数设置和输出成员数据。
时间: 2023-05-16 18:05:26 浏览: 60
好的,这是一个关于类的问题。这个类有圆心坐标(x,y)和半径r,成员数据采用私有访问权限,同时该类有两个共有成员函数,分别用来设置成员数据和输出成员数据。在主函数中,我们可以采用指针方式和引用方式来调用该类的公有函数设置和输出成员数据。下面是代码示例:
```cpp
#include <iostream>
using namespace std;
class Circle {
private:
double x, y, r;
public:
void set(double x, double y, double r) {
this->x = x;
this->y = y;
this->r = r;
}
void output() {
cout << "圆心坐标为(" << x << ", " << y << "), 半径为" << r << endl;
}
};
int main() {
Circle c;
Circle *p = &c;
Circle &q = c;
p->set(1, 2, 3);
p->output();
q.set(4, 5, 6);
q.output();
return 0;
}
```
这个类的成员数据采用私有访问权限,因此我们需要使用公有成员函数来设置和输出成员数据。在主函数中,我们定义了一个 Circle 类型的对象 c,以及一个指向 c 的指针 p 和一个引用 q。我们可以通过指针方式和引用方式来调用该类的公有函数设置和输出成员数据。
相关推荐
![](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)
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)