/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 长方柱
* 作 者: 郭岩岩
* 完成日期: 2012年3 月 19日
* 版 本 号:vc.1
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include<iostream>
using namespace std;
class Box
{
private:
int height;
int width;
int length;
public:
Box();
Box(int l,int w,int h):length(l),width(w),height(h){}
int volume();
int Perimeter();
void input_box();
};
Box::Box()
{
height=10;
width=10;
length=10;
}
int Box::volume()
{
return (height*length*width);
}
int Box::Perimeter()
{
int m;
m=2*(height*width+height*length+width*length);
return m;
}
void Box::input_box()
{
int a,b,c;
cout<<"请输入长方体的长、宽、高:";
cin>>a>>b>>c;
cout<<"第"<<5<<"个长方体的面积为:"<<2*(a*b+a*c+b*c)<<"体积为:"<<a*b*c<<endl;
}
int main()
{
int i;
Box B[5]={
Box(10,12,15),
Box(15,18,20),
Box(16,20,26),
Box(),
Box()
};
for(i=0;i<4;i++)
{
cout<<"第"<<i+1<<"个长方体的面积为:"<<B[i].volume()<<"体积为:"<<B[i].Perimeter()<<endl;
}
B[5].input_box();
return 0;
}
上机感言:多和别人交流思想
经验积累:通过上机掌握给数组定义的方式
第五个Box可以在定义一个函数
数组中最后一个元素后赋值时不能加“;”而且两个元素间加“,”