版权声明:转载请附上原文链接哟! https://blog.csdn.net/weixin_44170305/article/details/90049707
风华是一指流砂,苍老是一段年华。
题目描述
编写基于对象的程序,求长方柱(Bulk)的体积。数据成员包括长(length)、宽(width)、高(heigth)、体积,要求用成员函数实现下面的功能:
(1)由键盘输入长方柱的长、宽、高;
(2)计算长方柱的体积(volume)和表面积(areas);
(3)输出这长方柱的体积和表面积。
(可以复制提示部分的代码开始你的编程)
输入
长方柱的长、宽、高
输出
长方柱的体积和表面积
样例输入
copy
<span style="color:#333333">2 3 4</span>
样例输出
<span style="color:#333333">24
52</span>
提示
class Bulk
{
public:
//此处声明需要的成员函数
private:
double lengh;
double width;
double height;
};
//下面定义成员函数
//用main()函数测试,完成输入输出
int main()
{
Bulk b1;
b1.set_value();
cout<<b1.get_volume()<<endl;
cout<<b1.get_area()<<endl;
return 0;
}
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
class Bulk
{
public:
void set_value();
double get_volume();
double get_area();
//此处声明需要的成员函数
private:
double lengh;
double width;
double height;
};
void Bulk::set_value()
{
cin>>lengh>>width>>height;
}
double Bulk::get_volume()
{
return lengh*width*height;
}
double Bulk::get_area()
{
return 2*lengh*width+2*lengh*height+2*width*height;
}
//下面定义成员函数
//用main()函数测试,完成输入输出
int main()
{
Bulk b1;
b1.set_value();
cout<<b1.get_volume()<<endl;
cout<<b1.get_area()<<endl;
return 0;
}