c++友元

函数作为友元

#include <iostream>
#include <string>
using namespace std;
class Girl
{
public:
	Girl(string n, int d) : name(n), age(d){}
	friend void disp(Girl& x);		// 将disp函数声明为Girl类的友元函数
private:
	string name;
	int age;
};
void disp(Girl& x)
{
	cout << x.name << " " << x.age << endl;
}
int main()
{
	Girl g1("翠花", 18);
	disp(g1);
	return 0;
}

一个函数同时定义为两个类的友元函数

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;

class Boy;   // 声明Boy类
class Girl
{
public:
	Girl(string n, int d) : name(n), age(d){}
	friend void PriData(Girl& x, Boy& b);		// 将PriData函数声明为Girl类的友元函数
private:
	string name;
	int age;
};

class Boy
{
public:
	Boy(string n, int a) : name(n) , age(a) {}
	friend void PriData(Girl& x, Boy& b);		// 将PriData函数声明为Girl类的友元函数
private:
	string name;
	int age;
};

void PriData(Girl& x, Boy& b)
{
	cout << "Girl info " << endl;
	cout << x.name << " " << x.age << endl;
	cout << "Boy info " << endl;
	cout << b.name << " " << b.age << endl;
}
int main()
{
	Girl g1("翠花", 18);
	Boy b1("张三", 20);
	PriData(g1, b1);
	return 0;
}

成员函数作为函数的友元

一个类的成员函数作为为另一个类的友元函数时,必须先定义这个类

#include <iostream>
#include <string>
using namespace std;
// disp为类Boy的成员函数,又是类Girl的友元函数
class Girl;		// 提前声明Girl类,因为disp函数用到了Girl类,而Girl类到后面才被定义
class Boy
{
public:
	Boy(string n, int d) : name(n), age(d) {}
	void disp(Girl& x);
	
private:
	string name;
	int age;
};

class Girl
{
public:
	Girl(string n, int d) : name(n), age(d) {}
	friend void Boy::disp(Girl& x);		// 将disp函数声明为Girl类的友元函数,注意这里要加上Boy::
private:
	string name;
	int age;
};

// disp需要在类外实现,因为第五行只是声明了Girl类,并没有定义Girl类
void Boy::disp(Girl& x)
{
	cout << "Girl info " << endl;
	cout << x.name << " " << x.age << endl;
	cout << "Boy info " << endl;
	cout << name << " " << age << endl;
}

int main()
{
	Boy b("张三", 18);
	Girl g("翠花", 20);
	b.disp(g);
	return 0;
}

友元类

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
// 将Boy类声明为Girl类的友元,这样Boy类的成员函数都是Girl类的友元
class Girl;
class Boy
{
public:
	Boy(string n, int d) : name(n), age(d) {}
	void disp1(Girl& );
	void disp2(Girl& );
private:
	string name;
	int age;
};
class Girl
{
public:
	Girl(string n, int d) : name(n), age(d) {}
	friend class Boy;		// 将Boy类声明为Girl类的友元
private:
	string name;
	int age;
};

void Boy::disp1(Girl& g)
{
	cout << "name" << endl;
	cout << name << endl << g.name << endl;
}

void Boy::disp2(Girl& g)
{
	cout << "age" << endl;
	cout << age << endl << g.age << endl;
}
int main()
{
	Boy b("张三", 18);
	Girl g("翠花", 20);
	b.disp1(g);
	b.disp2(g);
	return 0;
}

当然,也可以这样做,没有成员函数作为函数的友元那么严格

class Girl
{
public:
	Girl(string n, int d) : name(n), age(d) {}
	friend class Boy;		// 将Boy类声明为Girl类的友元
private:
	string name;
	int age;
};
class Boy
{
public:
	Boy(string n, int d) : name(n), age(d) {}
	void disp1(Girl& g)
	{
		cout << "name" << endl;
		cout << name << endl << g.name << endl;
	}
	void disp2(Girl& g)
	{
		cout << "age" << endl;
		cout << age << endl << g.age << endl;
	}
private:
	string name;
	int age;
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值