问答题 请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程文件proj3,此工程中包含一个源程序文件p,oj3.cpp,其中定义了用于表示平面坐标系中的点的类MyPoint和表示矩形的类MyRectan~e;程序应当显示:(0,2)(2,2)(2,0)(0,0)4但程序中有缺失部分,请按照以下提示,把缺失部分补充完整:(1)在“//**1******found****”的下方是构造函数的定义,它用参数提供的左上角和右下角的坐标对up_left和down_fight进行初始化。(2)在“//**2******found****”的下方是成员函数getDownLeft的定义中的一条语句。函数getDownLeft返回用MyPoint对象表示的矩形的左下角。(3)在“//**3******found****”的下方是成员函数area的定义,它返回矩形的面积。注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。//proj3.cpp#include<iostream>usingnamespacestd;classMyPoint{//表示平面坐标系中的点的类doublex;doubley;public:MyPoint(doublex,doubleY){this->x=x;this->y=y;)doublegetX()const{returnx;)doublegetY()const{returnY;}voidshow()const{cout<<'('<<x<<','<<y<<')';)};classMyRectangle{//表示矩形的类MyPointup_left;//矩形的左上角顶点MyPointdown_riqht;//矩形的右下角顶点public:MyRectangle(MyPointupleft,MyPointdownright);MyPointgetUpLeft()const{returnup_1eft;}//返回左上角坐标MyPointgetDownRight()const{returndown_right;//返回右下角坐标MyPointgetUpRight()const;//返回右上角坐标MyPointgetDownLeft()const;//返回左下角坐标doublearea()const;//返回矩形的面积);//**1************found**********MyRectangle::MyRectangle(________):up_left(p1),down_right(p2){)MyPointMyRectangle::getUpRight()const{returnMyPoint(down_right.getX(),up_left.getY());}MyPointMyRectangle::getDownLeft()const{//**2************found**********returnMyPoint(________);}//**3************found**********________area()Iconst{return(getUpLeft().getX()-getDownRight().getX())*(getDownRight().getY()-getUpLeft().getY());}intmain(){MyRectangler(MyPoint(0,2),MyPoint(2,0));r.getUpLeft().show();r.getUpRight().show();r.getDownRight().show();r.getDownLeft().show();cout<<r.area()<<end1;return0;}
【正确答案】正确答案:(1)MyPointp1,MyPointp2 (2)up_left.getX(),downright.getY() (3)doubleMyRectangle::
【答案解析】解析:(1)考查构造函数,构造函数中的参数要给私有成员赋值,在下句中up—left(p1),down—right(p2){}指出私有成员赋值要使用形参p1和p2,因此这里参数要定义为MyPointp1,MyPointp2。 (2)主要考查成员函数的返回语句,MyPointMyRectangle::getDownLeft()const函数要求返回一个左下角的点坐标,因此使用语句MyPoint(up_left.getX(),down_right.getY());。 (3)主要考查成员函数的定义,在MyRectangle类中已经声明doublearea()const,因此此处只要添加doubleMyRect.删e::即可。