虚函数

#include < iostream >

using namespace std;

 

class BaseClass

{

public:

    virtual void fn1() {

        cout << "B1" << endl;

    }

    void fn2() {

        cout << "B2" << endl;

    }

};

class DerivedClass :public BaseClass

{

public:

    void fn1() {

        cout << "D1" << endl;

    }

    void fn2() {

        cout << "D2" << endl;

    }

};

int main()

{

    DerivedClass D;

    DerivedClass* d = &D;

    BaseClass* b = &D;

 

    b->fn1();

    b->fn2();

    d->fn1();

    d->fn2();

    return 0;

}

 

posted @ 2023-04-13 08:54  258333  阅读(12)  评论(0编辑  收藏  举报