举例说明C++友元类的作用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sun_ashe/article/details/86025572

如下所示例,class A与class B并没有继承关系,但是我们想让A直接访问B的私有变量,则可以将class B作为class A的友元。

#include <iostream>

using namespace std;

class A
{
	class B
	{
		private:
						int a;
						int b;
		friend class A;
	};
	B b;

	public:
	void init_b()
	{
		b.a=1;
		b.b=2;
		cout << b.a << endl;
		cout << b.b << endl;
	}

};

int main()
{
	A a;
	a.init_b();


	return 0;
}

再来举一个更加形象的例子,来源于别人的博客
https://www.cnblogs.com/renzhuang/articles/6592024.html

假设我们要设计一个模拟电视机和遥控器的程序。大家都之道,遥控机类和电视机类是不相包含的,而且,遥控器可以操作电视机,但是电视机无法操作遥控器,这就比较符合友元的特性了。即我们把遥控器类说明成电视机类的友元。下面是这个例子的具体代码:

复制代码
  1 #include <iostream>
  2 using namespace std;
  3 class TV 
  4 {
  5     public:
  6       friend class Tele;
  7       TV():on_off(off),volume(20),channel(3),mode(tv){}
  8     private:    
  9       enum{on,off};
 10       enum{tv,av};
 11       enum{minve,maxve=100};
 12       enum{mincl,maxcl=60};
 13       bool on_off;
 14       int  volume;
 15       int channel;
 16       int mode;
 17 };
 18 class Tele
 19 {
 20     public:
 21        void OnOFF(TV&t){t.on_off=(t.on_off==t.on)?t.off:t.on;}
 22        void SetMode(TV&t){t.mode=(t.mode==t.tv)?t.av:t.tv;}
 23        bool VolumeUp(TV&t);
 24        bool VolumeDown(TV&t);
 25        bool ChannelUp(TV&t);
 26        bool ChannelDown(TV&t);
 27        void show(TV&t)const;    
 28 };
 29 bool Tele::VolumeUp(TV&t)
 30 {
 31     if (t.volume<t.maxve)
 32     {
 33         t.volume++;
 34         return true;
 35     } 
 36     else
 37     {
 38         return false;
 39     }
 40 }
 41 bool Tele::VolumeDown(TV&t)
 42 {
 43     if (t.volume>t.minve)
 44     {
 45         t.volume--;
 46         return true;
 47     } 
 48     else
 49     {
 50         return false;
 51     }
 52 }
 53 bool Tele::ChannelUp(TV&t)
 54 {
 55     if (t.channel<t.maxcl)
 56     {
 57         t.channel++;
 58         return true;
 59     } 
 60     else
 61     {
 62         return false;
 63     }
 64 }
 65 bool Tele::ChannelDown(TV&t)
 66 {
 67     if (t.channel>t.mincl)
 68     {
 69         t.channel--;
 70         return true;
 71     } 
 72     else
 73     {
 74         return false;
 75     }
 76 }
 77 void Tele::show(TV&t)const
 78 {
 79     if (t.on_off==t.on)
 80     {
 81         cout<<"电视现在"<<(t.on_off==t.on?"开启":"关闭")<<endl;
 82         cout<<"音量大小为:"<<t.volume<<endl;
 83         cout<<"信号接收模式为:"<<(t.mode==t.av?"AV":"TV")<<endl;
 84         cout<<"频道为:"<<t.channel<<endl;
 85  
 86     } 
 87     else
 88     {
 89         cout<<"电视现在"<<(t.on_off==t.on?"开启":"关闭")<<endl;
 90     }
 91      
 92 }
 93 int main()
 94 {
 95     Tele t1;
 96     TV t2;
 97     t1.show(t2);
 98     t1.OnOFF(t2);
 99     t1.show(t2);
100     cout<<"调大声音"<<endl;
101     t1.VolumeUp(t2);
102     cout<<"频道+1"<<endl;
103     t1.ChannelUp(t2);
104     cout<<"转换模式"<<endl;
105     t1.SetMode(t2);
106     t1.show(t2);
107     return 0;
108 }
复制代码
我们在程序的第6行定义了一个TV电视机类的友元类Tele。那么程序中就可以来调用TV类中的私有成员。

猜你喜欢

转载自blog.csdn.net/sun_ashe/article/details/86025572
今日推荐