有如下程序:
#include<iostream>
using namespace std;
class B
public:
B(int xx):x(xx)++count;x+=10;
virtual void show()const
cout<<count<<’_’<<x<<end1;
protected:
static mt 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<<end1;
private:
int y;
;
int B::count=0;
int main()
B *ptr=new D(10,20);
ptr—>show();
delete ptr;
return 0;
运行时的输出结果是( )。
A.1_120
B.2_120
C.1_20
D.2_20
参考答案:B
解析: 本题考查了类的继承。继承有3种方式,public公有、private私有和protected保护,本题都涉及到了。本题中类D公有继承类B。在类B中又定义了虚函数,并且有保护类静态类型count及私有变量x。主函数中调用类D,类D又继承了类B,经过系统及调用,本题最终结果为2_120。