#include <iostream>
- #include <cmath>
- using namespace std;
- class CPoint
- {private:
- double x; // 横坐标
- double y; // 纵坐标
- public:
- CPoint(double xx=0,double yy=0):x(xx),y(yy){}
- friend double L1(CPoint &,CPoint &);//友函数声明。
- double L2(CPoint c2);//成员函数声明
- double getx(){return x;}
- double gety(){return y;}
- };
- double L3(CPoint &,CPoint &);//一般函数声明。
- double L1(CPoint &c1,CPoint &c2)//友函数定义。
- {
- double d;
- d=sqrt((c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y));
- return d;
- }
- double CPoint::L2(CPoint c2)//成员函数定义。
- {
- double d;
- d=sqrt((c2.x-x)*(c2.x-x)+(c2.y-y)*(c2.y-y));
- return d;
- }
- double L3(CPoint &c1,CPoint &c2)//一般函数定义。
- {
- double d;
- d=sqrt((c1.getx()-c2.getx())*(c1.getx()-c2.getx())+(c1.gety()-c2.gety())*(c1.gety()-c2.gety()));
- return d;
- }
- int main()
- {
- CPoint c1(5,6),c2(8,2);
- cout<<"两点间的距离:";
- cout<<c1.L2(c2);
- cout<<endl;
- cout<<"两点间的距离:";
- cout<<L1(c1,c2);
- cout<<endl;
- cout<<"两点间的距离:";
- cout<<L3(c1,c2);
- cout<<endl;
- system("pause");
- return 0;
- }