第八周项目二 用对象数组操作长方柱类

来源:互联网 发布:表白的html源码 编辑:程序博客网 时间:2024/06/15 10:47
  1. * All rights reserved, 
  2.  * 文件名称:用对象数组操作长方柱类.Cpp 
  3.  * 作者:刘默涵 
  4.  * 完成日期:2016年4月21日 
  5.  * 版本号:vc++6.0 
  6.  * 
  7.  * 问题描述:基于对象的程序,求五个长方柱的体积及其表面积。 
  8.  */

#include<iostream>
using namespace std;
class Bulk
{
public:
    Bulk(double x=1.0,double y=1.0,double z=1.0);
    void get_value();
    double value();
    double area();
private:
    double length;
    double width;
    double heigth;
};
Bulk::Bulk(double l,double w,double h)
{
    length=l,width=w,heigth=h;
}
void Bulk::get_value()
{
    cin>>length>>width>>heigth;
}
double Bulk::value()
{
    return width*length*heigth;
}
double Bulk::area()
{
    return 2*(width*length+length*heigth+heigth*width) ;
}

int main()
{
    int i;
    Bulk b[5]={Bulk(2.3,4.5,6.7),Bulk(1.5,3.4),Bulk(10.5)};
    cout<<"Please input the length,width and height:";
    b[4].get_value();
    cout<<"The volume is:";
    for(i=0;i<5;i++)
        cout<<b[i].value()<<"   ";
    cout<<endl;
    cout<<"The area is:";

    for(i=0;i<5;i++)
        cout<<b[i].area()<<"   ";
    cout<<endl;
    return 0;
}

0 0