在构造函数和析构函数中调用虚函数
源程序:
#include <iostream>
using namespace std;
class A{
public:
virtual void hello(){
cout<<"A::hello"<<endl;
}
virtual void bye(){
cout<<"A::bye";
}
};
class B:public A{
public:
virtual void hello(){
cout<<"B::hello"<<endl;
}
B(){
hello();
}
~B(){
bye();
}
};
class C:public B{
public:
virtual void hello(){
cout<<"C::hello"<<endl;
}
};
int main(){
C obj;
return 0;
}
运行结果: