问题 单项选择题

如下程序的输出结果是
#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.n=0

B.n=1

C.n=2

D.n=3

答案

参考答案:A

解析: 静态数据成员的初始值n=1,执行Test*p=new Test;,调用构造函数后,n= 3,deletep;调用析构函数,n-=3,所以最终n=0。

多项选择题
问答题 简答题