您的位置:首页 > 其它

第五周实验报告(三)

2012-03-22 19:40 239 查看
【任务3】编写基于对象的程序,求5个长方柱的体积和表面积。长方柱类的数据成员包括长(length)、宽(width)、高(heigth)等。另外:

(1) 需要定义长方柱类,5个长方柱采用一个对象数组表示;

(2) 前4个长方柱(即数组的前4个元素)要在定义数组时初始化,其中前3个直接给出参数初始化,第4个用默认构造函数初始化;第5个长方柱定义时不初始化,而是由键盘输入长、宽、高赋值;

(3) 输出这5个长方柱的体积和表面积;

#include <iostream>

using namespace std;

class BOX
{
private:
float length;
float width;
float high;
public:
BOX(float h=10,float w=12,float len=15):high(h),width(w),length(len){}

void volume();
void s();
void set_data();
};

int main()
{
BOX a[5] = {
BOX(11,11,11),
BOX(12,12,12),
BOX(13,13,13),
};
cout<<"输入第4个长方体的长宽高。"  ;
a[3].set_data();
for(int i=0;i<5;i++)
{
cout<<"第"<<i+1<<"个长方体的面积,体积为";
a[i].volume();
a[i].s();
cout<<endl;
}
system("pause");
return 0;
}

void BOX::set_data()
{
cin>>length;
cin>>width;
cin>>high;
}
void BOX::volume()
{
cout<<(length*width*high)<<"        ";
}
void BOX::s()
{
cout<< (2*(length*width+length*high+width*length));
}




迟来的报告3.早就生出来了。就是上不了户口。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  float system 任务