#include <iostream>
#include <Cmath>
using namespace std;
class Pixel;
class Test
{
public:
void printX(Pixel p);
};
class Pixel
{
private:
int x,y;
public:
Pixel(int x0,int y0)
{
x=x0;
y=y0;
}
void printxy()
{
cout<<"pixel:("<<x<<","<<y<<")"<<endl;
}
//friend double getDist(Pixel p1,Pixel p2);
double getDist(Pixel p);
};
//以下是友元函数的用法
double getDist(Pixel p1,Pixel p2);
{
double xd=double(p1.x - p2.x);
double yd=double(p1.y - p2.y);
return sqrt(xd*xd+yd*yd);
}
//以下是成员函数的用法
double Pixel::getDist(Pixel p)
{
double xd=double(this->x - p.x);
double yd=double(this->y - p.y);
return sqrt(xd*xd+yd*yd);
}
int main()
{
Pixel p1(0,0),p2(10,10);
p1.printxy();
p2.printxy();
cout<<"(p1,p2)之间的距离="<<p1.getDist(p2)<<endl;
//Test t;
cout<<"从友元函数中输出..."<<endl;
//t.printX(P);
return 1;
}
运行结果: