5、盒子类

问题描述 :

定义盒子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;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值