问题
填空题
下列程序的输出结果是______。 #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; }
答案
参考答案:042
解析: 此题考查的是类的构造函数与析构函数的调用。语句 cout<<Test::Count()<<"";;输出“0”,因为static型变量cnt的默认值是0;“T Test ti,t2;Test*pT3=new Test;Tcst*pT4=new Test;”调用4次类的构造函数,使得cnt的值增加到4,并输出4;然后delete pT4;delete pT3;调用两次析构函数,cnt的值变为2,并输出2。