全部题库 / C++ / 试题详情
单选题 C++
2013-08-06

有如下程序:
#included<iostream>
usingnamespacestd;
classTestClass
{inta;
pubIic:
TestClass(intx){a=x;}
voidshow(){cout<<a;)};
classTestClassl:publicTestClass
{intb;
public:
TestClassl(inti):TestClass(i+1),b(i){}
voidshow(){cout<<b;)};
intmain()
{TestClassb(5),*P;
TestClassld(1):
p=&d;
p->show();
return0;}
该程序运行后的打印结果是(  )。

A.5
B.1
C.0
D.2

参考答案

D

答案解析

D。【解析】TestClass为TestClass1的基类,在主函数main中定义TestClass对象b,*P。TestClass1对象d,P指向d,调用其show函数。“TestClass(inti):TestClass(i+1),b(i){}”语句中的TestClass基类参数为2,所以show输出2。

你可能感兴趣的试题

A.Volumeoperator-(double,double);
B.doubleVolume::operator-(double);
C.VolumeVolume::operator-(Volume);
D.Volumeoperator-(Volume);
A.XB::XB(inta,intb):x(a),y(b){}
B.XB::XB(inta,intb):XA(a),y(b){}
C.XB::XB(inta,intb):x(a),XB(b){}
D.XB::XB(inta,intb):XA(a),XB(b){}