計算5個長方柱的體積和表面積。
#include <iostream>
using namespace std;
class Box
{
public:
Box();
Box(int h,int w,int l):height(h),width(w),length(l){};
int volume();
int area();
void input();
private:
int height;
int width;
int length;
};
int Box:: volume()
{
return(height*width*length);
}
int Box::area()
{
int s;
s=height*width+width*length+height*length;
return (2*s);
}
Box::Box()
{
height=10;
width=10;
length=10;
}
void Box::input()
{
cin>>height>>width>>length;
}
int main() {
Box a[5]={
Box(10,12,15),
Box(15,18,20),
Box(16,20,26),
Box(),
Box()
};
cout<<"長方柱1的面積是:"<<a[0].area()<<'\t'<<"體積是:"<<a[0].volume()<<endl<<endl;
cout<<"長方柱2的面積是:"<<a[1].area()<<'\t'<<"體積是:"<<a[1].volume()<<endl<<endl;
cout<<"長方柱3的面積是:"<<a[2].area()<<'\t'<<"體積是:"<<a[2].volume()<<endl<<endl;
cout<<"長方柱4的面積是:"<<a[3].area()<<'\t'<<"體積是:"<<a[3].volume()<<endl<<endl;
cout<<"請輸入長方柱5的長,寬,高:";
a[4].input();
cout<<endl;
cout<<"長方柱5的面積是:"<<a[4].area()<<'\t'<<"體積是:"<<a[4].volume()<<endl<<endl;
return 0;
}
上級感言:網速真的真的很欠佳啊!!發了很多遍了。。。