include#
includeusingnamespacestd;
classPoint{public:
Point(inta,intb):
x(a),y(b){cout~Point()
{coutvoidshow()
{coutdoubledistance
(Pointp){returnsqrt((p.y-y)*(p.x-y)+(p.x-x)*(p.x-x));
}private:intx;
inty;};
intmain()
{Pointa(0,0);
Pointb(1,1);
coutreturn0;
}。
扩展资料
定义一个类Point,代表一个点。
public属性有x和y,方法有显示点坐标show(),构造函数有两个参数分别给x,y赋值。在main方法中构造两个对象。
再创建一方法(getMiddle)为取两个点构成线段的中点的坐标,参数为2个点对象,调用此方法后得到一个新的点,编写Application,显示该对象的坐标值。
#include
class Point
{
private:
double x;
double y;
public:
Point(): x(0.0), y(0.0) {};
Point(double x, double y)
{
this->x = x;
this->y = y;
}
double getX() { return x; }
double getY() { return y; }
};
class Line
{
private:
Point p1;
Point p2;
public:
Line(Point &p1, Point &p2)
{
this->p1 = p1;
this->p2 = p2;
}
double getDistance()
{
return sqrt((p1.getX() - p2.getX()) * (p1.getX() - p2.getX()) +
(p1.getY() - p2.getY()) * (p1.getY() - p2.getY()));
}
};
int main(int argc, char* argv[])
{
Point p1(1.0, 2.0), p2(4.0, 5.0);
Line l1(p1, p2);
printf("Distance: %f\n", l1.getDistance());
return 0;
}