首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

大家来拿分了噢,超简单,该如何处理

2012-02-20 
大家来拿分了噢,超简单#includeiostream.hclassX{public:voidrotate(){cout Thebaseclass srotatew

大家来拿分了噢,超简单
#include   <iostream.h>


class   X
{
        public:
        void   rotate(){cout < < "The   base   class 's   rotate   was   called. " < <endl;};
};

class   Z   :   public   X
{
        public:
        void   rotate(){cout < < "The   derived   class 's   rotate   was   called. " < <endl;};
};

void   rotate(X   datum,   X   *pointer,   X   &reference)
{
        cout < < "The   pointer: " < <endl;
        (*pointer).rotate();
        cout < < "The   reference: " < <endl;
        reference.rotate();
        cout < < "The   object: " < <endl;
        datum.rotate();
}

main()
{
        Z   z;
        rotate(z,   &z,   z);
        return   0;
}

输出结果:
The   pointer:
The   base   class 's   rotate   was   called.
The   reference:
The   base   class 's   rotate   was   called.
The   object:
The   base   class 's   rotate   was   called.

为什么指针和引用没有调用派生类的rotate?

[解决办法]
因为不是虚函数,静态邦定。所以 是调用基类 函数
[解决办法]
virtual void rotate(){cout < < "The base class 's rotate was called. " < <endl;
这样就能调用了!

热点排行