C++:创建一个长方形类,可实例化多个长方形来求其面积、体积。

#include<iostream>
using namespace std;
class Box
{
  private:
    int length, width, high;
  public:
    void set_length(int l)
    {
        length=l;
    }
    void set_width(int w)
    {
        width=w;
    }
    void set_high(int h)
    {
        high=h;
    }
    int get_length()
    {
        return length;
    }
    int get_width()
    {
        return width;
    }
    int get_high()
    {
        return high;
    }
    int get_area()
    {
        return 2 * length*width + 2 * width*high + 2 * high*length;
    }
    int get_space()
    {
        return length*width*high;
    }
};
int main()
{
    Box A, B, C;
    A.set_length(2);
    A.set_width(3);
    A.set_high(4);
    cout << "A.length=" << A.get_length() << endl;
    cout << "A.width=" << A.get_width() << endl;
    cout << "A.high=" << A.get_high() << endl;
    cout << "A.area=" << A.get_area() << endl;
    cout << "A.space=" << A.get_space() << endl;
    B.set_length(5);
    B.set_width(4);
    B.set_high(8);
    cout << "B.length=" << B.get_length() << endl;
    cout << "B.width=" << B.get_width() << endl;
    cout << "B.high=" << B.get_high() << endl;
    cout << "B.area=" << B.get_area() << endl;
    cout << "B.space=" << B.get_space() << endl;
    C.set_length(7);
    C.set_width(1);
    C.set_high(6);
    cout << "C.length=" << C.get_length() << endl;
    cout << "C.width=" << C.get_width() << endl;
    cout << "C.high=" << C.get_high() << endl;
    cout << "C.area=" << C.get_area() << endl;
    cout << "C.space=" << C.get_space() << endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值