niusouti.com

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

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


相似考题
更多“有如下程序: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() {r”相关问题
  • 第1题:

    有如下程序: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() { Test*p=new Test;

    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的值,并输出。

  • 第2题:

    有如下程序:includeusing namespace std;class test{private: int a;public: test(

    有如下程序:#include<iostream>using namespace std;class test{private: int a;public: test(){cout<<"constructor"<<endl;} test(int a){cout<<a<<endl;} test(const test&_test) { a=_test.a; cout<<"copy constructor"<<en+dl; } ~test(){cout<<"destructor"<<endl;}};int main(){ test A(3); rerun 0;}运行时输出的结果是

    A.3

    B.constructor destructor

    C.copy constructor destructor

    D.3 destructor


    正确答案:D
    解析:本题考查的知识点是:构造函数和析构函数。一个类可以有多个构造函数,但只能有一个析构函数。每一个对象在被创建的时候,都会隐含调用众多构造函数中的一个,而在被销毁的时候,又会隐含调用唯一的那个析构函数。因此,解此类题目只要找准创建时调用的是哪个构造函数,和对象何时被销毁即可。本题只有主函数中创建了一个对象A,并使用了构造参数3,因此会隐含调用test(int a)这个构造函数,输出一个3。接下来主函数结束,对象A被销毁,所以又隐含调用~test()析构函数,输出一个destructor。故本题应该选择D。

  • 第3题:

    根据输出结果填空完成下面程序。 include class Test { private: static int val; in

    根据输出结果填空完成下面程序。

    include<iostream.h>

    class Test

    {

    private:

    static int val;

    int a;

    public:

    static int func( );

    void sfunc(Test &r);

    };

    ______//初始化静态变量val

    int Test::func( )

    {

    return val++;

    }

    void Test::sfunc(Test &r)

    {

    r.a=125;

    cout<<"Result3="<<r.a;

    }

    void main( )

    {

    cout<<"Resultl="<<Test::func( )<<endl;

    Test A;

    cout<<"Result2="<<A.fune( )<<endl;

    A. sfunc(A);

    }

    输出结果为:

    Result1=201

    Result2=202

    Result3=125


    正确答案:int Test::val=200;
    int Test::val=200; 解析:类的静态成员变量必须要进行初始化才能使用,初始化时需要用域限定符::指明该变量所属的类名。

  • 第4题:

    应在下面程序下划线中填写的正确的语句是( )。 include using namespace std;

    应在下面程序下划线中填写的正确的语句是( )。 #include <iostream> using namespace std; class A{ public: void test(){cout<< "this is A!";} }; class B:public A{ void test(){ ______ //显示调用基类函数test() cout<< "this is B!"; } }; void main(){}

    A.A::test()

    B.test()

    C.B::test()

    D.this->test()


    正确答案:A
    解析:A::表示A的作用域。

  • 第5题:

    有如下程序: include using namespace std; class Test { public

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

    A.n=0

    B.n=1

    C.n=2

    D.n=3


    正确答案:A
    解析:本题考查构造函数和析构函数的调用。类的静态成员和成员函数是类属,不依赖于对象实例存在。

  • 第6题:

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

  • 第7题:

    有以下程序:include using namespace std;class count{ static int n;public: count

    有以下程序: #include <iostream> using namespace std; class count { static int n; public: count ( ) { n++; } static int test() { for (int i = 0; i < 4; i++ ) n++; return n; } }; int count :: n = 0; int main() { cout<<count :: test()<<" "; count c1, c2; cout<<count :: test()<<end1; return 0; } 执行后的输出结果是( )。

    A.4 10

    B.1 2

    C.22

    D.24


    正确答案:A
    解析:程序首先定义了类count,其内部含有private类型数据成员“staticintn;”,同时含有public类型构造函数count()和静态成员函数staticinttest(),这两个函数的功能分别是为对象申请系统资源并将静态数据成员n加1和将静态数据成员n加4。主函数前,程序将静态数据成员n初始化为0,该数据成员为所有类count的对象所共有的数据成员。主函数中,程序首先执行静态成员函数test()(由于test声明为static,因此其调用时无需通过具体对象)。而其执行过程中,静态数据成员n应该加4变成n=4,因此此处输出为4。此后程序创建对象c1和c2,由于在每次创建过程中都要调用构造函数count(),而每次调用count()函数后,静态数据成员n值都会加1,因此,创建两个对象之后,n值变为n=6;再次执行test()函数后,n的值再次加4,此时变为n=6+4=10。故程序全部执行后,变量n值变为10,而中间程序输出为“410”。

  • 第8题:

    有下列程序: 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。

  • 第9题:

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

  • 第10题:

    有如下程序:

    #include<iostream>

    usingnamespacestd;

    classTest

    {

    public:

    Test(){n+=2;}

    ~Test(){n-=3;}

    staticintgetNum(){returnn;}

    private:

    staticintn;

    };

    intTest::n=1;

    intmain()

    {

    Test*p=neWTest;

    deletep;

    cout<<"n="<<Test::getNum()<<endl;

    return0;

    }

    执行该程序的输出结果是( )。

    A.n=0

    B.n=1

    C.n=2

    D.n=3


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

  • 第11题:

    有如下程序: #inClude<iostream> using namespaCe std; Class test{ private: int a; publiC: test( ){Cout<<”ConstruCtor”<<endl;} test(int A.{Cout<<a<<endl;} test(Const test&_test){ a=test.a: Cout<<”Copy ConstruCtor”<<endl: } test( ){Cout<<”destruCtor”<<endl;} }; int main( ){ test A(3); return 0; } 执行这个程序的输出结果是( )。

    A.3

    B.ConstruCtor destruCtor

    C.Copy ConstruCtor destruCtor

    D.3 destruCtor


    正确答案:D
    本题考查默认构造函数和带参数的构造函数以及析构函数,本题中定义了一个对象A(3),对象带着参数,所以执行带参数的构造函数.输出3,然后执行析构溺数,输出destructor。所以本题答案为D。

  • 第12题:

    如下程序编译时发生错误,错误的原因是show函数实现语句错误,则正确的语句应该为______。

    include<iostream.h>

    class test

    {

    private:

    int hum;

    public:

    test(int);

    void show( );

    };

    test::test(int n){num=n;}

    test::show( ){cout<<num<<endl;}

    void main( )

    {

    test T(10):

    T.show( );

    }


    正确答案:void test::show( ){coutnumendl;}
    void test::show( ){coutnumendl;} 解析:show成员函数的声明和实现不一致,即实现部分应有void修饰符,这样才能编译通过。

  • 第13题:

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

    A.n=0

    B.n=1

    C.n=2

    D.n=3


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

  • 第14题:

    有如下程序: #include using namespace std; Class Test{ public: Test(){} Test(const Test&t){cout<<1;} ); Test fun(Test &u){Test t=u;retum t;} int main(){Test X,y;x=fun(y);retum 0;} 运行这个程序的输出结果是( )。

    A.无输出

    B.1

    C.11

    D.111


    正确答案:C
    解析:本题调用了fun函数。

  • 第15题:

    下面程序的运行结果是【】。 include using namespace std; class count{ static int n;

    下面程序的运行结果是【 】。

    include <iostream>

    using namespace std;

    class count

    {

    static int n;

    public:

    count()

    {

    n++;

    }

    static int test()

    {

    for(int i=0;i<4;i++)

    n++;

    return n;

    }

    };

    int count::n = O;

    int main()

    {

    cout<<count:: test()<<" ";

    count c1, c2;

    cout<<count:: test()<<endl;

    return 0;

    }


    正确答案:410
    410 解析:本题主要考查C++类中静态数据成员的使用。题目程序首先定义了类count,其内部含有private 类型数据成员static int n;同时含有public 类型构造函数 count()和静态成员函数static int test(),这两个函数的功能分别是为对象申请系统资源并将静态数据成员n加1和将静态数据成员n加4。主函数前,程序将静态数据成员n初始化为0,该数据成员为所有类count 的对象所共有的数据成员;主函数中程序首先执行静态成员函数test() (由于test 声明为 static,因此其调用时无需通过具体对象),其执行过程中,静态数据成员n应该加4变成n:4,因此此处输出为4;此后程序创建对象c1和c2,由于在每次创建过程中都要调用构造函数count(),而每次调用count()函数后,静态数据成员n值都会加1。因此,创建两个对象之后,n值变为n=6:再次执行test()函数后,n的值再次加4,因此变为n=6+4=10。故程序全部执行后,变量n值变为10,而中间程序输出为“410”。

  • 第16题:

    下列程序的运行结果是【 】。 include class test { private: int num; public: tes

    下列程序的运行结果是【 】。

    include <iostream. h>

    class test

    {

    private:

    int num;

    public:

    test()

    int TEST() {return num+100;}

    ~test()

    };

    test::test(){num=0;}

    test::~test(){cout<<"Destructor is active"<<endl;}

    void main()

    {

    test x[3]

    cout<<x[1]. TEST()<<endl;

    }


    正确答案:100
    100 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

  • 第17题:

    有如下程序: include using namespace std; class Test{ public: Tes

    有如下程序: #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
    解析:语句Test*p=new Test;会调用类的构造函数Test() {n+=2;},使n的值由原来的1变为3,然后delete p调用类的析构函数~Test() {n-=3;},因为n是static型变量,所以会在3的基础上减 3,使得输出结果为0。

  • 第18题:

    有如下程序: #inelude<iostream> usingnamespacestd; classTest { public: Test(){n+=2;} ~Test(){n-=3;} staticintgetNum(){returnn;} private: staticintn; }; intTest::n=1; intmain()

    Test*P=newTest: deleteP; cout<<"n="<<Test::getNum()<<endl; return0; } 执行后的输出结果是( )。

    A.n=0

    B.n=1

    C.n=2

    D.n=3


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

  • 第19题:

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


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

  • 第20题:

    有如下程序: #include<iostream> usingnamespacestd; classTest { public: Test(){n+=2;} ~Test(){n-=3;} staticintgetNum(){returnn;} private: staticintn; }; intTest::n=1; intmain() { Test*p=neWTest; deletep;

    cout<<"n="<<Test::getNum()<<endl; return0; } 执行该程序的输出结果是( )。

    A.n=0

    B.n=1

    C.n=2

    D.n=3


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

  • 第21题:

    有下列程序: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。

  • 第22题:

    下面程序的运行结果是()。includeusing namespace std;class TestClass{static int n;

    下面程序的运行结果是( )。 #include<iostream> using namespace std; class TestClass { static int n; public: TestClass () { n++; } static int test() { for(int i=0;i<4;i++) n++; return n; } }; int TestClass::n=0; int main() { cout<<TestClass::test()<<" "; TestClass c1,c2; cout<<TestClass::test()<<endl; return (); }

    A.4,10

    B.4,6

    C.0,6

    D.0,4


    正确答案:A
    解析:在主函数中首先调用TestClass中的test函数输出,类中的n为静态数据成员,可以为所有的对象共享这些数据,这里调用后n等于4。定义对象c1,c2调用构造函数后n=6,所以主函数再次执行“coutTestClass::test()endl;”后,n等于10。

  • 第23题:

    下列程序的输出结果是______。 include using namespace std; class Test( public: Te

    下列程序的输出结果是______。 #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; }

    A.024

    B.042

    C.420

    D.240


    正确答案:B
    解析:此题考查的是类的构造函数与析构函数的调用。语句 coutTcst::Count()"";;输出“0”,因为static型变量cnt的默认值是0;“T Test t1,t2;Test*pT3=new Test;Test*pT4=new Test;”调用4次类的构造函数,使得cnt的值增加到4,并输出4;然后delete pT4;delete pT3;调用两次析构函数,cnt的值变为2,并输出2。