用C++写一个求3个长方柱的体积。

来源:百度知道 编辑:UC知道 时间:2024/06/16 17:31:28
需要求3个长方柱的体积,编写一个基于对象的程序。数据成员包括length(长)、width(宽)、height(高)。要求用成员函数实现以下功能。
(1)由键盘分别输入3个长方柱的长宽高;
(2)计算长方柱的体积;
(3)输出3个长方柱的体积。

#include <iostream.h>
class cube
{
public:
void set();//构造函数
float com_volume();//计算体积
void disp();//输出体积
private:
float length;
float width;
float height;
};
void cube::set()
{
float l,float w,float h;
cout<<"下面进入长方柱的参数设置,单位:立方米"<<endl;
cout<<"请输入长方柱的长:";
cin>>l;
length=l;
cout<<"请输入长方柱的宽:";
cin>>w;
width=w;
cout<<"请输入长方柱的高:";
cin>>h;
height=h;
}
float cube::com_volume()
{
return length*width*height;
}
void cube::disp()
{
cout<<"此长方柱的体积为:"<<com_volume()<<"立方米"<<endl;
}
int main()
{
cube c[3];

for(int i=0;i<3;i++)
{
c[i].set() ;
c[i].disp();
cout<<endl;
}

return 0;
}

楼上的