测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A22042. 执⾏如下代码,会输出钢琴:叮咚叮咚和 吉他:咚咚当当 。这体现了⾯向对象编程的( )特性。class Instrument { public: virtual void play() { cout << "乐器在演奏声音" << endl; } virtual ~Instrument() {} }; class Piano : public Instrument { public: void p…

单选题 困难
知识点

题目描述

执⾏如下代码,会输出钢琴:叮咚叮咚和 吉他:咚咚当当 。这体现了⾯向对象编程的(    )特性。

class Instrument {
public:
    virtual void play() {
        cout << "乐器在演奏声音" << endl;
    }
    virtual ~Instrument() {}
};

class Piano : public Instrument {
public:
    void play() override {
        cout << "钢琴: 叮咚叮咚" << endl;
    }
};

class Guitar : public Instrument {
public:
    void play() override {
        cout << "吉他: 咚咚当当" << endl;
    }
};

int main() {
    Instrument* instruments[2];
    instruments[0] = new Piano();
    instruments[1] = new Guitar();

    for (int i = 0; i < 2; ++i) {
        instruments[i]->play();
    }

    for (int i = 0; i < 2; ++i) {
        delete instruments[i];
    }
    return 0;
}

选项(单选)

上一题 下一题