c++用类中的成员函数做友元函数
//计算两点之间距离 #include<iostream> #include<MATH.H> using namespace std; class Point; class test { public: double dist(Point &p1,Point &p2); }; class Point { private: int x,y; public: Point(int xx=0,int yy=0) { x=xx; y=yy; } void display() { cout<<"("<<x<<","<<y<<")"; } friend double test::dist(Point &p1,Point &p2); }; double test::dist(Point &p1,Point &p2) { return sqrt(double((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))); } int main() { Point p1(3,4),p2(4,5); p1.display(); cout<<"----->"; p2.display(); test a; cout<<"距离:"<<a.dist(p1,p2)<<endl; return 0; }