有以下程序 #include<iostream> #include<string> using namespace std; class base { private: charbaseName[10]; public: base

admin2010-03-29  19

问题 有以下程序  #include<iostream>  #include<string>  using namespace std;  class base  {  private:    charbaseName[10];  public:    base()    {      strcpy(baseName,"Base");    }    virtual char*myName()        return baseName;    }    char *className()    {      return baseName;     }   };   class Derived: public base   {  private:    char derivedName[10];  public:    Derived()    {     strcpy(derivedName,"Derived");    }   char *myName()   {     return derivedName;   }  char *className()  {     return derivedName;   } };  void showPtr(base &p)  {    cout<<p.myName0<<" "<<p.className();  }  int main()  {    base bb;    Derived dd;    showPtr(dD) ;    retum 0;  } 动行后的输出结果为

选项 A、Derived Base
B、Base Base
C、Derived Derived
D、Base Derived

答案1

解析 本题考核虚函数的应用。类Derived是从基类Base公有派生而来的。因此, Derived是基类Base的子类型。main()函数中定义了一个基类对象bb和一个派生类对象dd。从程序中可看出派生类Derived的对象dd交给了处理基类Base的对象的函数 showPtr进行处理。由于在基类中函数myName被定义成虚函数。所以在函数showPtr中调用的myName函数为派生类的成员函数myName,从而输出Derived。然后输出 className,即基类名称Base。
转载请注明原文地址:https://kaotiyun.com/show/Yhjp777K
0

随机试题
最新回复(0)