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;
}
本文介绍了一个简单的C++ Cylinder类实现。该类包含半径、高和体积三个私有成员变量,并提供输入半径和高的成员函数以及计算并显示体积的成员函数。通过实例化Cylinder对象并调用相应函数,可以计算出圆柱体的体积。
1万+

被折叠的 条评论
为什么被折叠?



