2012年 上半年 第三周 C++程序设计 (七)

【题目】仿照你阅读过的程序,编写基于对象的程序,求3个长方柱的体积。数据成员包括长(length)、宽(width)、高(heigth)、体积,要求用成员函数实现下面的功能:
(1)由键盘输入3个长方柱的长、宽、高;
(2)计算长方柱的体积(volume)和表面积(areas);
(3)输出这3个长方柱的体积和表面积;


 

#include <iostream> 
  
using namespace std; 
 
class Box 
{
private:  
	float length;  
	float width;  
	float heigh; 

public: 
	void input();
	void show_value();
	float ares(); 
	float volume();
};  


void Box::input()
{
	cout << "请输入长,宽,高: " ;
	cin >> length;
	cin >> width;
	cin >> heigh;
}

float Box::ares()
{
	float s;
	s = 2*(length*heigh+length*width+width*heigh);
	return s;
}

float Box::volume()
{
	float v;
	v = length*heigh*width;
	return v;
}

void Box::show_value()
{
	cout << "此长方柱的表面积为: "<< ares() << endl;
	cout << "此长方柱的体积为: "<< volume() << endl;
}

int main()  
{
	Box box1 ,box2 ,box3;
	box1.input ();
	box1.show_value();

    box2 .input ();
	box2.show_value();

	box3.input();
	box3.show_value();

	return 0;

}


       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值