niusouti.com

( 28 )有如下程序#include <iostream>using namespace std;class Test{public:Test(){ }Test(const Test &t) {cout<<1;}};Test fun(Test &u) {Test t=u; return t;}int main(){Test x,y; x=fun(y); return 0;}运行这个程序的输出结果是A )无输出B ) 1C ) 11D ) 111

题目

( 28 )有如下程序

#include <iostream>

using namespace std;

class Test{

public:

Test(){ }

Test(const Test &t) {cout<<1;}

};

Test fun(Test &u) {Test t=u; return t;}

int main(){Test x,y; x=fun(y); return 0;}

运行这个程序的输出结果是

A )无输出

B ) 1

C ) 11

D ) 111


相似考题
更多“( 28 )有如下程序#include &lt;iostream&gt;using namespace std;class Test{public:Test(){ }Te ”相关问题
  • 第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;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。

  • 第3题:

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

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

  • 第6题:

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