编写长方体(C++)

【问题描述】

利用给定的矩形类,编写长方体类

class Rect {
public:
    Rect(float l, float w);                        //定义矩形接受长和宽
    float getArea();                                      //矩形面积
protected:
    float mLength;                                            //定义长
    float mWidth;                                              //定义宽
};

Rect::Rect(float l, float w) {
    this->mLength = l;
    this->mWidth = w;
}
float Rect::getArea() {
    float area = mWidth * mLength;
    return area;
}



class cubiod : public Rect {
public:
    cubiod(float l, float w, float h);
    float getVol();
    float Area();
protected:
    float mHeight;
};

cubiod::cubiod(float l, float w, float h) : Rect(l, w) {
    this->mHeight = h;
}

float cubiod::getVol() {             //体积
    float underside = getArea();
    float V = mHeight * underside;
    return V;
}

float cubiod::Area() {          //表面积
    float underside = mLength * mWidth;
    float side1 = mHeight * mWidth;
    float side2 = mHeight * mLength;
    float Area = (underside + side1 + side2) * 2;
    return Area;
}

#include<iostream>

using namespace std;

int main() {
    int j, k, l;
    cout << "请输入矩形的长和宽:";
    cin >> j >> k;                             //键盘接收长和宽
    Rect r1(j, k);
    cout << "矩形的面积为:" << r1.getArea() << endl;
    cout << "请输入立方体的高:";
    cin >> l;                                     //接受高
    cubiod c1(j, k, l);
    cout << "立方体体积为:" << c1.getVol() << endl;
    cout << "立方体表面积为:" << c1.Area();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

*OASIS*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值