C++之虚函数

<span style="font-size:18px;">#include <iostream>

using namespace std ; 

class   AA 
{
    public:
        int a ;

        //虚函数
        virtual void say_hello(void)
        {
            cout << "this is your parent " << endl ; 
        }

};

class  BB : public  AA
{
    public:
        int b ; 

//如果子类没有实现虚函数,则多态中会调用父类的虚函数
//如果子类有重新实现虚函数,则多态中会调用子类的虚函数 
//__weak   相似

//        void say_hello(void)
//        {
//            cout << "this is your son " << endl ; 
//        }
};

int main(void)
{
    BB bb ; 

    AA * aa = NULL ;

    aa =  (AA *)&bb ; 

    aa->say_hello();
    

    return  0 ; 
}

</span>

运行结果:

本文同步分享在 博客“Engineer-Bruce_Yang”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

评论

暂无评论