单选题 C++
题号 #2250920

有如下程序:
#include<iostream>
usingnamespacestd;
classB{
public:
B(intxx):x(xx){++count;x+=10;}
virtualvoidshow(  )const
{cout<<count<<''<<x<<endl;}
protected:
staticintcount;
private:
intx;
};
classD:publicB{
public:
D(intxx,intyy):B(XX),y(yy){++count;y+=
100;)
virtualvoidshow(  )const
{cout<<count<<''<<y<<endl);
private:
inty;
};
intB::count==0;
intmain(  ){
B*ptr=newD(10,20);
ptr->show(  );
deleteptr;
return0;
}
运行时的输出结果是(  )。

进入在线模考

参考答案

B

答案解析

B。【解析】本题考查了类的继承。继承有3种方式,public公有、private私有和protected保护,本题都涉及了。本题中类D公有继承类B。在类B中又定义了虚函数,并且有保护类静态类型count及私有变量x。主函数中调用类D,类D又继承了类B,经过系统及调用,本题最终结果为2_120。

你可能感兴趣的试题

A.栈顶元素最先能被删除
B.栈顶元素最后才能被删除
C.栈底元素永远不能被删除
D.以上三种说法都不对
A.有一个以上根结点的数据结构不一定是非线性结构
B.只有一个根结点的数据结构不一定是线性结构
C.循环链表是非线性结构
D.双向链表是非线性结构
找试题 · 找答案 · 找资料 在线模考