请阅读下述代码,写出程序执行的结果(6分) #include <iostream> using namespace std; class CBase { public: virtual void print() { cout<< "base" << endl; } void DoPrint() { print(); } }; class CChild1: public CBase { public: virtual void print() { cout<< "child1" << endl; } }; class CChild2: public CBase { public: virtual void print() { cout<< "child2" << endl; } }; void DoPrint(CBase *base) { base->DoPrint(); } void main() { CBase* base = new CBase(); CChild1* child1 = new CChild1(); CChild2* child2 = new CChild2(); DoPrint(child1); /* 为什么打印出来是child1,因为虚函数的调用都是从虚函数列表中调用的,所以在DoPrintf()中执行 printf()时,是从虚函数表中查找的,在子类中,我们查到的是子类的printf(),所以输出child1。 */ DoPrint(child2); DoPrint(base); // 比较好理解,直接调用父类的函数。 delete base; base = child1; base->print(); // 传统的虚函数的用法。 delete child1; delete child2; } child1 child2 Base child1