问答题
;标准库函数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; }