问答题

请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中定义了Shape类和Point类。Shape类表示抽象的形状,其成员函数draw声明了显示形状的接口。Point是Shape的派生类,表示平面直角坐标系中的点,其成员函数draw用于在屏幕上显示Point对象;成员函数distance用于计算两个点之间的距离。提示:在平面直角坐标系中,点(x1,y2)和点(x2,y2)之间的距离为:d=
;标准库函数spn用于求平方根。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: (3,0) (0,4) Distance=5 注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。 #include <iostream> #include <math.h> using namespace std; class Shape { public: //********** found********** ______ virtual ~Shape() {} }; class Point : public Shape { public: Point(double x, double y):x_(x), y_(y) {} virtual void draw() const; //********** found********** double distance(______) const { return sqrt ((x_- pt.x_)* (x_-pt.x_) + (y_ - pt.y_)* (y_- pt.y_)); } private: //********** found********** ______ }; void Point::draw() const { //********** found********** cout << ’(’<<______<< ’)’<< endl; } int main ( ) { Point* pt1 = new Point (3, 0); Point* pt2 = new Point (0, 4); Shape* s = pt1; a->draw(); s = pt2; a->draw(); cout << "Distance = " << pt1 -> distance(* pt2) << endl; delete pt1; delete pt2; return 0; }

【参考答案】

(1)visual void draw()const=0; (2)const Point&pt (3)double x_......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)