第四周项目三——用对象数组操作长方柱类

来源:互联网 发布:频谱测试仪软件 编辑:程序博客网 时间:2024/04/30 21:48
/* *Copyright  (c)  2014,烟台大学计算机学院  *All rights reserved.  *文件名称: test.cpp *作        者:陈丹  *完成日期:2015年4月1日  *版本号:v1.0  * *问题描述:编写基于对象的程序,求5个长方柱的体积和表面积 *输入描述:  *程序输出: */#include <iostream>using namespace std;class Bulk{private:    double length;    double width;    double height;public:    Bulk(double x=1.0,double y=1.0,double z=1.0):length(x),width(y),height(z){};    void get_value();    void output();};int main(){     Bulk b[5]={Bulk(2.3,4.5,6.7),Bulk(1.5,3.4),Bulk(10.5)};     b[4].get_value();     //下面分别输出这5个长方柱的体积和表面积     for(int i=0;i<5;i++)     {        cout<<'b'<<'['<<i<<']'<<endl;        b[i].output();     }        return 0;}void Bulk::get_value(){    cout<<"输入长宽高:";    cin>>length;    cin>>width;    cin>>height;}void Bulk::output(){    cout<<"体积为:"<<length*width*height<<endl;    cout<<"表面积为:"<<2*(length*width+length*height+width*height)<<endl;}

运行结果:


0 0
原创粉丝点击