定义一个名为CCircle的圆类,要求: 1.其属性数据为圆的半径radius; 2.定义构造函数; 3.成员函数area()计算圆的面积。 4.编写主函数计算一个内径和外径分别为a和b的圆环的面积,其中a和b由键盘输入,π取值为3.14159。 写出完整的类的定义和实现。用c++
时间: 2023-05-11 13:02:32 浏览: 127
以下是CCircle类的定义和实现:
```c
#include <iostream>
using namespace std;
class CCircle {
private:
double radius;
public:
CCircle(double r) {
radius = r;
}
double area() {
return 3.14159 * radius * radius;
}
};
int main() {
double a, b;
cout << "请输入内径和外径:";
cin >> a >> b;
CCircle inner(a / 2), outer(b / 2);
double area = outer.area() - inner.area();
cout << "圆环面积为:" << area << endl;
return 0;
}
```
在主函数中,我们先通过键盘输入内径和外径,然后分别创建内圆和外圆的对象,计算出圆环面积并输出。注意,我们在创建内圆和外圆的时候,需要将输入的内径和外径除以2,得到半径再传入构造函数中。
相关问题
义一个名为CCircle的圆类,要求: 1.其属性数据为圆的半径radius; 2.定义构造函数; 3.成员函数area()计算圆的面积。 4.编写主函数计算一个内径和外径分别为a和b的圆环的面积,其中a和b由键盘输入,π取值为3.14159。 写出完整的类的定义和实现。
以下是CCircle的圆类的定义和实现:
```cpp
#include <iostream>
using namespace std;
class CCircle {
private:
double radius;
public:
CCircle(double r = 0) : radius(r) {}
double area() {
return 3.14159 * radius * radius;
}
};
int main() {
double a, b;
cout << "请输入内径和外径:";
cin >> a >> b;
CCircle inner(a / 2), outer(b / 2);
double area = outer.area() - inner.area();
cout << "圆环面积为:" << area << endl;
return 0;
}
```
在主函数中,我们首先从键盘输入内径和外径,然后分别用这两个值创建内圆和外圆的对象。接着,我们计算出圆环的面积,即外圆面积减去内圆面积。最后,输出圆环面积。
定义点CPoint类作为基类,在此基础上派生出直线CLine类和圆CCircle类,并要求基类和各派生类具有以下特点: a.CLine类含有计算直线长度和斜率的成员函数; b.CCircle类含有计算圆面积的成员函数。
以下是基类CPoint的定义:
```C++
class CPoint {
protected:
double x, y;
public:
CPoint(double _x = 0, double _y = 0) : x(_x), y(_y) {}
void Set(double _x, double _y) { x = _x, y = _y; }
double GetX() const { return x; }
double GetY() const { return y; }
};
```
以下是直线CLine类的定义:
```C++
class CLine : public CPoint {
protected:
double len;
public:
CLine(double _x1, double _y1, double _x2, double _y2) : CPoint(_x1, _y1) {
len = sqrt((x - _x2) * (x - _x2) + (y - _y2) * (y - _y2));
}
double GetLen() const { return len; }
double GetSlope() const { return (x != 0) ? (y / x) : INFINITY; }
};
```
以下是圆CCircle类的定义:
```C++
class CCircle : public CPoint {
protected:
double r;
public:
CCircle(double _x, double _y, double _r) : CPoint(_x, _y), r(_r) {}
double GetArea() const { return M_PI * r * r; }
};
```
注意,这里使用了数学库中的常量M_PI,它表示圆周率π。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)