问题
单项选择题
下列程序的输出结果是______。
#include<iostream>
using namespace std;
class Test(
public:
Test() cnt++;
~Test() cnt--;
static int Count()return cnt;
private:
static int cnt;
;
int Test::cnt=0;
int main()
cout<<Test::Count()<<"";
Test t1,t2;
Test*pT3=new Test;
Test*pT4=new Test;
cout<<Test::Count()<<"";
delete pT4;
delete pT3;
cout<<Test::Count()<<end1;
return 0;
A.024
B.042
C.420
D.240
答案
参考答案:B
解析: 此题考查的是类的构造函数与析构函数的调用。语句 cout<<Tcst::Count()<<"";;输出“0”,因为static型变量cnt的默认值是0;“T Test t1,t2;Test*pT3=new Test;Test*pT4=new Test;”调用4次类的构造函数,使得cnt的值增加到4,并输出4;然后delete pT4;delete pT3;调用两次析构函数,cnt的值变为2,并输出2。