您的位置:首页 > 编程语言 > C语言/C++

C++友元函数的使用学习笔记

2013-10-19 23:44 267 查看
#include <iostream>
#include <cmath>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

class Point{
	private:
		int X,Y;
	public:
		Point(double xx,double yy){
			X=xx;
			Y=yy;
		}
		
		double GetX(){
			return X;
		}
		
		double GetY(){
			return Y;
		}
		
		friend double distances(Point&,Point&);
};

double distances(Point& a,Point& b){
	double dx= a.X-b.X;
	double dy =a.Y-b.Y;
	return sqrt(dx*dy+dy*dy);
}

int main(int argc, char** argv) {
	Point p1(1.0,2.0),p2(3.0,4.0);
	cout<<distances(p1,p2)<<endl;;
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: