请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程中包含一个源程序文件main.cpp,其中有坐标点类Point、线段类Line和矩形类Rectangle的定义,还有main函数的定义。程序中两点间的距离的计算是按公式d=实现

admin2015-06-27  14

问题 请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程中包含一个源程序文件main.cpp,其中有坐标点类Point、线段类Line和矩形类Rectangle的定义,还有main函数的定义。程序中两点间的距离的计算是按公式d=实现的。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
Width:4
Height:6
Diagonal:7.2111
are,a:24
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include
#include
usingnamespacestd;
classPoint{//坐标点类
public:
constdoublex,y;
Point(doublex=0.0,doubley=0.
0):x(x),y(y){)
//**********found**********
doubledistanceTo(________)
const{
//到指定点的距离
returnsqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
};
classLine{//线段类
public:
constPointp1,p2;//线段的两个端点
Line(Pointp1,Pointp2):p1(p1),
p2(p2){)
//**********found**********
doublelength()const{returnp1.
________;)//线段的长度
};
classRectangle{//矩形类
public:
constPointupper_left;//矩形的左上角坐标
constPointdown_right;//矩形的右下角坐标
Rectangle(Pointpl,Pointp2):upperleft(p1),down_right(p2){}
doublewidth()const{//矩形水平边长度
//**********found**********
returnLine(upperLleft,________).
length();
}
doubleheight()const{//矩形垂直边长度
returnLine(upperleft,Point(upperleft.x,down_right.y)).length();
l
doublelengthOfDiagonal()const{
//矩形对角线长度
returnLine(upperleft,down_right).length();
}
doublearea()const{//矩形面积
//**********found**********
return________;
  }
};
intmain(){
Rectangler(Point(1.0,8.0),Point(5.0,2.0));
cout<<"Width:"<cout<<”Height:”<cout<<”Diagonal:”<Diagonal()<cout<<”area:"<return0;
}

选项

答案(1)const Point& p (2)distanceTo(p2) (3)Point(down_right.x, upper_left.y) (4)width() * height()

解析 (1)主要考查考生对成员函数的掌握,根据函数体可知形参应为const Point& p。
(2)主要考查考生对成员函数的掌握,求线段的距离直接调用函数distanceTo即可。
(3)主要考查考生对成员函数的掌握,函数功能求矩形水平边长度,这里程序直接构造Line类型并调用length函数。
(4)主要考查考生对成员函数的掌握,矩形面积为高乘宽,因此,调用函数height和width取得高和宽。
转载请注明原文地址:https://jikaoti.com/ti/DkXiFFFM
0

随机试题
最新回复(0)