#include<iostream>
#include<math.h>
using namespace std;
class Point //点类
{
private:
float x,y;//定义一个点 x,y表示坐标。
public:
Point(float xx, float yy)//point的构造函数
{
cout<<"point构造函数"<<endl;
x = xx;
y = yy;
}
Point (Point &p)//point的拷贝构造函数
{
x = p.x;
y = p.y;
cout<<"point拷贝构造函数"<<endl;
}
float GetX(void)//获得x的值。
{
return x;
}
float GetY(void)//获得y的值
{
return y;
}
};
class Distance //直线类
{
private:
Point p1, p2 ; //构成直线的两个点
double dist; //距离值
public:
Distance(Point a, Point b); //Distance的构造函数
double GetDis(void) //返回距离值
{
return dist;
}
};
Distance::Distance (Point a,Point b):p1(a),p2(b) //Distance的构造函数
{
cout<<"Distance构造函数"<<endl;
double x = double(p1.GetX()-p2.GetX());
double y = double(p1.GetY()-p2.GetY());
dist = sqrt(x*x+y*y); //计算两点之间的距离
}
void main()
{
Point myp1(1, 1),myp2(0, 0);
Distance myd(myp1, myp2);
cout<<"the distance is:";
cout<<myd.GetDis()<<endl;
}
运行结果: