您的位置:首页 > 编程语言 > C语言/C++

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

2012-03-22 21:50 302 查看
【题目】仿照你阅读过的程序,编写基于对象的程序,求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;

}


       
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ float iostream input class