友元。。...

我们知道如果要访问类的私有成员变量,调用类的公有成员函数是唯一办法,而类的私有成员函数则无法访问。

友元提供了另一种访问私有成员的方案,友元有三种:

1.友元全局函数

2.友元类

3.友元成员函数

1.友元全局函数

在友元全局函数中可以访问另一个类的所有成员。

#include<iostream>
using namespace std;

class CGirl
{

public:
	string m_name;
	CGirl() { m_name = "zhongge"; m_xw = 87; }
	void showname() { cout << "name is : " << m_name << endl; }
private:
	int m_xw;
	void showxw() { cout << "xiongwei is " << m_xw<<endl; }

};
int main() {
	CGirl g;
	g.showname();
	g.showxw();
	return 0;
}

 

报错了,无法访问私有成员函数。

把main()函数声明为他的友元,main就可以访问他的私有成员了;

#include<iostream>
using namespace std;

class CGirl
{
	friend int main();//这行声明友元放哪都行

public:
	string m_name;
	CGirl() { m_name = "zhongge"; m_xw = 87; }
    void showname() { cout << "name is : " << m_name << endl; }
private:
	int m_xw;
	void showxw() { cout << "xiongwei is " << m_xw<<endl; }

};
int main() {
	CGirl g;
	g.showname();
	g.showxw();
	return 0;
}

2.友元类:

在友元类的所有成员函数中,都可以访问另一个类的所有成员。


class CGirl
{
	friend int main();
    friend class CBoy;
    //声明CBoy它为友元类,CBoy就可以随便访问CGirl的成员了!!!
public:
	string m_name;
	CGirl() { m_name = "zhongge"; m_xw = 87; }
    void showname() const{ cout << "name is : " << m_name << endl; }
private:
	int m_xw;
	void showxw() const{ cout << "xiongwei is " << m_xw<<endl; }

};

 友元类的注意事项:

.友元类不能被继承

.友元关系是单向的,不具备交换性。

3.友元成员函数:

在友元类某成员函数中,可以访问另一个类的所有成员。

#include<iostream>
using namespace std;
class CGirl;


class CGirl
{   
	
public:
	string m_name;
	CGirl() { m_name = "zhongge"; m_xw = 87; }
    void showname() const{ cout << "name is : " << m_name << endl; }
private:
	int m_xw;
	void showxw() const{ cout << "xiongwei is " << m_xw<<endl; }

};
class CBoy {
public:
	void func1(const CGirl& g) {
		cout << "func()1 my girlfriend'xw is" << g.m_xw << endl;
	}
	void func2(const CGirl& g) {
		cout << "func2() my girlfriend'xw is" << g.m_xw << endl;
	}
};
int main() {
	CGirl g;
	CBoy b;
	b.func1(g);
	return 0;
}

现在CBoy与CGirl没有任何关系,我们要是把CBoy类的func1()func2()设置为超女类的友元函数;

分四步:
1.把超女类的声明前置

class CGirl;

2.把男朋友类的定义放在超女类之前;

class CGirl;

class CBoy {
public:
    void func1(const CGirl& g) {
        cout << "func()1 my girlfriend'xw is" << g.m_xw << endl;
    }
    void func2(const CGirl& g) {
        cout << "func2() my girlfriend'xw is" << g.m_xw << endl;
    }
};

3.把男朋友的成员函数的函数体从类的定义中拿出来,放在定义超女类的后面。

class CGirl;

class CBoy {
public:
    void func1(const CGirl& g) ;
    void func2(const CGirl& g) ;
};

class CGirl
{   
    
public:
    string m_name;
    CGirl() { m_name = "zhongge"; m_xw = 87; }
    void showname() const{ cout << "name is : " << m_name << endl; }
private:
    int m_xw;
    void showxw() const{ cout << "xiongwei is " << m_xw<<endl; }

};

void func1(const CGirl& g) {
        cout << "func()1 my girlfriend'xw is" << g.m_xw << endl;
    }
void func2(const CGirl& g) {
        cout << "func2() my girlfriend'xw is" << g.m_xw << endl;
    }

4.把男朋友类的成员函数声明为超女类的成员函数。

class CGirl;

class CBoy {
public:
    void func1(const CGirl& g) ;
    void func2(const CGirl& g) ;
};

class CGirl

{

friend void CBoy::func1(const CGirl&g);  

friend void CBoy::func2(const CGirl&g);  
public:
    string m_name;
    CGirl() { m_name = "zhongge"; m_xw = 87; }
    void showname() const{ cout << "name is : " << m_name << endl; }
private:
    int m_xw;
    void showxw() const{ cout << "xiongwei is " << m_xw<<endl; }

};

void func1(const CGirl& g) {
        cout << "func()1 my girlfriend'xw is" << g.m_xw << endl;
    }
void func2(const CGirl& g) {
        cout << "func2() my girlfriend'xw is" << g.m_xw << endl;
    }

  • 25
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值