编写基于对象的程序,求5个长方柱的体积和表面积。长方柱类Bulk的数据成员包括长(length)、宽(width)、高(heigth)等。
对照已经给出的代码,要做的工作及要求有:
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个长方柱的体积和表面积
}/* All rights reserved.
* 文件名称:test.cpp
* 作者:陈丹妮
* 完成日期:2015年 4 月 2 日
* 版 本 号:v1.0
*/
#include <iostream>
using namespace std;
class Bulk
{
public:
Bulk(double x=1.0,double y=1.0,double z=1.0):lengh(x),width(y),height(z){};
void get_value();
void display();
private:
double lengh;
double width;
double height;
};
void Bulk::get_value()
{
cout<<"请分别输入长方体的长,宽,高:"<<endl;
cin>>lengh>>width>>height;
}
void Bulk::display()
{
cout<<"输出长方柱的体积:"<<lengh*width*height<<endl;
cout<<"输出长方柱的表面积:"<<2*(lengh*width+width*height+lengh*height)<<endl;
cout<<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].display();
b[1].display();
b[2].display();
b[3].display();
b[4].display();
return 0;
}
<pre name="code" class="cpp">/* All rights reserved.
* 文件名称:test.cpp
* 作者:陈丹妮
* 完成日期:2015年 4 月 2 日
* 版 本 号:v1.0
*/
#include <iostream>
using namespace std;
class Bulk
{
public:
Bulk(double x=1.0,double y=1.0,double z=1.0):lengh(x),width(y),height(z){};
void get_value();
void display();
private:
double lengh;
double width;
double height;
};
void Bulk::get_value()
{
cout<<"请分别输入长方体的长,宽,高:"<<endl;
cin>>lengh>>width>>height;
}
void Bulk::display()
{
cout<<"分别输出长,宽,高:"<<lengh<<" "<<width<<" "<<height<<endl;
cout<<"输出长方柱的体积:"<<lengh*width*height<<endl;
cout<<"输出长方柱的表面积:"<<2*(lengh*width+width*height+lengh*height)<<endl;
cout<<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();
for(int i=0;i<5;i++)
{
cout<<"关于b["<<i<<"]"<<endl;
b[i].display();
}
return 0;
}
原文:http://blog.csdn.net/nufangdongde/article/details/44838213