问题及代码:
#include <iostream>
using namespace std;
class Bulk
{
public:
Bulk():length(1),width(1),high(1){};
Bulk(double l,double w=1,double h=1):length(l),width(w),high(h){};
void output();
void get_value();
double get_area();
double get_volume();
private:
double length,width,high;
};
void Bulk::output()
{
cout<<"volume:"<<get_volume()<<endl;
cout<<"area:"<<get_area()<<endl;
cout<<endl;
}
void Bulk::get_value()
{
double a,b,c;
cin>>a>>b>>c;
length=a;
width=b;
high=c;
}
double Bulk::get_area()
{
return 2*(length*width+length*high+width*high);
}
double Bulk::get_volume()
{
return length*width*high;
}
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;
for(i=0;i<5;i++)
b[i].output();
//下面分别输出这5个长方柱的体积和表面积
}
程序截图:
学习小结:
编完了,这个挺简单的。