《C&C++完美演绎》源代码
源代码在线查看: 18-5.txt
/* 范例:18-5 */
#include
class Base
{
public:
virtual void fun1(){ cout void fun2(){ cout };
class Derived:public Base
{
public:
void fun1(){ cout void fun2(){ cout };
void main()
{
Derived *obj_Derived = new Derived ;
Base *obj_Base = new Derived ;
obj_Base->fun1(); /* 调用Derived::fun1(),依照链接 */
obj_Base->fun2(); /* 调用Base::fun2() */
obj_Derived->fun1(); /* 调用Derived::fun1(),依照链接 */
obj_Derived->fun2(); /* 调用Derived::fun2() */
delete obj_Base;
delete obj_Derived;
getchar();
}
程序执行结果:
Derived::fun1
Base::fun2
Derived::fun1
Derived::fun2