第4周项目3-用对象数组操作长方体类

来源:互联网 发布:在线网络理财产品排名 编辑:程序博客网 时间:2024/05/01 03:57

  #include <iostream>#include <string>using namespace std;class Bulk{public:    void get_value();    void output();    Bulk(double ,double,double);private:    double length,width,heigh;};Bulk::Bulk(double n=1,double m=1,double k=1){    length=n;    width=m;    heigh=k;}void Bulk::get_value(){    cout<<"请输入长   宽   高";    cin>>length>>width>>heigh;}void Bulk::output(){   double s,v;    s=length*width*2+length*heigh*2+width*heigh*2;    v=length*width*heigh;    cout<<"表面积为:"<<s;    cout<<"体积为:"<<v<<endl;}int main(){    int i;    Bulk b[5]={Bulk(2.3,4.5,6.7),Bulk(1.5,3.4),Bulk(10.5)};    b[4].get_value();    for(i=0;i<5;i++)    {        cout<<"第"<<i+1<<"个长方体的:";        b[i].output();    }    return 0;}





0 0
原创粉丝点击