友元函数、友元类、友元成员函数

022普通函数做类友元函数


#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
using namespace std;

class  Human
{
private:
	int age;
	string name;
public:
	//构造函数
	Human();
	Human(int age, string name);
	//析构函数
	~Human()
	{
		cout << "Human析构函数" << endl;
	}
	//友元函数
	friend  void myFriendFunction01(const Human&temHuman) ;	//声明这个函数是Human的友元函数
};


//友元函数的实现
void myFriendFunction01(const Human&temHuman)
{
	cout << "我是Human类的友元函数" << endl;
	cout << temHuman.age << endl;//友元函数可以访问private成员
	
}


Human::Human()
{
	cout << "Human默认构造函数" << endl;
}

Human::Human(int age, string name)
{
	this->age = age;
	this->name = name;

}



int main(void)
{
	Human h1 = Human(12, "jisuanji");
	myFriendFunction01(h1);

	
	system("pause");
	return 0;

}
/*
*(1)普通函数做类友元函数,
*3种访问权限:public,protected,private.
*只要让函数成为类的友元函数,就可以访问类的所有成员(包括成员函数和成员变量(public ,protected private))都可以
*
*	在类内部声明友元函数,使用 friend 函数声明
*	在类外部定义友元函数
*	1.一般要传递类成员作为函数参数,因为只有类对象才可以访问成员属性和成员函数。
*
*	2019年11月20日16:45:35
*	 Sunrise于东北电力大学第二教学楼1121实验室
*/




023友元类


#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
using namespace std;

class A
{
private:
	int number;
public:
	//构造函数
	A(int number)
	{
		this->number = number;
	}
	friend class  B;	//这里还没有定义类B,但是没报错,如果有的编译器报错,可以在前面加一个类声明 class B;
	
};

class B
{
private:
	int number02=0;
public:
	
	void myFunction01(const A&temA);
};



void B::myFunction01(const A&temA)
{
	cout << "可以访问类A的私有成员" << endl;
	cout << temA.number << endl;
	
}


int main(void)
{
	A a(12);
	B b;
	b.myFunction01(a);


	system("pause");
	return 0;

}
/*
*(1)友元类
*	声明一个类B是类A的友元类(在类A里面声明friend class B;),
*		则类B的成员函数可以访问类A的所有成员函数和成员属性
*
*	1.每个类都负责控制自己的友元函数和友元函数。(自己类的友元函数,友元函数在自己类声明)
*	2.友元关系不能被继承
*	3.友元关系是单向的。类A是类B的友元类,但是类B是类A的友元类
*	4.友元关系没有传递性。A-->B-->C
*	
*
*2019年11月20日16:52:37
* Sunrise于东北电力大学第二教学楼1121实验室

*/




024友元成员函数


#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
class A;

//class C;	//因为不仅仅用到了C的类声明,而且包含了里面的函数。甩下面的形式
class C
{
private:
	int number03;
public:
	void myFunctionC(int x, A&a);

public:
};

using namespace std;


class A
{
	friend class  B;	//这里还没有定义类B,但是没报错,如果有的编译器报错,可以在前面加一个类声明 class B;

	friend void C::myFunctionC(int x, A& a);
private:
	int number;
public:
	//构造函数
	A(int number)
	{
		this->number = number;
	}
	
};

class B
{
private:
	int number02 = 0;
public:

	void myFunction01(const A&temA);
};





void C::myFunctionC(int x, A& a)
{
	a.number = x;
	cout << a.number << endl;
	
}



void B::myFunction01(const A&temA)
{
	cout << "可以访问类A的私有成员" << endl;
	cout << temA.number << endl;

}


int main(void)
{
	A a(12);
	C c;
	c.myFunctionC(1, a);


	system("pause");
	return 0;

}
/*
*(1)友元成员函数--有的时候不需要让类中的所有成员函数都去访问另一个类的成员函数和成员变量。而只需要一个成员函数去访问
*
*总结:
*	(1)friend允许在特定情况下访问类的成员属性和成员函数。
*	(2)破坏了类的封装性,降低了类的可靠性与可维护性。
*	(3)要看具体的情况使用友元函数和友元类。
*
*
*2019年11月20日16:52:37
* Sunrise于东北电力大学第二教学楼1121实验室

*/




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值