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

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


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

    有如下程序:includeusing namespace std;class A{public:static int a;void init(){a

    有如下程序: #include<iostream> using namespace std; class A{ public: static int a; void init(){a=1;} A(int a=2){init(); a++;} }; int A::a=0; A obj; int main() { cout<<obj.a; return 0; 运行时输出的结果是( )。

    A.0

    B.1

    C.2

    D.3


    正确答案:B
    解析:有如下程序:  #includeiostream>  using namespace std;  class A{  public:  static int a;  void init(){a=1;}  A(int a=2){init(); a++;}  };  int A::a=0;  A obj;  int main()  {  coutobj.a;  return 0;  运行时输出的结果是(  )。 

  • 第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 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”。

  • 第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题:

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

  • 第11题:

    下面程序的运行结果是()。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。

  • 第12题:

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

  • 第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题:

    若下面程序运行时输出结果为1,A,10.1 2,B,3.5 include using namespace std; int mai

    若下面程序运行时输出结果为

    1,A,10.1

    2,B,3.5

    include <iostream>

    using namespace std;

    int main()

    {

    void test(int, char, doubie【 】);

    test(1, 'A', 10.1 );

    test(2, 'B');

    return 0;

    }

    void test(int a, char b, double c)

    {

    cout<<a<<','<<b<<','<<c<<endl;

    }


    正确答案:3.5
    3.5 解析:本题考查了函数默认参数的应用。本题定义的函数test()仅仅是按顺序输出了三个形参值,题目中第1次调用该函数会输出1,A,10.1,但第2次调用少了一个实参却要求输出2,B,3.5。由此可见,应该将test()函数的第3个参数声明为默认参数,且默认值为3.5。故应该填入=3.5,或加上形参名c=3.5。

  • 第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题:

    有如下程序: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的值,并输出。

  • 第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题:

    有如下程序:include using namespace std;int s=0;class sample { static int n;publ

    有如下程序: #include <iostream> using namespace std; int s=0; class sample { static int n; public: sample(int i) { n=i; } static void add() { s+=n; } };

    A.2

    B.5

    C.7

    D.3


    正确答案:B
    解析:本题考核静态数据成员和静态成员函数的应用。程序中定义一个类sample,它包括一个静态数据成员n和一个静态成员函数add,并在类的构造函数中给类私有静态数据成员n赋值。在主函数main中,定义对象a(2)时,通过构造函数使静态数据成员n的值变为2,在定义对象b(5)时,通过构造函数使静态数据成员n=5(覆盖了前面的n=2),再执行sample::add()使全局变量s=5。

  • 第22题:

    有如下程序:includeusing namespace std;class MyClass{public:MyClass(){++count;}~

    有如下程序: #include<iostream> using namespace std; class MyClass{ public: MyClass(){++count;} ~MyClass(){--count;} static int getCount(){return count;} private: static int count; }; int MyClass::count=0; int main(){ MyCl

    A.121

    B.232

    C.221

    D.122


    正确答案:A
    解析: 本题考查的知识点是静态成员。类中的静态成员是解决同—:个类的不同对象之间的数据和函数共享问题的。静态成员被所有属于这个类的对象共享。这种共享与全局变量或全局函数相比,既没有破坏数据隐藏的厚则,又保证了安全性。题目中,首先定义了一个obj对象,其构造函数使MyClass的静态数据成员count增1,所以第一条输出语句输出的结果是1。然后通过指针动态创建了一个MyClass类的对象,构造函数再次被调用,count变为2,所以第2次输出结果是2。接下来是用dilete删除了刚才动态创建的MyClass对象。析

  • 第23题:

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