20

问答题 20 /69

下面两段代码的输出分别是什么?
class Base{
    public:
        virtual void Print() const{
            cout << "Print in Base" << endl;
        }
};
class Derive::public base
{
    public:
        void Print() const{
            cout << "Print in Derive" << endl;
        }
};
void Print(const Base* base){
    base->Print();
}
int main(){
    Base b;
    Derive d;
    print(&b);
    print(&d);
    return 0;
}
class Base{
    public:
        void Print() const{
            cout << "Print in Base" << endl;
        }
};
class Derive::public base
{
    public:
        void Print() const{
            cout << "Print in Derive" << endl;
        }
};
void Print(const Base* base){
    base->Print();
}
int main(){
    Base b;
    Derive d;
    print(&b);
    print(&d);
    return 0;
}

参考答案

考察对虚函数的基本理解
第一个:Print in Base, Print in Derive
第二哥:Print in Base, Print in Base