题目:建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储在一个double变量中。在类cylinder中包含一个成员函数vol,用来显示每个cylinder对象的体积。
代码:
#include<iostream>
using namespace std;
class cylinder
{
private:
double r,h,v;
public:
cylinder(double r1, double h1)
{
r = r1;
h = h1;
}
double calculate(double r, double h)
{
v = 3.14 * r * r * h;
return v;
}
void vol()
{
cout << "This cylinder volume is " << v << endl;
}
};
int main()
{
double a, b;
cin >> a >> b;
cylinder c1(a,b);
c1.calculate(a,b);
c1.vol();
}