2 回答
TA贡献1775条经验 获得超11个赞
下面分析两个经典面试题。
l 例1:
class A
{
protected:
int m_data;
public:
A(int data = 0)
{
m_data = data;
}
int GetData()
{
return doGetData();
}
virtual int doGetData()
{
return m_data;
}
};
class B: public A
{
protected : int m_data;
public:
B(int data = 1)
{
m_data = data;
}
int doGetData()
{
return m_data;
}
};
class C : public B
{
protected : int m_data;
public :
C(int data = 2)
{
m_data = data;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
C c(10);
cout<<c.GetData() << endl; //1
cout<<c.A::GetData()<<endl; //1
cout<<c.B::GetData()<<endl; //1
cout<<c.C::GetData()<<endl; //1
cout<<c.doGetData()<<endl; //1
cout<<c.A::doGetData()<<endl; //0
cout<<c.B::doGetData()<<endl; //1
cout<<c.C::doGetData()<<endl; //1
return 0;
}
- 2 回答
- 0 关注
- 643 浏览
添加回答
举报