C++:编写一个点类Point,再由它派生线段类Line。

2024-11-19 11:37:41
推荐回答(3个)
回答(1):

#include
#include

using namespace std;

class Point
{
private:
float x,y;
public:
Point(){}
Point(float a,float b):x(a),y(b){}
Point(Point& a);
float Distance(Point b);
};

Point::Point(Point& a)
{
x=a.x;
y=a.y;
}

float Point::Distance(Point b)
{
return sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y));
}

class Line:public Point
{
private:
Point a,b;
public:
Line(float x1,float y1,float x2,float y2):a(x1,y1),b(x2,y2){}
float Display();
};

float Line::Display()//这个地方不知道你要输出的是什么,但从你的源程序中知道要返回一个值,所以,我返回了线段的距离
{
return a.Distance(b);
}
int main()
{
Point a;
Point b(7.8,9.8),c(34.5,67.8);
a=c;
cout<<"两点之间距离是:"< Line s(7.8,8.34,34.5,67.8);
cout< return 0;
}

回答(2):

Class Point
{
private:
float x,y;
public:
Point(float x, float y)
{
this.x=x;
this.y=y;
}
float Distance(Point b)
{
return sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y));
}
}

Clase Line:public Point
{
private:
Point a;
Point b;
public:
Line(float x1,float y1,float x2,float y2)
{
a.x=x1;a.y=y1;
b.x=x2;b.y=y2;
}
void Display(void)
{
cout<<"A("<cout<<"B("<}
}

回答(3):

chass point
{
point(int a,int b)
{
x=a;
y=b;
}
private:
point *p;
int x,y;
public:
int getx()
{
return x;
}
int getx()
{
return y;
}

int Distance(point *q)
{
p=q;
return squre((x-q->getx())*(x-q->getx())+(y-q->gety())*(y-q->gety()));
}
}
chass line public:point
{
line(int a,int b,int c,int d)
{
x=a,y=b,x1=c,y1=d;
}
private:
int x1,x2;
public:
int Distplay()
{
return squre((x-x1)*(x-x1)+(y-y1)*(y-y1));
}
}