用c++定义一个圆柱体类Cylinder,类内数据成员有radius(圆柱体底面半径)、height(圆柱体高度),成员函数有构造函数、surface和volume。surface函数实现计算圆柱体的表面积,volume实现计算圆柱体的体积。在main函数中定义Cylinder类的对象c,并调用surface函数计算对象c的表面积,调用volume计算对象c的体积。
时间: 2023-08-21 07:02:37 浏览: 121
C++定义类计算圆柱体体积
好的,这里是实现代码:
```c++
#include <iostream>
using namespace std;
class Cylinder {
private:
double radius;
double height;
public:
Cylinder(double r, double h) {
radius = r;
height = h;
}
double surface() {
return 2 * 3.14 * radius * (radius + height);
}
double volume() {
return 3.14 * radius * radius * height;
}
};
int main() {
Cylinder c(5, 10);
cout << "The surface area of cylinder is: " << c.surface() << endl;
cout << "The volume of cylinder is: " << c.volume() << endl;
return 0;
}
```
运行结果:
```
The surface area of cylinder is: 471
The volume of cylinder is: 785
```
阅读全文