A-A+

有如下程序: #includedusingnamespacestd; c

2022-08-06 01:50:22 问答库 阅读 174 次

问题详情

有如下程序:
#included<iostream>
usingnamespacestd;
classTestClass
{inta;
pubIic:
TestClass(intx){a=x;}
voidshow(){cout$amp;classTestClassl:publicTestClass
{intb;
public:
TestClassl(inti):TestClass(i+1),b(i){}
voidshow(){cout$amp;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。

考点:程序