有如下程序: #include <iostream> using namespace std; class Test { public: Test() {n+=2; } ~Test() {n-=3;

admin2013-02-27  23

问题 有如下程序:    #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的运算具有叠加性,执行“n+=2”和“n-=3”后n的值为0。
转载请注明原文地址:https://jikaoti.com/ti/hCL0FFFM
0

相关试题推荐
最新回复(0)