给讲一道c++的题

来源:百度知道 编辑:UC知道 时间:2024/06/06 10:19:29
1.编写一个点类Point,再由它派生线段类Line。要求:Point只能有一个构造函数,而且构造函数只能有两个参数(点的x,y坐标)。并将下面不完整的测试主程序补充完整,以便对你定义的点类及线段类进行测试。
void main()
{
Point a;
Point b(7.8,9.8),c(34.5,67,8);
a=c;
cout<<”两点之间距离是:”<<a.Distance(b)<<endl;
Line s(7.8,8.34,34.5,67.8);
cout<<s.Display()<<endl;
}
说错了 没事

#include<iostream>
#include<cmath>

using namespace std;

class Point
{
public:
Point()
{
x = y = 0.0;
}
Point(double x, double y)
{
this->x = x;
this->y = y;
}
double Distance(Point &b)
{
double dist = sqrt( pow(x-b.x,2) + pow(y-b.y,2));
return dist;
}
void Display()
{
cout <<"("<<x<<","<<y<<")"<<endl;
}
private:
double x;
double y;

};

class Line:public Point
{
public:
Line()
{
}

Line(double x1, double y1, double x2, double y2):p1(x1,y1),p2(x2,y2)
{
}
double Display()
{
p1.Display();
p2.Display();

return p1.Distance(p2);
}
private:
Point p1;
Point p2;
};

void main()
{
Poi