单选题
C++
题号 #2255016
有如下程序:
#inelude<iostream>
usingnamespacestd;
classTest
{
public:
Test(){n+=2;}
~Test(){n-=3;}
staticintgetNum(){returnn;}
private:
staticintn;
};
intTest::n=1;
intmain()
Test*P=newTest:
deleteP;
cout<<"n="<<Test::getNum()<<endl;
return0;
}
执行后的输出结果是( )。
参考答案
A
答案解析
A。【解析】静态数据成员是类中所有对象共享的成员,而不是某个对象的成员。题中变量n是静态数据成员,对象对其操作的结果具有叠加作用,main函数中先定义了Test的对象*P,然后又delereP,所以对静态数据n进行了两次操作,分别是"n+=2"和"n-=3",n的初始值是1,那么n最后的值变为0。main函数最后通过调用静态函数getNum得到n的值,并输出。
你可能感兴趣的试题
A.20
B.30
C.10
D.0
A.hello,test
B.Hello,Test
C.HELLO,TEST
D.hELLO,tEST
A.printl
B.print2
C.printlprint2
D.程序编译时出错