优秀的编程知识分享平台

网站首页 > 技术文章 正文

C++|类的友元函数和友元:对数据保护和隐藏的一种折衷选择

nanyue 2024-08-10 18:35:55 技术文章 11 ℃

友元分为友元函数和友元类,是对数据封装和保护的一种突破,也可以提高程序运行的效率。

友元函数是通过引用类对象来访问类的成员。

当声明一个类为另一个类的友元类时,友元类的成员函数都是另一个类的友元函数。

直接看代码和注释:

附代码:

#include <iostream.h>
#include <math.h>
const double PI = 3.14159;
class Point
{
	double x,y;									// 默认为私有成员
public:
	Point(double xx, double yy)
	{
		x = xx;
		y = yy;
	}
	void display()
	{
		cout<<"("<<x<<","<<y<<")"<<endl;
	}
	friend double distance(Point &a, Point &b);	// 声明友元函数
	friend class Circle;						// 声明友元类
};
												// 实现友元函数
double distance (Point &a,Point &b)				// 引用类对象作为形参
{
	double dx = a.x-b.x;						// 访问对象的私有成员
	double dy = a.y-b.y;
	return sqrt(dx*dx+dy*dy);
}
class Circle									// 友元类的声明和实现
{
	double cir;
public:
	void circum(Point a,Point b)				// 用类对象作为形参
	{
		double dx = a.x-b.x;					// 访问对象的私有成员
		double dy = a.y-b.y;
		cir = PI * sqrt(dx*dx+dy*dy);	
		cout<<"the circumstance of circle is "<<cir<<endl;
	}
};
void main()
{
	Point p1(2.0,2.0), p2(5.0,6.0);
	cout<<"p1:";
	p1.display();
	cout<<"p2:";
	p2.display();
	double d = distance(p1,p2);					// 调用友元函数
	cout<<"p1和p2的距离="<<d<<endl;
	Circle c;
	c.circum(p1,p2);
	cin.get();
}

-End-

Tags:

最近发表
标签列表