虚函数的性质:
无论虚函数被继承多少次,它仍然是虚函数;
虚函数是分层的。
测试代码:
#include<iostream>
using namespace std;
class base
{
public:
virtual void show(){cout<<"This is in base"<<endl;}
};
class derive1:public base
{
public:
void show(){cout<<"This is in derive1"<<endl;}
};
class derive2:public derive1
{
public:
void show(){cout<<"This is in derive2"<<endl;}
};
int main()
{
base *p,a;
derive1 b;
derive2 c;
p=&a;
p->show();
p=&b;
p->show();
p=&c;
p->show();
return 0;
}
测试结果:
This is in base
This is in derive1
This is in derive2
Press any key to continue