请打开考生文件夹下的解决方案文件proj3,此工程中包含一个源程序文件proj3.cpp,其中定义了用于表示平面坐标系中的点的类MyPoint和表示矩形的类MyRectangle;程序应当显示:
(0,2)(2,2)(2,0)(0,0)4
但程序中有缺失部分,请按照以下提示,把缺失部分补充完整:
(1)在“//**1** ****found****”的下方是构造函数的定义,它用参数提供的左上角和右下角的坐标对up_left和down_right进行初始化。
(2)在“//**2** ****found****”的下方是成员函数getDownLeft的定义中的一条语句。函数getDownLeft返回用MyPoint对象表示的矩形的左下角。
(3)在“//**3** ****found****”的下方是成员函数area的定义,它返回矩形的面积。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。
1 //proj3.cpp
2 #include<iostream>
3 using namespace std;
4 class MyPoint{//表示平面坐标系中的点的类
5 double x;
6 double y;
7 public:
8 MyPoint(double x,double y){this->x=x;this->y=y;}
9 double getX()const{return x;}
10 double getY()const{return y;}
11 void show()const(cout<<'('<<x<<','<<y<<:')';}
12 };
13 class MyRectangle{ //表示矩形的类
14 MyPoint up left; //矩形的左上角顶点
15 MyPoint down right; //矩形的右下角顶点
16 public:
17 MyRectangle(MyPoint upleft,MyPoint downright);
18 MyPoint getUpLeft()const{return up left;}//返回左上角坐标
19 MyPoint getDownRight()const {return down right;} //返回右下角坐标
20 MyPoint getUpRight()const;
//返回右上角坐标
21 MyPoint getD0wnLeft()const;
//返回左下角坐标
22 double area()const; //返回矩形的面积
23 };
24 //**1** **********found**********
25 MyRectangle:: MyRectangle (____________): up left(p1),down_right(p2){}
26 MyPoint MyRectangle::getUpRight ()const
27 {
28 return MyPoint(down_right.getX(),up_left.getY());
29 }
30 MyPoint MyRectangle::getDown-Left()const
31 {
32 //**2** **********found**********
33 return MyPoint(___________);
34 }
35 //**3** **********found**********
36 ___________area()const
37 {
38 return(getUpLeft().getX()-getDownRight().getX()) *(getDownRight().getY()-getU-pLeft().getY());
39 }
40 int main()
41 {
42 MyRectangle r(MyPoint(0,2), MyPoint(2,0));
43 r.getUpLeft().show();
44 r.getUpRight().show();
45 r.getDownRight().show();
46 r.getDownLeft().show();
47 cout<<r.area()<<endl;
48 return 0;
49 }
【正确答案】(1)MyPoint p1,MyPnint p2
(2)up_left.getX(),down_right.getY()
(3)double MyRectangk::
【答案解析】(1)考查构造函数,构造函数中的参数要给私有成员赋值,在下句中up_left(p1),down_right(p2){}指出私有成员赋值要使用形参p1和p2,因此这里参数要定义为MyPoint p1,MyPoint p2。
(2)主要考查成员函数的返回语句,MyPoint My Rectangle::getDownLeft()const函数要求返回一个左下角的点坐标,因此使用语句MyPoint(up_left.getx(),down_right.getY());。
(3)主要考查成员函数的定义,在MyRectangle类中已经声明double area()const,因此此处只要添加double MyRectangle::即可。