若有以下程序:#include <iostream>using namespace std;class datapublic:int x;data(int x) { d

题目
若有以下程序:include using namespace std;class datapublic:int x;data(int x) { d

若有以下程序: #include <iostream> using namespace std; class data public: int x; data(int x) { data: :x=x; }; class A private: data d1; public: A(int x): d1(x){} void dispa() { cout<<d1.x<<","; } }; class B: public A { private: data d2; public: B(int x): A(x-1),d2(x){} void dispb() { cout<<d2.x<<end1; } }; class C: public B { public: C(int x): B(x-1){} void disp() { dispa(); dispb(); } }; int main() { C obj(5); obj.disp(); return 0; 程序执行后的输出结果是 }

A.5,5

B.4,5

C.3,4

D.4,3

参考答案和解析
正确答案:C
解析:本题考核派生类的定义和访问权限。本题涉及多层次的继承关系。类B是类A的派生类,类C又是类B的派生类。类C中的构造函数调用了类B的构造函数来初始化类B的私有数据成员,而类B的构造函数又调用了类A的构造函数来初始化类A的私有数据成员。由此可知,程序最后的输出为3,4。
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

以下程序执行后的输出结果是include using namespace std;void try(int,int,int,int)

以下程序执行后的输出结果是 #include <iostream> using namespace std; void try(int,int,int,int); int main () { int x,y,z,r; x =1 ; y = 2; try(x,y,z,r); cout<<r<<endl; return 0; } void try(int x,int y,int z,int r) { z = x+y; x = x*x; y = y*y; r = z+x+y; }

A.18

B.9

C.10

D.不确定


正确答案:D
解析:本题考核函数调用(参数的传递)。本题常见的错误解答是:把x=1,y=2代入到函数try中,逐步计算出r=8。最后得到r的输出值是8。以下是正确解答,根据程序逐步分析:程序中定义了一个名为try的void型函数,即函数try没有任何返回值。而try函数在main函数中是以一条独立语句的方式被调用的,且main函数最后输出变量r的值。但在main函数中,并没有对变量r赋值。在c++语言中,数据只能从实参单向传递给形参,称为按值传递。也就是说,当简单变量作为实参时,用户不能在函数中改变对应实参的值。所以虽然在函数try中,r的值为8,但它并不能传递给实参,当然最终的输出肯定是不确定的随机数了。

第2题:

若有以下程序:includeusing namespace.std;class data{public: int x; data (int x)

若有以下程序: #include<iostream> using namespace.std; class data { public: int x; data (int x) { data::x=x; } ); class A { private: data d1; public: A(int x) :dl(X){} void dispa() { cout<<dl.X<<","; } }; class B:public A { private: data d2; public: B(int x) : A(x-1),d2(x){} void dispb() { cout<<d2.x<<endl; } }; class C:public B { public: C(int x) :B(x-1){} void disp() { dispa(); dispb(); } }; int main() { C obj(5); obj.disp(); return 0; } 程序执行后的输出结果是

A.5,5

B.4,5

C.3,4

D.4,3


正确答案:C
解析:本题考核派生类的定义和访问权限。本题涉及多层次的继承关系。类B是类A的派生类,类C又是类B的派生类。类C中的构造函数调用了类B的构造函数来初始化类B的私有数据成员,而类B的构造函数又调用了类A的构造函数来初始化类A的私有数据成员。由此可知,程序最后的输出为3,4。

第3题:

若有以下程序段:include using namespace std;int main (){ int a[]={1,4,5}; int *p

若有以下程序段: #include <iostream> using namespace std; int main () { int a[]={1,4,5}; int *p=&a[0],x=6, y,z; for (y=0; y<3; y++) z= ( (* (p+y) <x) ? *(p+y) :x); cout<<z<<end1; return 0; } 程序运行后的输出结果是( )。

A.1

B.4

C.5

D.2


正确答案:C
解析:本题考核指针的运算。程序首先定义了一整型数组a,并赋初值1,4,5。即a[0],a[1]和a[2]的值分别为1,4和5。程序还定义了一个整型指针p,并将其初始化且指向数组元素a[0]。在for循环语句中,赋值语句“z=((*(p+y)x)?*(p+y):x);”可等价为“z=(a[y]x)?a[y]:x”。因为表达式中的“*(p+y)=p[y]=a[y]”。for语句每循环一次,就给变量z重新赋值一次,z的值其实就是数组和x相比较,将数组a中的最大值赋给z。当y=2后,退出循环,输出结果是5。

第4题:

00330038003000301585067361821下列程序段执行结果是___________。 x = 1 print(type(x)) x = 1.0 print(type(x)) x = '1.0' print(type(x)

A.<class 'int'> <class 'float'> <class 'str'>

B.<class 'float'> <class 'int'> <class 'str'>

C.<class 'str'> <class 'float'> <class 'int'>

D.<class 'str'> <class 'int'> <class 'float'>


A

第5题:

以下程序执行后的输出结果是include.using namespace std;void try(int,int,int,int)

以下程序执行后的输出结果是 #include<iostream>. using namespace std; void try(int,int,int,int); int main() { int x,y,z,r; x=1; y=2; try(x,y,z,r); cout<<r<<end1; return 0; } void try(int x,int y, int z,int r) { z = x+y; x = x*x; y = y*y; r = z+x+y; }

A.18

B.9

C.10

D.不确定


正确答案:D
解析:本题考核函数调用(参数的传递)。本题常见的错误解答是:把x=1,y=2代入到函数try中,逐步计算出r=8。最后得到r的输出值是8。以下是正确解答,根据程序逐步分析:程序中定义了一个名为try的void型函数,即函数try没有任何返回值。而try函数在main函数中是以一条独立语句的方式被调用的,且main函数最后输出变量r的值。但在main函数中,并没有对变量r赋值。在C++语言中,数据只能从实参单向传递给形参,称为按值传递。也就是说,当简单变量作为实参时,用户不能在函数中改变对应实参的值。所以虽然在函数try中,r的值为8,但它并不能传递给实参,当然最终的输出肯定是不确定的随机数了。

第6题:

以下程序执行后的输出结果是includeusing namcspace std;void try(int,int,int,int);

以下程序执行后的输出结果是 #include<iostream> using namcspace std; void try(int,int,int,int); int main() { int x,y,z,r; x=1; y=2; try(x,y,z,r); cout<<r<<end1; return 0; } void try(int x,int y, int z,int r) { z = x+y; x = x*x; y = y*y; r = z+x+y; }

A.18

B.9

C.10

D.不确定


正确答案:D
解析:本题考核函数调用(参数的传递)。本题常见的错误解答是:把x=1,y=2代入到函数try中,逐步计算出r=8。最后得到r的输出值是8。以下是正确解答,根据程序逐步分析:程序中定义了一个名为try的void型函数,即函数try没有任何返回值。而try函数在main函数中是以一条独立语句的方式被调用的,且main函数最后输出变量r的值。但在main函数中,并没有对变量r赋值。在C++语言中,数据只能从实参单向传递给形参,称为按值传递。也就是说,当简单变量作为实参时,用户不能在函数中改变对应实参的值。所以虽然在函数try中,r的值为8,但它并不能传递给实参,当然最终的输出肯定是不确定的随机数了。

第7题:

以下程序执行后的输出结果是()。includeusing namespace std;void try(int,int,int,in

以下程序执行后的输出结果是( )。 #include<iostream> using namespace std; void try(int,int,int,int); int main() { int x,y,z,r; x=1; y=2; try(x,y,z,r); cout<<r<<endl; return 0; } void try(int x,int y, int z,int r) { z = x+y; X = x*x; y = y*y; r = z+x+y; }

A.18

B.9

C.10

D.不确定


正确答案:D

第8题:

以下程序执行后的输出结果是( )。include usingnamespacestd;void try(int,int,int,in

以下程序执行后的输出结果是( )。 #include <iostream> using namespace std; void try(int,int,int,int); int main ( ) { int x,y,z,r; x=1; y=2; try(x,y,z,r); cout<<r<<end1; return 0; } void try(int x,int y, int z,int r) { z = x+y; x = X*X; y = y*y; r = z+x+y; }

A.18

B.9

C.10

D.不确定


正确答案:D
解析:本题常见的错误解答是:把x=1,y=2代入到函数try中,逐步计算出r=8。最后得到r的输出值是8。下面是正确解答。根据程序逐步分析:①程序中定义了一个名为try的void型函数,即函数try()没有任何返回值。②而try()函数在主函数中是以一条独立语句的方式被调用的,且主函数最后输出变量r的值。③但在主函数中,并没有对变量r赋值。④在C++语言中,数据只能从实参单向传递给形参,称为按值传递。也就是说,当简单变量作为实参时,用户不能在函数中改变对应实参的值。所以,虽然在函数try()中,r的值为8,但它并不能传递给实参,当然最终的输出肯定是不确定的随机数了。

第9题:

阅读下列说明和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里面的实参。

第10题:

下列程序段执行结果是___________。 x = 1 print(type(x)) x = 1.0 print(type(x)) x = '1.0' print(type(x)

A.<class 'int'> <class 'float'> <class 'str'>

B.<class 'float'> <class 'int'> <class 'str'>

C.<class 'str'> <class 'float'> <class 'int'>

D.<class 'str'> <class 'int'> <class 'float'>


C.循环执行1次