问题 填空题

下面程序的打印结果是 【11】 。 #include <iostream> using namespace std; class Base { public: Base(int x) {a=x; } void show() {cout<<a; } private: int a; }; class Derived : public Base { public: Derived(int i) :Base(i+1) ,b(i) { } void show() {cout<<b; } private: int b; }; int main ( ) { Base b(5) , *pb; Derived d(1); pb=&d; pb->show(); return 0; }

答案

参考答案:B

解析: 基类Base派生出派生类Derived,在主函数中,定义了基类对象b,基类指针pb,以及派生类对象d,并让基类指针pb指向派生类对象乙在C++中,当派生类的对象赋值给基类对象时,只能使用派生类对象中从基类继承的成员。所以最后执行语句“pb->show();”是调用基类的成员函数show(),输出a的值2。

选择题
多项选择题