我正在开展一个项目,其中有一系列儿童课程。我想从数组中调用重写的子函数,但它调用了父函数。
#include <iostream>
class Parent {
public:
Parent(){}
void print() {
std::cout << "I'm the parent!" << std::endl;
}
};
class ChildOne : public Parent {
public:
ChildOne(){}
void print() {
std::cout << "I'm childOne!" << std::endl;
}
};
class ChildTwo : public Parent {
public:
ChildTwo() {}
void print() {
std::cout << "I'm childTwo!" << std::endl;
}
};
int main(int argc, char const *argv[]) {
Parent arr[] = {ChildOne(), ChildTwo()};
int n = 2;
for(int i = 0; i …Run Code Online (Sandbox Code Playgroud)