1、计算机二级(C+)上机考试 22 及答案解析(总分:-3.00,做题时间:90 分钟)1.改错题 使用 VC6 打开考生文件夹下的工程 test11_1,此工程包含一个源程序文件 test11_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下: ch1=7 ch2=9 源程序文件test11_1.cpp 清单如下: #include class Sample /* found */ char ch1,ch2 public: /* found */ friend void set(Sample s, char c1,char c2) s.ch1=c1; s.ch2=c2
2、 void print()coutch; fun(str,ch); cout a b; CPosition psA(a, b); cout a b; cout ch; fun(str,ch); cout a b; CPosition psA(a, b); cout a b; cout “The distance is “ psA.distance(a,b) _正确答案:((1) CPosition:CPosition() (2) void move(double ax, double ay)x += ax;y += ay; (3) return sqrt(pow(x-bx, 2) + pow(y-by, 2); )解析:本题主要考查考生对于类的定义和重载构造函数的掌握情况。在(3)中使用了基本的数学函数sqrt(x)求 x 的开方,pow(x,n)函数是求 x 的 n 次方。