免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1200 | 回复: 3
打印 上一主题 下一主题

多态的问题请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-18 10:12 |只看该作者 |倒序浏览
#include <iostream.h>
using namespace std;

class Base
{

public:
        virtual Base * fn1(int x)
        {
                cout << "[fn1]In Base class " << endl;
                return this;
        }
        virtual Base * fn2()
        {
                cout << "[fn2]In Base class " << endl;
                return this;
        }

};
class SubClass : public Base  
{

public:
        virtual SubClass * fn1(float x)
        {
                cout << "[fn1]In Subclass class"<< endl;
                return this;
        }
        virtual SubClass * fn2()
        {
                cout << "[fn2]In Subclass class " << endl;
                return this;
        }

};


void test(Base & b)
{
        Base *bb;
        int i = 1;
        float f = 2.0;
        bb = b.fn1(i);
        bb = b.fn1(f);
        bb = b.fn2();
}

void main()
{
        Base bc;
        SubClass sc;
        cout << "Calling test(bc)"<<endl;
        test(bc);
        cout <<endl << "Calling test(sc)"<<endl;
        test(sc);
}

运行结果为:
Calling test(bc)
[fn1]In Base class
[fn1]In Base class
[fn2]In Base class

Calling test(sc)
[fn1]In Base class                      //为什么没有连接到 Subclass 中的fn1函数阿?
[fn1]In Base class
[fn2]In Subclass class                //为什么这个就连接到 Subclass 中的fn2函数阿?

哪个是 先期联编,哪个是迟后联编?
如何判断是否存在多态啊?

论坛徽章:
0
2 [报告]
发表于 2006-08-18 10:18 |只看该作者
要搞清楚overload, oerlap,和override的区别.

论坛徽章:
0
3 [报告]
发表于 2006-08-18 10:29 |只看该作者
这几个单词只认识 重载
也查不到相关资料,晕倒

论坛徽章:
0
4 [报告]
发表于 2006-08-18 10:36 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP