圆柱体类Volume的数据成员包括:私有成员Radius(半径)、Height(高)、Name(名称);公有成员函数如下: void SetRadius(int r) //设置圆柱体半径为r void SetHeight(int h) //设置圆柱体高度为hvoid SetName(string mm) //设置圆柱体名称为mm int GetRadius( ) //获取圆柱体半径 int GetHeight( ) //获取圆柱体高度 double GetVolume( ) //获取圆柱体体积 double GetSphere( ) //获取圆柱体表面积 void DispInfo( ) //显示圆柱体的基本信息 Voulme( ) //不带任何参数的构造函数 Volume(int h, int r, string name) //构造函数,初始化圆柱体的高、半径及名字~Volume( ) //析构函数,显示提示信息如“**圆柱体消失了”要求:定义Volume类,在main函数中利用该类定义3个圆柱体,其中一个圆柱体用带参数的构造函数产生,另外两个圆柱体的名称、半径和高由键盘输入。计算这三个圆柱体的体积和表面积;最后输出三个圆柱体的基本信息。
#include <iostream>
#include <string>
using namespace std;
class Volume
{
private:
int Radius;
int Height;
string Name;
public:
Volume(int h,int r, string name);
Volume();
void SetRadius(int r)
{ Radius = r;}
void SetHeight(int h)
{ Height = h;}
void SetName(string mm)
{ Name = mm;}
int GetRadius()
{ return Radius;}
int GetHeight()
{ return Height;}
double GetVolume()
{ return 3.14*Radius*Radius*Height;}
double GetSphere()
{ return 2*3.14*Radius*Radius+2*3.14*Radius*Height;}
void DispInfo()
{
cout<< "**************************" <<endl;
cout<<"圆柱体的名称是:"<< Name<<endl;
cout<<"其高是:"<< Height<<endl;
cout<<"其半径是:"<< Radius<<endl;
cout<<"其体积是:"<< GetVolume()<<endl;
cout<<"其表面积是:"<<GetSphere()<<endl;
}
~Volume()
{ cout << Name << "圆柱体消失了" << endl;}
};
Volume::Volume(int h, int r, string name)
{
Radius = r;
Height = h;
string copy (Name,name);
}
int main()
{
int r1,r2,h1,h2;
string n1,n2;
cout<<"请输入第一个圆柱体的名称、半径、高:";
cin>>n1>>r1>>h1;
cout<<"请输入第二个圆柱体的名称、半径、高:";
cin>>n2>>r2>>h2;
Volume A(h1,r1,n1);
A.GetHeight();
A.GetRadius();
A.GetSphere();
A.GetVolume();
A.DispInfo();
return 0;
}