单选题 若有以下程序:
#include <iostream>
using namespace std;
class Base
{
private:
int a,b;
public:
Base(int x, int y)
{
a=x;
b=y;
}
void show()
{
cout<<a<<", "<<b<<end1;
}
};
class Derive : public Base
{
private:
int c, d;
public:
Derive(int x, int y, int z,int m):Base(x,y)
{
c=z;
d=m;
}
void show()
{
cout<<c<<", "<<d<<end1;
}
};
int main ( )
{
Base b(50,50) ,*pb;
Derive d(10,20,30,40);
pb=&d;
pb->show {);
return 0;
}
【正确答案】 A
【答案解析】[解析] 本题考核基类指针的使用。本题首先定义了一个基类Base和一个由Base派生出来的派生类Derive。在主函数中,定义了一个基类Base指针吵和基类对象b,还定义了派生类Derive的对象do然后将派生类对象d的地址赋值给指向基类Base的指针pb。由于Derive是Base的子类型,因此允许上述赋值。但这时指针pb只能使用从基类Base继承的成员,即当pb指向d对象时,pb->show还是调用基类Base的成员函数show()。所以程序最后输出的是对象d中对基类成员的初始化值,即10,20。