公有成员函数与友元函数的转换
#include <stdafx.h>#include <iostream.h>
#include <cmath>
class Point
{
double x;
double y;
public:
Point(double a,double b)
{
x=a;
y=b;
}
friend double dist(Point a,Point b);
};
double dist(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void main()
{
Point p1(1,2);
Point p2(5,2);
cout<<dist(p1,p2)<<endl;
}
将友元函数dist(point a,point b)改为Point类的公有成员函数 Point ::dist(point &b),请修改主函数中的相应代码时程序功能不变。