关于以下代码,说法正确的是( )。
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;
}分析时间复杂度时关注循环层数和每层迭代次数,递归算法则分析递归树。注意最坏/最好/平均情况的区别。
分析复杂度时注意外层循环和内层循环的关联。例如内外两层都到n的循环不一定是O(n²),要看内层是否每次都在缩小。
【考点】虚函数机制|override 重写|编译期错误
本题考查的是 C++ 虚函数机制与 override 关键字的绑定关系,位于 GESP 六级《面向对象编程》模块。命题人专门卡住了"override 不是装饰,是 C++ 标准强绑定"这个认知断层——大量考生看到 override 就默认能重定义,却忘了它必须对应一个已有的 virtual 函数。