自考新教材-p260

在构造函数和析构函数中调用虚函数

源程序:

#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;

}

运行结果:

猜你喜欢

转载自www.cnblogs.com/duanqibo/p/12183343.html