第五周实验报告3
2012-03-20 19:49
281 查看
/* Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称: class Box * 作 者: 刘程程 * 完成日期: 2012 年 03 月 20 日 * 版 本 号: 1.0 * 对任务及求解方法的描述部分 * 输入描述: 长方体的长,宽,高 * 问题描述: 计算长方体的面积和体积 * 程序输出: 长方体的长,宽,高,体积,面积 * 程序头部的注释结束 */ #include <iostream> using namespace std; class Bulk { public: Bulk():lengh(1.0),width(1.0),height(1.0){};//默认构造函数 Bulk(double x,double y,double z):lengh(x),width(y),height(z){};//带参数的构造函数 void set_value(); void display(); private: double lengh; double width; double height; }; void Bulk::set_value() { cout<<"please input lengh, width,height:"; cin>>lengh; cin>>width; cin>>height; } void Bulk::display() { cout<<" The volume is: "<<lengh*width*height<<endl; cout<<" The surface area is: "<<2*(lengh*width+lengh*height+width*height)<<endl; cout<<endl; } int main() { Bulk b[5]={Bulk(2.3,4.5,6.7),Bulk(1.5,3.4,9.5),Bulk(10.5,22.3,3.14)};//前三个元素用带参构造函数初始化,后两个用默认构造函数 b[4].set_value(); //注意b[4]是一个对象,调用成员函数的形式b[4].set_value() for(int i=0;i<5;++i) { cout<<"关于b["<<i<<"]"<<endl; b[i].display();//调用对象数组中元素的成员函数 } system("pause"); return 0; }
通过仔细的看书,看程序,能够对对象数组有更深的认识!!!