您的位置:首页 > 其它

孙鑫教程第三章小测试程序

2012-04-15 17:05 253 查看
#include <IOSTREAM.H>
//定义一个基类,模拟CWinApp
class Base
{
public:
Base();
//	virtual void fn();/*测试虚函数结果:call the Derived's fn*/
void fn();/*测试非虚函数结果:call the Base's fn*/
Base *p;
};
Base::Base()
{
p = this;//this指针指向哪一个对象?答:指向派生类对象dd
}

void Base::fn()
{
cout << "call the Base's fn" << endl;
}

//定义一个派生类,模拟CTestApp
class Derived:public Base
{
public:
void fn();
};
void Derived::fn()
{
cout << "call the Derived's fn" << endl;
}

//定义一个派生类的全局对象,模拟theApp
Derived dd;

int main()
{
(dd.p)->fn();
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐