18.2 LAB (C++)

考试模拟题
Example Input
100
Example Output
No. 100 bus.
No. 100 yellow bus.

主程序 (不能修改)

#include <iostream>
using namespace std;

class Bus
{
    int number;
protected:
    int getNumber() const {
        return number;
    }
public:
    Bus(int number) {
        this->number = number;
    }
    void print() const {
        cout << "No. " << number << " bus." << endl;
    }
};

#include "source.cpp"

int main() {
    int number; 
    cin >> number;
    Bus bus(number);
    bus.print();

    YellowBus yellow(number);
    yellow.print();
}

参考答案

class YellowBus{
    public:
    int data;
    YellowBus(int number) {
        data = number;
    }
    void print() const {
        cout << "No. " << data << " yellow bus." << endl;
    }
};

答案

class YellowBus : public Bus
{
public:
    YellowBus(int number) : Bus(number) {}
    void print() const {
        cout << "No. " << getNumber() << " yellow bus." << endl;
    }
};
支持 makedown语法