第五周C++报告三。

 


#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();}

 

在输出时这样建立一个循环并调用函数,非常方便。在今后输出的内容多时,要学会使用。图片传不上啊,贺哥谅解!~~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值