问题描述 :
定义盒子Box类,计算盒子的体积及表面积。Box类包括:
私有数据成员:
int Length //Box的长度
int Width //Box的宽度
int Height //Box的高度
公有成员函数:
void InitBox( int x, int y, int z); //设置Box的尺寸
void setVolume( ); //计算Box的体积
void setArea( ); //计算Box的表面积
void show( ); //输出盒子的信息,输出形式见“输出说明”
要求使用以下main函数测试Box类:
int main()
{
int intLength, intWidth, intHeight;
Box box;
cin>>intLength;
cin>>intWidth;
cin>>intHeight;
box.initBox(intLength,intWidth,intHeight);
box.setVolume();
box.setArea();
box.show();
return 0;
}
输入说明 :
输入三个整数:Length(长)、Width(宽)、Height(高),整数之间以空格分隔。
输入的整数都为非负数。
输出说明 :
输出三行:
第一行输出Length(长)、Width(宽)、Height(高),整数之间以一个空格分隔
第二行输出体积
第三行输出表面积
输出行之间无多余的空行和空格
输入范例 :
1 2 3
输出范例 :
1 2 3
6
22
题解代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
using namespace std;
class Box{
private:
int Length;
int Width;
int Height;
long long int v, s;
public:
void InitBox(int x, int y, int z);
void setVolume();
void setArea();
void show();
};
void Box::InitBox(int x, int y, int z)
{
Length = x;
Width = y;
Height = z;
return ;
}
void Box::setVolume()
{
v = Length * Width * Height;
return;
}
void Box::setArea()
{
s = 2 * (Length * Width + Width * Height + Length * Height);
return ;
}
void Box::show()
{
cout << Length << " " << Width << " " << Height << endl;
cout << v << endl;
cout << s << endl;
return ;
}
int main()
{
int intLength, intWidth, intHeight;
Box box;
cin >> intLength;
cin >> intWidth;
cin >> intHeight;
box.InitBox(intLength, intWidth, intHeight);
box.setVolume();
box.setArea();
box.show();
return 0;
}