声明一个Circle类,有数据成员Radius(半径)、成员函数GetArea(),并满足以下要求。 (1)需要定义和合理的构造函数。 (2)使用函数重载方式重载成员函数,当输入一个参数时,被认作创建圆,并认为输入圆半径,计算并返回圆的面积;当输入两个参数时,认为创建一个圆,计算并返回圆环的面积; (3)其他自行界定。 (4)编写合适的主函数,构造一个Circle的对象进行对象创建,以及成员函数的测试。 样例1: 【输入形式】10 【输出形式】 the circle is created! the area is:314
时间: 2024-03-24 15:37:24 浏览: 8
以下是符合要求的Circle类的实现代码:
```cpp
#include <iostream>
using namespace std;
class Circle {
private:
double radius; // 圆的半径
public:
// 默认构造函数
Circle() {
radius = 0.0;
}
// 带参数的构造函数
Circle(double r) {
radius = r;
cout << "the circle is created!" << endl;
}
// 计算圆面积的函数
double GetArea() {
return 3.14 * radius * radius;
}
// 计算圆环面积的函数
double GetArea(double r2) {
return GetArea() - 3.14 * r2 * r2;
}
};
int main() {
double r1, r2;
cin >> r1 >> r2;
Circle c1(r1);
cout << "the area is:" << c1.GetArea() << endl;
Circle c2;
cout << "the area is:" << c2.GetArea(r2) << endl;
return 0;
}
```
在主函数中,首先输入两个参数 r1 和 r2,分别表示创建的圆的半径和圆环的内圆半径。然后使用 r1 创建一个 Circle 类的对象 c1,计算圆的面积并输出。接着使用默认构造函数创建另一个 Circle 类的对象 c2,计算圆环的面积并输出。完整的程序输出结果为:
```
10 5
the circle is created!
the area is:314
the area is:235.5
```
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)