运行代码:
//定义一个派生自D1的D2类,并且在D2中覆盖pvf()
//建立一个D2类的对象并且调用f()、vf()、pvf()
#include"std_lib_facilities.h"
//----------------------------------------------------------------------
//定义B1类。
class B1
{
public:
virtual void vf() { cout<< "B1::vf()\n"; }
void f() { cout<< "B1::f()\n"; }
virtual void pvf()=0 { cout << "B1::pvf()\n"; }
};
//----------------------------------------------------------------------
//定义派生自B1的D1类
class D1 : public B1
{
public:
void vf() { cout << "D1::vf()\n"; }
void f() { cout << "D1::f()\n"; }
};
//-----------------------------------------------------------------------
// 定义一个派生自D1的D2类
class D2 :public D1
{
public:
void pvf() { cout << "D2::pvf()\n"; }
};
//-----------------------------------------------------------------------
//定义一个B1的引用
void call( B1& b1)
{
b1.vf();
b1.f();
}
//-----------------------------------------------------------------------
int main()
try{
D2 d2;
d2.f();
d2.vf();
d2.pvf();
}
catch (exception& e) {
cerr << "error:" << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Oops:unknown exception!\n";
keep_window_open();
return 2;
}
//-----------------------------------------------------------------------
运行结果: