单选题
有如下程序:
#include<iostream>
using namespace std;
class B{
public:
B(int xx):x(xx){++count;x+=10;}
virtual void show()const
{cout<<count<<"_"<<x<<endl;}
protected:
static int count;
private:
int x;
};
class D:public B{
public:
D(int xx,int yy):B(xx),y(yy)(++count;y+=100;}
virtual void show()const
{cout<<count<<"_"<<y<<endl;}
private:
int y;
};
int B::count=0;
int main(){
B*ptr=new D(10,20);
ptr->show();
delete ptr;
return 0;
}
运行时的输出结果是______。
【正确答案】
B
【答案解析】[解析] 本题考查派生类构造函数调用、静态成员以及虚函数的使用。题中,定义基类指针ptr指针派生类的动态空间。要先调用基类B的构造函数,coum自增后为1,x的值为20;再调用派生类D的构造函数,count自增后为2,y的值为120。“ptr->show();”调用的是派生类D中的show()函数,输出count和y的值,分别为2和120。最终输出结果为2_120。