C++:建立Cylinder类,有三个double型私有数据成员:半径、高和体积,构造函数传递两个值:半径和高,计算体积。成员函数showVolume()用来显示每个对象的体积。
#include<iostream>
#include<cstring>
#define PI 3.1415926//定义常量PI
using namespace std;
class Cylinder {
double r;
double h;
double vol;
public:
void rhinput()//输入r,h
{
cin >> r >> h;
}
void showVolume()//显示体积
{
vol = PI * r * r * h;
cout<<"The volume of cylinder is :"<<vol<<endl;
}
};
int main()
{
Cylinder v1;
v1.rhinput();
v1.showVolume();
return 0;
}