如果不使用多态机制,那么通过基类的指针虽然可以指向派生类对象,但是只能访问从基类继承的成员。有如下程序,没有使用多态机制: #include <iostream> using namespace std; class Base{

admin2012-04-29  42

问题 如果不使用多态机制,那么通过基类的指针虽然可以指向派生类对象,但是只能访问从基类继承的成员。有如下程序,没有使用多态机制:
   #include <iostream>
   using namespace std;
   class Base{
   int a,b;
   public:
    Base(int x,inty){a=x;b=y;}
   void show( ){cout<<a<<’,’<<b<<endl;}
   };
   class Derived:public Base{
   int C,d;
   public:
    Derived(int x,int y,int z,intm):Base(x,y){c=z;d=m;}
   void show( ){cout<<c<<’,’<<d<<endl;}
   }.
   int main( ){
   Base BI(50,50),*pb;
   Derived D1(10,20,30,40);
   pb=&D1;
   pb->show( );
   return 0;
   }
   程序的输出结果是
A) 10,20
B) 30,40
C) 20,30
D) 50,50

选项 A、 
B、 
C、 
D、 

答案A

解析
转载请注明原文地址:https://jikaoti.com/ti/stL0FFFM
0

随机试题
最新回复(0)