计算机二级

有如下程序: include using namespace std; class Sac{ int n; public: Sac():n(4){co有如下程序:include<iostream>using namespace std;class Sac{int n;public:Sac():n(4){cout<<n;}Sac(int k):n(k){cout<<n;}~Sac(){cont<<n+n;}};int main(){Sac s1,*s2;s2=new Sac(3);delete s2;ret

题目
有如下程序: include using namespace std; class Sac{ int n; public: Sac():n(4){co

有如下程序:

include<iostream>

using namespace std;

class Sac{

int n;

public:

Sac():n(4){cout<<n;}

Sac(int k):n(k){cout<<n;}

~Sac(){cont<<n+n;}

};

int main(){

Sac s1,*s2;

s2=new Sac(3);

delete s2;

return 0;

}

运行时的输出结果是______。

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

第1题:

阅读下列说明和C++代码,填写程序中的空(1)~(6),将解答写入答题纸的对应栏内。
【说明】
以下C++代码实现一个简单绘图工具,绘制不同形状以及不同颜色的图形。部分类及其关系如图6-1所示。



【C++代码】
#include?#include?using?namespace?std;class?DrawCircle?{??????//绘制圆形,抽象类? ? ? public: (1);//定义参数为?int?radius,?int?x,?inty? ?virtual~DrawCircle()?{?}};class?RedCircle:public?DrawCircle?{????//绘制红色圆形? ? ? ? public: void?drawCircle(intradius,?int?x,?int?y)?{cout?<?drawCircle?=?drawCircle;? }? ?virtual~shape()?{?}? public:? ?virtual?void?draw()?=?0;};class?Circle:public?Shape?{????//圆形? ? private:? ? ?int?x,y,radius;? ? public:? Circle(int?x,inty,int?radius,DrawCircle?*drawCircle)? (3)? {? this->x?=?x;? ?this->y?=?y;? ? this->radius?=?radius; }? ? ? public:? void?draw(){? drawCircle?-> (4); }};int?main(){Shape?*redCirclenew?Circle(100,100,10,????(5)????);//绘制红色圆形? Shape?*greenCircle=new?Circle(100,100,10, (6)??);//绘制绿色圆形redCircle >draw();? ?greenCircle?->draw();? ?return?0;}


答案:
解析:
(6)(1)void drawCircle (int radius,int x,int y)
(2)DrawCircle*drawCircle
(3)drawcircle
(4)drawCircle(radius,x,y)
(5)new RedCircle()
(6)new GreenCircle()【解析】
第一空是填接口里面的方法,在接口的实现里面找,可以发现应该填void drawCircle (int radius,int x,int y)。
第二空可以根据后面this drawCircle=drawCircle判断,这里应该有一个drawCircle属性,因此应该填)DrawCircle drawCircle。
第三空这里填drawcircle,用-> drawcircle来引用父类的成员。
第四空调用drawCircle(radius,x,y)方法。
第五、六空分别创建一个红色圆形对象和一个绿色圆形对象作为Circle里面的实参。

第2题:

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

第3题:

有如下程序:include using namespace std;class Base{private:charc;public:Base(cha

有如下程序:#include <iostream>using namespace std;class Base{private: char c;public: Base(char n) :c(n){} ~Base() { cout<<c; } };class Derived: public Base{private: char c; public: Derived(char n):Base(n+1),c(n) {} ~Derived() { cout<<c; }};int main (){ Derived obj ('x'); return 0;}执行上面的程序净输出

A.xy

B.yx

C.x

D.y


正确答案:A
解析:在C++中,由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反。在此题的程序中,在主函数main结束时,派生类Derived对象obj将被删除,所以就会调用对象的析构函数。先调用派生类的析构函数,输出x,然后调用基类的析构函数,输出y。

第4题:

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

第5题:

有如下程序:includeincludeusing namespace std;class BASE{char c;public

有如下程序: #include<iostream>#include<iosream> using namespace std; class BASE{ char c; public; BASE(char n):c(n){} virtual ~ BASE(){cout<<c;} }; class DERIVED; public BASE{ char c; public: DERIVED (char n): BASE (n+1)

A.XY

B.YX

C.X

D.Y


正确答案:A

第6题:

有如下程序: include using namespace std; class Pair{ int m, n; public: Pair(int

有如下程序:

#include<iostream>

using namespace std;

class Pair{

int m, n;

public:

Pair(int j, int k):m(j), n(k){}

int get(){return m;}

int get()const{return m+n;)

};

int main() {

Pair a(3, 5);

const Pair b(3, 5);

cout<<a. get()<<b. get();

return 0;

}

运行时的输出结果是( )。

A) 33

B) 38

C) 83

D) 88

A.

B.

C.

D.


正确答案:B

第7题:

有如下程序:includeUsing namespace std;Class Demo{public:Demo(){cout<<“default

有如下程序: #include<iostream.h> Using namespace std; Class Demo { public: Demo(){ cout<<“default constructor\n”;} Demo(const Demo &x){ cont<<“copy constructor\n”;} }; Demo usercode(Demob){Demo c(b);return c;} int main() {

A.1和1

B.1和2

C.2和3

D.2和4


正确答案:D
解析: 本题考查的是引用调用,因为函数swap引用调用参数a,所以在swap函数中a的变化会改变主函数中a的值,即a自加1,但b的值并未改变。

第8题:

有如下程序:includeusing namespqce Std;class TestClass{private;Char C;public;Tes

有如下程序: #include<iostream> using namespqce Std; class TestClass { private; Char C; public; TestClass(char n):c(n){} ~TestClass() { cout<<c; }; class TestClassl:public TestClass { private: Char C; public:

A.xy

B.yx

C.x

D.y


正确答案:A
解析: 本题中TestClass为基类,TestClassl为派生类。在主函数中定义TestClassl对象obj(‘x’),“TestClassl(char n):TestClass(n+1),c(n){}”,所以选输出x,然后调用基类构造函数,’x’+1=‘y’,所以输出y,即答案为xy。

第9题:

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