您的位置:首页 > 其它

对象数组操作长方柱类

2014-05-11 17:03 127 查看
#include <iostream>
using namespace std;
class Bulk
{
public:
Bulk(double l=1.0,double w=1.0,double h=1.0):length(l),width(w),heigth(h){};
void get_value();
double area();
double cube();
void output();
private:
double length;
double width;
double heigth;
};
void Bulk::get_value()
{
cout<<"请输入长宽高:";
cin>>length>>width>>heigth;
}
double Bulk::area()
{
return 2*(length*width+length*heigth+width*heigth);
}
double Bulk::cube()
{
return length*width*heigth;
}
void Bulk::output()//可直接输入面积和体积表达式,把area()和cube()去除
{
cout<<"表面积:"<<area()<<endl;
cout<<"体积:"<<cube()<<endl;
}
int main()
{
Bulk b[5]={Bulk(2.3,4.5,6.7),Bulk(1.5,3.4),Bulk(10.5)};
b[4].get_value();
b[0].output();//可写个循环输出
b[1].output();
b[2].output();
b[3].output();
b[4].output();
return 0;
}
运行结果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: