友元的使用.cpp

来自「c++实例~ 初学基础」· C++ 代码 · 共 46 行

CPP
46
字号
#include <iostream.h>
#include <math.h>

class Point
{
public:
	Point(double xx, double yy)
	{ 
		x=xx; 
		y=yy; 
	}

	void Getxy();
	friend double Distance(Point &a, Point &b);

private:
	double x;
	double y;
};


void Point::Getxy()
{
	cout << "(" << x << "," << y << ")" << endl;
}


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


void main()
{
	Point p1(3.0, 4.0);
	Point p2(6.0, 8.0);
	p1.Getxy();
	p2.Getxy();
	double d = Distance(p1, p2);
	cout << "Distance is" << d << endl;
}

⌨️ 快捷键说明

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