507.cpp

来自「C++实训教程」· C++ 代码 · 共 30 行

CPP
30
字号
//507.CPP
//demo friend function
#include <iostream.h>
#include <math.h>

class P  //Point
{
  friend double Dist(const P& p1,const P& p2);
   int x,y;   
public:
   P(){ x = 0; y = 0 ;}
   P(int a,int b)
   { x = a; y= b; }
};
double Dist(const P& p1,const P& p2)
{
	double dx = p1.x - p2.x;
	double dy = p1.y - p2.y;
	return sqrt(dx*dx + dy*dy);
}
int main()
{

   P p1,p2(3,4);  //两个对象
   cout << Dist(p1,p2)<< endl;
   return 0;
}
/*
5
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?