列表

详情


CPP45. 重写子类计算逻辑

描述

在父类 Base 中定义了计算方法 calculate(),该方法用于计算两个数的乘积(X*Y)。请在子类 Sub 中重写该方法,将计算逻辑由乘法改为除法(X/Y)。注意,当分母为0时输出“Error”。

输入描述

两个整数

输出描述

两个整数的商(int类型,不考虑小数部分)

示例1

输入:

6
2

输出:

3

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-10-31

#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( getY() ) cout << getX()/getY() << endl;
        else cout << "Error" << endl;
    }
};

int main() {

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

C++ 解法, 执行用时: 2ms, 内存消耗: 404KB, 提交时间: 2022-02-09

#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(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;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 408KB, 提交时间: 2021-11-21

#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(getY())
            cout << getX() / getY() << endl;
        else cout << "Error";
        }
    };

int main() {

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

C++ 解法, 执行用时: 2ms, 内存消耗: 412KB, 提交时间: 2022-02-08

#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(getY()==0){
            cout << "Error";
            return;
        }
        cout << getX() / getY() << endl;
    }
};

int main() {

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

C++ 解法, 执行用时: 2ms, 内存消耗: 412KB, 提交时间: 2021-11-13

#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(getY()){
            cout<<getX()/getY()<<endl;
        }
        else{
            cout<<"Error"<<endl;
        }
    }
    
};

int main() {

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

上一题