计算机二级

下列程序的运行结果是【 】。 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 ma

题目
下列程序的运行结果是【 】。 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;

}

如果没有搜索结果,请直接 联系老师 获取答案。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

如下程序编译时发生错误,错误的原因是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修饰符,这样才能编译通过。

第2题:

下面程序的结果是 include class test{private: int num; publi

下面程序的结果是 #include<iostream.h> class test{ private: int num; public: test( ); int getint( ) {return num;} ~test( );}; test::test( ) { num=0;} test::~test( ) { cout<<"Destructor is active"<<endl;} void

A.Exiting main Destructor is active Destructor is active Destructor is active

B.Exiting main Destructor is active Destructoris active

C.Exiting main Destructoris active

D.Exiting main


正确答案:A
解析:C++语言中析构函数是在程序退出不用该类的对象时进行调用。

第3题:

下面程序的运行结果为includeclass A{ int num;public: A(int){num=i;} A(ABm){num

下面程序的运行结果为 #include<iostream.h> class A { int num; public: A(int){num=i;} A(ABm){num=a.num++;} void print(){cout<<num;} }; void main() { Aa(1),b(a); a.print(); b.print(); }

A.11

B.12

C.21

D.22


正确答案:C
解析:本题;号查考生对拷贝构造函数的掌握。因为++运算是右结合的,所以在使用a对b赋值时,b的num变为l而a的num变为2(先赋值再自加)。

第4题:

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

第5题:

下列程序的运行结果是 includeclass Location{private:int X.Y;public:void init(i

下列程序的运行结果是 #include<iostream.h> class Location{ private: int X.Y; public: void init(int=0,int=0); void valueX(int val){X=val;} int valueX( ){ return X;} void valueY

A.5 0 6 4

B.0 0 6 4

C.5 0 6 2

D.0 0 6 2


正确答案:A
解析:本题中有成员函数和它的重载函数,要注意它们的不同,在本题中先调用了init函数,初始化了X,Y,都为0,valueX(5);又将X变为5,所以输出5和0,然后初始化init(6,2),接着又valueY(4);将X,Y设为6和4,所以输出6和4。

第6题:

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

第7题:

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

下列程序的运行结果是______。

include<iostream.h>

class test

{

private:

int hum;

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 Destructor is active Destructor is active Destructor is active
100 Destructor is active Destructor is active Destructor is active 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

第8题:

下列程序的运行结果是【 】。 include class SomeClass { public: SomeClass(int va

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

include <iostream. h>

class SomeClass

{

public:

SomeClass(int value) { some_value=value;};

void show_data(void) { cout<<data<<"<<~some_value<<endl; };

static void set_data(int value) {data=value; }

private:

static int data;

int some_value

};

int SomeClass::data

void main(void)

{

SomeClass my_class(1001),your_class(2002);

your_class. set_data(4004);

my_elass. show_data()

}


正确答案:4004 1001
4004 1001 解析:本题考查静态成员变量在不同对象间的共享现象。无论哪个对象修改了其静态变量的值,其他对象再访问该变量时已经发生了变化。

第9题:

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