试题与答案

有如下程序: #include<iostream> using namespac

题型:问答题

题目:

有如下程序:
#include<iostream>
using namespace std;
class Test
public:
Test() n+=2;
~Test() n-=3;
static int getNum()return n;
private:
static int n;

int Test::n=1;
int main()

Test*p=new Test;
delete p;
cout<<"n="<<Test::getNum()<<endl;
return 0;

执行后的输出结果是( )。

答案:

参考答案:A

解析: 语句Test*p=new Test;会调用类的构造函数Test() {n+=2;},使n的值由原来的1变为3,然后delete p调用类的析构函数~Test() {n-=3;

试题推荐
微信公众账号搜索答案