#include<iostream>
using namespace std;
class Box
{
public:
Box(int h=1,int w=2,int len=3):height(h),width(w),length(len){}
void set_box();
void show_box();
int volume();
int area();
private:
int height;
int width;
int length;
};
int Box::volume()
{
return (height*width*length);
}
int Box::area()
{
return ((height*width+length*width+height*length)*2);
}
void Box::set_box()
{
cin>>length;
cin>>height;
cin>>width;
}
void Box::show_box()
{
cout<<"volume is"<<volume()<<" "<<"area is"<<area()<<endl;
}
int main()
{
Box a[5]={
Box(10,20,30),
Box(10,12,15),
Box(16,20,26),
Box()
};
a[4].set_box();
for(int i=0;i<=4;i++)
{
cout << "a[" << i <<"]:";
a[i].show_box();
}
return 0;
}
for(int i=0;i<=4;i++)
{cout << "a[" << i <<"]:";
a[i].show_box();}
在输出时这样建立一个循环并调用函数,非常方便。在今后输出的内容多时,要学会使用。图片传不上啊,贺哥谅解!~~