您的位置:首页 > 其它

第五周项目(4)-长方柱类

2016-04-07 08:30 399 查看
/*

*copyright (c) 2016,烟台大学计算机学院

*All rights reserved.

*文件名称:hellow.cpp

*作者:田甜

*完成日期:2016年4月6日

*版本号:v1.0

*问题描述:编写基于对象的程序,求3个长方柱(Bulk)的体积。数据成员包括长(length)、宽(width)、高(heigth)、体积,要求设计成员函数实现下面的功能:

  (1)由键盘输入3个长方柱的长、宽、高;

  (2)计算长方柱的体积(volume)和表面积(areas);

  (3)输出这3个长方柱的体积和表面积;

*输入描述:输入3个长方柱的长、宽、高。

*程序输出:输出这3个长方柱的体积和表面积。

*/

问题及代码:

#include <iostream>

using namespace std;
class Bulk
{
public:
void get_value();
void get_are();
void get_volume();
void show();
private:
float lengh;
float width;
float hight;
float area;
float volume;
};

void Bulk::get_value()
{
cout<<"please input value:"<<endl;
cin>>lengh>>width>>hight;
get_are();
get_volume();//在此处调用比较好,程序比较清晰
}

void Bulk::get_are()
{
area=lengh*hight*2+lengh*width*2+hight*width*2;
}

void Bulk::get_volume()
{
volume=lengh*width*hight;
}

void Bulk::show()
{
cout<<"The eare is:"<<area<<"  The volume is:"<<volume<<endl;
}
int main()
{
Bulk bulk1;
Bulk bulk2;
Bulk bulk3;
bulk1.get_value();
bulk2.get_value();
bulk3.get_value();
cout<<"for bulk1:"<<endl;
bulk1.show();
cout<<"for bulk2:"<<endl;
bulk2.show();
cout<<"for bulk3:"<<endl;
bulk3.show();

return 0;
}


运行结果:



心得:

在赋值语句中加入求面积和体积的函数,可以减少在主函数中调用的次数从而使程序更加简洁。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: