friend function

来源:互联网 发布:尚学堂java 编辑:程序博客网 时间:2024/05/13 16:16
//============================================================================
// Name        : friend.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================


#include <iostream>
#include<cmath>
#include<math.h>
using namespace std;


class Point {
private:
double x, y;
public:
Point(double x, double y) {
this->x = x;
this->y = y;
}
double getX() {
return x;
}
double getY() {
return y;
}
friend double Distance(Point &a, Point &b);
};


double Distance(Point &a, Point &b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}


int main() {
Point a(3, 4), b(5, 6);
cout << Distance(a, b);
return 0;
}