问答题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,其中有矩形类Rectangle、函数show和主函数main的定义。程序中位于每个“//ERROR ****found****”下一行的语句有错误,请加以改正。改正后程序的输出结果应该是: Upper left=(1,8),down right=(5,2),area=24. 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。 #include <iostream> #include <cmath> using namespace std; class Rectangle{ double x1, y1; //左上角坐标 double x2, y2; //右下角坐标 public: // ERROR ********** found********** Rectangle (double x1, y1; double x2, y2){ this ->x1=x1; this ->y1=y1; this ->x2=x2; this ->y2=y2; } double getX1 ()const{ return x1; } double getY1 ()const{ return y1; } double getX2 ()const{ return x2; } double getY2 ()const{ return y2; } double getHeight () const { return fabs(y1-y2);} double getWidth () const { return fabs(x1-x2);} double area () const { return getH-eight()* getWidth(); } }; // ERROR ********** found********** void show(Rectangle r)const{ cout <<"Upper left = ("; // ERROR ********** found********** cout << r.x1 <<", " << r.y1 <<"), down right = (" <<r.x2 <<", " <<r.y2; cout << "), area =" << r. area () << ". " << endl; } int main(){ Rectangle r1 (1, 8, 5, 2); show(r1); return 0; }
【正确答案】(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();
【答案解析】[考点] 本题考查Rectangle类,其中涉及构造函数和const函数。 [解析] (1)主要考查考生对构造函数的掌握,函数的参数要使用“,”隔开,不能使用“;”。 (2)主要考查考生对const函数的掌握,程序中调用函数r.area(),该函数修改了成员值,因此不能使用const。 (3)主要考查考生对成员函数的掌握,类外函数不能直接调用类的私有成员,只能通过成员函数调用。