niusouti.com

有如下程序:include using namespace std;class Test{public:Test(){n+=2; }~Test(){有如下程序: #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 m

题目
有如下程序:include using namespace std;class Test{public:Test(){n+=2; }~Test(){

有如下程序: #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


相似考题
更多“有如下程序:#include <iostream>using namespace std;class Test{public:Test(){n+=2; }~Test(){ ”相关问题
  • 第1题:

    如下程序的输出结果是includeusing namespace std;class Test{public:Test( ){n+=2;}

    如下程序的输出结果是 #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。

  • 第2题:

    有下列程序: include using namespace std; classTest{ public: Test(){n+=2;} ~Test

    有下列程序: #include<iostream> using namespace std; classTest{ 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

    A.n=0

    B.n=l

    C.n=2

    D.n=3


    正确答案:A
    解析: 语句Test*p=new Test;会调用类的构造函数Test() {n+=2;},使n的值由原来的1变为3,然后delete p调用类的析构函数~Test() {n-=3;},因为n是static型变量,所以会在3的基础上减3使得输出结果为0。

  • 第3题:

    有下列程序:includeusing namespace Std;class Test{public:Test(){n+=2;}~Test(){n-

    有下列程序: #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()

    A.n=0

    B.n=l

    C.n=2

    D.n=3


    正确答案:A
    解析: 此题考查的是静态数据成员和静态成员函数。静态数据成员是类中所有对象共享的成员,而不是某个对象的成员。题目中的静态数据成员n的运算具有叠加性,执行“n+=2”和“n-=3”后n的值为0。

  • 第4题:

    有如下程序:include usingnamespacestd:class Test{public: Test(){n+=2; ~Test(){n-

    有如下程序:#include <iostream>using namespace std:class Test{public: Test() {n+=2; ~Test() {n-=3; ; static int getNum() {return n;}privaue: static int n:};int Test::n=1;int main(){ Test* p=new Test; delete p; cout<<"n="<<Test::getNum()<<end1; return 0;} 执行后的输出结果是

    A.n=0

    B.n=1

    C.n=2

    D.n=3


    正确答案:A
    解析:本题考核静态数据成员与静态成员函数的定义与使用方式。静态数据成员是类中所有对象共享的成员,而不是某个对象的成员.题中变量n是静态数据成员,对象对其操作的结果具有叠加作用,main函数中先定义了Test的对象*p,然后又delete p,所以对静态数据n进行了两次操作,分别是“n+=2”和“n-=3”,n的初始值是1,那么n最后的值变为0。main函数最后通过调用静态函数getNum得到n的值,并输出。

  • 第5题:

    有如下程序: include using namespace std;class Test {public: Test() {n+=2;} ~Tes

    有如下程序: #include <iostream> using namespace std; class Test { public: Test() {n+=2;} ~Test() {n-=3;} static int getNum(){retum n;} private: static int n; }; int Test:: n=1; int main() { Test*p=new Test; delete p; cout<<"n="<<Test:: getNum()<<end1; return 0; };执行后的输出结果是______.

    A.n=0

    B.n=1

    C.n=2

    D.n=3


    正确答案:A
    解析:经过一次构造函数和析构函数的调用后,执行后的输出结果是A。