问题 问答题

[说明]以下代码实现了对象引用作为函数参数,仔细阅读以下代码,分析运行结果,填入 (n) 处。[代码] #include<iostream.h> class Sample { int x,y; public: Sample() {x=y=0; } Sample (int i, int j ) {x=i; y=j; } void copy ( Sample &s )., void setxy ( int i, int j ) {x=i; y=j; } void print ( {cout<<"x="<<x<<end1 ; cout <<"y="<<y<<end1; }; void Sample: copy ( Sample &s ) { X=S.X; y=s.y; } void func ( Sample s1, Sample &s2 ) { s1.setxy ( 10,20 ); s2.setxy ( 30,40 ); } void main ( ) { Sample p ( 1,2 ) ,q; q.copy ( p ); time ( p,q ); p.print ( ); q.print ( ); } 运行结果 (1) (2) (3) (4)

答案

参考答案:

解析:(1)x=1 (2)v=2 (3)x=30 (4)y=40 本题考查考生对C++面向对象语言的掌握程度和人工运算代码的能力。本题说明对象引用作为函数参数的作用。Sample类中的copy()成员函数进行对象拷贝。在main()中先建立对象p和q,p与q对象的x,y值相同,调用func( )函数,由于第2个参数为引用类型,故实参发生改变;而第1个参数不是引用类型,实参不发生改变。

问答题
单项选择题