#3846. [GESP202512 六级 C++] 第 3 题

[GESP202512 六级 C++] 第 3 题

关于以下代码,说法正确的是( )。

class Instrument {
public:
   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 < 3; ++i) {
        delete instruments[i];
    }
    return 0;
}

{{ select(1) }}

  • 执行代码会输出两行,内容分别为:钢琴:叮咚叮咚吉他:咚咚当当
  • 执行代码会输出两行,内容分别为:乐器在演奏声音乐器在演奏声音
  • 代码编译出现错误
  • 代码运行出现错误