我是面向对象编程的新手,所以我对最基本的概念含糊不清。但是,以下示例完全使我感到困惑:
class Base {
public:
void Hello() {
cout << "hello world from the BASE class" << endl;
}
void printHello() {
for (int i = 0; i <= 5; i++) {
Hello();
}
}
};
class Derived: public Base {
public: void Hello() {
cout << "hello world from the DERIVED class";
}
};
int main() {
Derived obj;
obj.printHello();
return 0;
}
预期结果:5 *“来自DERIVED类的Hello world”
结果:5 *“来自BASE类的世界”
是否应将BASE类中的Hello()函数替换为DERIVED类中的Hello()函数?如果没有,我如何获得呢?
谢谢!