下列程序的输出结果为2,请将程序补充完整。 #inelude<iostream.h> using namespace std; class Base { public: ______ void fun( ){cout<<

admin2013-05-30  20

问题 下列程序的输出结果为2,请将程序补充完整。
   #inelude<iostream.h>
   using namespace std;
   class Base
   {
   public:
   ______ void fun( ){cout<<1;}
   };
   class Derived: public Base
   {
   pbulic:
   void fubn( ){cout<<2;}
   };
   int main( )
   {Base*P=new Derived;
   P→fun( );
   delete P;
   return 0;
   }

选项

答案virtual

解析 本题考查虚函数的概念。在C++中,一个基类的指针可以用于指向它的派生类对象。而且通过这样的指针调用虚函数时,被调用的是该指针实际指向的对象类的那个重定义版本。在本题中,通过对象指针P调用的是派生类中重定的。fun函数,那么基类中的fun只有是虚函数才能满足要求。
转载请注明原文地址:https://kaotiyun.com/show/xUNp777K
0

随机试题
最新回复(0)