题解 | #重写子类计算逻辑#

重写子类计算逻辑

https://www.nowcoder.com/practice/8521afcde7d147bf9975352fc5f3fb7c

#include <iostream>
using namespace std;

class Base {

  private:
    int x;
    int y;

  public:
    Base(int x, int y) {
        this->x = x;
        this->y = y;
    }

    int getX() {
        return x;
    }

    int getY() {
        return y;
    }

    void calculate() {
        cout << getX() * getY() << endl;
    }

};

class Sub : public Base {

// write your code here......
  public:

    Sub(int x, int y ): Base(x, y) {

    }

    void calculate() {
        if (this->getY() == 0) {
            cout << "Error" << endl;
        } else {
            cout << getX() / getY() << endl;
        }

    }
};

int main() {

    int x, y, z;
    cin >> x;
    cin >> y;
    Sub sub(x, y);
    sub.calculate();

    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:02
ssob上原来真有BOSS啊
硫蛋蛋:这种也是打工的,只不是是给写字楼房东打工
点赞 评论 收藏
分享
积极的小学生不要香菜:你才沟通多少,没500不要说难
点赞 评论 收藏
分享
06-15 18:44
黄淮学院 Java
Lynn012:如果是居民楼还是算了吧,看着有点野呢
点赞 评论 收藏
分享
07-03 16:02
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务