20140802 虚函数

1、虚函数

http://blog.csdn.net/livelylittlefish/article/details/2171504

2、面试宝典

#include<iostream>
using namespace std;

class A
{
protected:
    int m_data;
public:
    A(int data=0)
    {m_data=data;
    cout<<"构造函数A"<<endl;
    }

    int Getdata()
    {return    doGetdata();    
    }
    virtual int doGetdata()
    {        cout<<m_data<<endl;
    return m_data;}
};

class B:    public A 
{
protected:
    int m_data;
public:
    B(int data=1)
    {m_data=data;
    cout<<"构造函数B"<<endl;}
    int Getdata()
    {    return doGetdata();    }
    virtual int doGetdata()
    {
        cout<<m_data<<endl;
        return m_data;
    }
};

class C: public B
{
protected:
    int m_data;
public:
    C(int data=2)
    {m_data=data;
cout<<"构造函数C"<<endl;    }
};

int main()
{
    C c(10);
    c.Getdata();
    c.A::Getdata();
    c.B::Getdata();
    c.C::Getdata();
    c.doGetdata();
    c.A::doGetdata();
    c.B::doGetdata();
    c.C::doGetdata();
    system("pause");
    return 0;

}
posted @ 2014-08-04 19:45  yexuannan  阅读(148)  评论(0编辑  收藏  举报