#include iostream using namespace std; class Base { public: virtual void show() { coutBaseendl; } }; class De:public Base { private: virtual void show() { coutDeriendl; } }; void main() { Base *p = new De; p-show(); //OK,Output : Deri }
#include
using namespace std;
class Base
{
public:
virtual void show() {
cout }
};
class De:public Base
{
private:
virtual void show() {
cout }
};
void main()
{
Base *p = new De;
p->show(); //OK,Output : Deri
}
当把基类和派生类的public,private互换时,
error #308: function "Base::show" is inaccessible
p->show();
可见类虚拟成员函数的访问性是由调用该函数的指针决定的
(本程序中是由p的类型)
#include
using namespace std;
class Base
{
public:
virtual void show() {
cout }
};
class De:public Base
{
private:
virtual void show() {
cout }
};
void Test(Base& p)
{
p.show();
}
void main()
{
Base *p = new De;
p->show(); //OK output : Deri
Base ba;
Test(ba); //OK : base
De da;
Test(da); //OK : Deri
}