- * All rights reserved,
- * 文件名称:用对象数组操作长方柱类.Cpp
- * 作者:刘默涵
- * 完成日期:2016年4月21日
- * 版本号:vc++6.0
- *
- * 问题描述:基于对象的程序,求五个长方柱的体积及其表面积。
- */
#include<iostream>
using namespace std;
class Bulk
{
public:
Bulk(double x=1.0,double y=1.0,double z=1.0);
void get_value();
double value();
double area();
private:
double length;
double width;
double heigth;
};
Bulk::Bulk(double l,double w,double h)
{
length=l,width=w,heigth=h;
}
void Bulk::get_value()
{
cin>>length>>width>>heigth;
}
double Bulk::value()
{
return width*length*heigth;
}
double Bulk::area()
{
return 2*(width*length+length*heigth+heigth*width) ;
}
int main()
{
int i;
Bulk b[5]={Bulk(2.3,4.5,6.7),Bulk(1.5,3.4),Bulk(10.5)};
cout<<"Please input the length,width and height:";
b[4].get_value();
cout<<"The volume is:";
for(i=0;i<5;i++)
cout<<b[i].value()<<" ";
cout<<endl;
cout<<"The area is:";
for(i=0;i<5;i++)
cout<<b[i].area()<<" ";
cout<<endl;
return 0;
}