代码:
#include <iostream>
using namespace std;
class Bulk
{
private:
double length;
double width;
double heigth;
public:
Bulk(double x=1.0,double y=1.0,double z=1.0):length(x),width(y),heigth(z){};
void get_value();
void output();
};
void Bulk::get_value()
{
cin>>length>>width>>heigth;
}
void Bulk::output()
{
cout<<"体积为:"<<length*width*heigth<<' ';
cout<<"表面积为:"<<(length*width+length*heigth+width*heigth)/2<<endl;
}
int main()
{
Bulk b[5]={Bulk(2.3,4.5,6.7),Bulk(1.5,3.4),Bulk(10.5)};
b[4].get_value();
int i=0;
for(i=0;i<5;i++)
{
cout<<"第"<<i+1<<"个长方柱的";
b[i].output();
}
return 0;
}
运行结果: