问答题 请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,其中有矩形类Rectangle、函数show和主函数main的定义。程序中位于每个“//ERROR****found****”下一行的语句有错误,请力11以改正。改正后程序的输出结果应该是:Upperleft=(1,8),downright:(5,2),areal=24.注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>#include<cmath>usingnamespacestd;classRectangle{doublex1ty1;//左上角坐标doublex2,y2;//右下角坐标public://ERROR**********found**********Rectangle(doublex1,y1;doublex2,y2){this->x1=x1;this->y1=y1;thiS->x2=x2;this->y2=y2;}doublegetXl()const{returnx1;}doublegetYl()const{returny1;}doublegetX2()const{returnx2;}doublegetY2()const{returny2;}doublegetHeight()const{returnfabs(y1—y2);}doublegetWidth()constfreturnfabs(x1—x2);}doublearea()const{returngetHeight()*getWidth();}};//ERROR**********found**********voidshow(Rectangler)const{cout<<"Upperleft=(";//ERROR**********found**********cout<<r.x1<<”,”<<r.y1<<”),downright=("<<r.x2<<","<<r.y2;cout<<"),area="<<r.area()<<"."<<end1;}intmain(){Rectanglerl(1,8,5,2);show(r1);return0;}
【正确答案】正确答案:(1)Rectangle(double x1, double y1, double x2, double y2){ (2)void show(Rectangle r){ (3)cout<< r.getX1()<< " , "<< r.getY1()<< "),down right=("<< r.getX2()<< " , "<< r.getY2();
【答案解析】解析:(1)主要考查考生对构造函数的掌握,函数的参数要使用","隔开,不能使用";"。 (2)主要考查考生对const函数的掌握,程序中调用函数r.area(),该函数修改了成员值,因此不能使用const。 (3)主要考查考生对成员函数的掌握,类外函数不能直接调用类的私有成员,只能通过成员函数调用。