Z
zs0723
#include <iostream>
using namespace std;
class A
{
public:
virtual void foo(){ cout<<"A::foo"<<endl;}
};
class B
ublic A
{
protected:
virtual void foo(){ cout<<"B::foo"<<endl;}
};
class C
ublic A
{
private:
virtual void foo(){ cout<<"C::foo"<<endl;}
};
int main()
{
A *p = new B;
p->foo();
A * q =new C;
q->foo();
}
My question is why p->foo() can work , I think foo() method in class B
is protected ,
the external client have no right to access it.
using namespace std;
class A
{
public:
virtual void foo(){ cout<<"A::foo"<<endl;}
};
class B
{
protected:
virtual void foo(){ cout<<"B::foo"<<endl;}
};
class C
{
private:
virtual void foo(){ cout<<"C::foo"<<endl;}
};
int main()
{
A *p = new B;
p->foo();
A * q =new C;
q->foo();
}
My question is why p->foo() can work , I think foo() method in class B
is protected ,
the external client have no right to access it.