C++_友元1-友元类是什么

友元函数:不是类的成员函数,但是能够访问类的私有数据成员。

之前有个矛盾就是规定非成员函数不能直接访问类的私有数据,但是这会儿却可以,但那只是针对常规非成员函数而言,特殊的非成员函数就可以访问类的私有数据——友元函数

友元函数就是这么特殊,虽然在类中声明,却不是类的成员函数,这是因为加了前缀friend。

友元不仅包含函数,类也可以当友元,也就是友元类

友元类的所有方法都可以访问原始类的私有成员和保护成员。

也可以做更严格的限制,只将限定的成员函数指定为另一类的友元。

尽管友元被授予从外部访问类的私有部分的权限,但它们并不与面向对象的编程思想相违背。

 

接下来就是讨论友元类

假定要编写一个模拟电视机和遥控器的简单程序。

决定定义一个Tv类和一个Remote类,来分别表示电视机和遥控器。

很明显,这两个类之间应当存在某种关系,遥控器并非电视机,反之亦然,

但是遥控器却可以改变电视机的状态。这样的关系可以将Remote类作为Tv类的一个友元来实现。

友元类声明位置无关紧要,可以声明在公有、私有、保护部分;

 

定义一个电视机类,用一些状态成员来表示电视机:
开/关;
频道设置;
音量设置;
有线电视or天线调节模式;
TV调谐或A/V输入

tv.h文件,声明tv和remote的类

 1 #ifndef TV_H_
 2 #define TV_H_
 3 
 4 class Tv
 5 {
 6 public:
 7     friend class Remote;
 8     enum{Off, On};
 9     enum{MinVal, MaxVal=20};
10     enum{Antenna, Cable};
11     enum{TV,DVD};
12     
13     Tv():state(s),volume(5),maxchannel(mc),channel(2),mode(Cable),input(TV){}
14     void onoff() {state=(state == On)?Off:On;}
15     bool ison() const {return state==On;}
16     bool volup();
17     bool voldown();
18     void chanup();
19     void chandown();
20     void set_mode() {mode=(mode == Antenna)?Cable:Antenna;}
21     void set_input() {input=(input == TV)?Cable:Antenna;}
22     void settings() const;  //display all settings;
23 private:
24     int state;    //on or off
25     int volume;
26     int maxchannel;
27     int channel;  //current channel setting
28     int mode;    //broadcast or cable
29     int inputs;  //TV or DVD
30 };
31 
32 class Remote  //Remote方法以TV的对象引用作为参数,这表明遥控器必须针对特定的电视机
33 {
34 public:
35     int mode;   //controls TV or DVD;
36 private:
37     Remote(int m=Tv::Tv): mode(m) {}
38     bool volup(Tv & t) {return t.volup();} 
39     bool voldown(Tv & t) {return t.voldown();}
40     void onoff(Tv & t) {t.onoff();}
41     void chanup(Tv & t) {t.chanup();}
42     void chandown(Tv & t) {t.chandown();}
43     void set_chan(Tv & t, int c) {t.channel = c;}
44     void set_mode(Tv & t) {t.set_mode();}
45     void set_input(Tv & t) {t.set_input();}
46 };
47 #endif

tv.cpp

 1 #include <iostream>
 2 #include "tv.h"
 3 
 4 bool Tv::volup()
 5 {
 6     if(volume<MaxVal)
 7     {
 8         volume++;
 9         return true;
10     }
11     else
12         return false;
13 }
14 
15 bool Tv::voldown()
16 {
17     if(volume>MinVal)
18     {
19         volume--;
20         return true;
21     }
22     else
23         return false;
24 }
25 
26 void Tv:chanup()
27 {
28     if(channel<maxchannel)
29         channel++;
30     else
31         channel=1;
32 }
33 
34 void Tv::chandown()
35 {
36     if(channel>1)
37         channel--;
38     else
39         channel = maxchannel;
40 }
41 
42 void Tv::Settings() const
43 {
44     using std::cout;
45     using std::endl;
46     cout<<"TV is "<<(state==Off?"Off":"On")<<endl;
47     if(state == On)
48     {
49         cout<<"Volume setting = "<<volume<<endl;
50         cout<<"Channel setting = "<<channel<<endl;
51         cout<<"Mode = "<<(mode == Antenna? "antenna":"cable")<<endl;
52         cout<<"Input = "<<(mode == TV? "TV":"DVD")<<endl;
53     }
54 }

 

use_tv.cpp

 1 #include<iostream>
 2 #include "tv.h"
 3 
 4 int main()
 5 {
 6     using std::cout;
 7     Tv s42;
 8     cout<<"Initial settings for 42" TV:
";
 9     s42.settings();
10     s42.onoff();
11     s42.chanup();
12     cout<<"
Adjusted settings for 42" TV:
";
13     s42.chanup();
14     cout<<"
Adjusted settings for 42" TV:
";
15     s42.settings();
16     
17     Remote grey;
18     
19     grey.set_chan(s42, 10);
20     grey.volup(s42);
21     grey.volup(s42);
22     cout<<"
42" settings after using remote:
";
23     s42.settings();
24     
25     Tv s58(Tv::On);
26     s58.set_mode();
27     grey.set_chan(s58,28);
28     cout<<"
58" settings:
";
29     s58.settings();
30     return 0;
31 }
原文地址:https://www.cnblogs.com/grooovvve/p/10421125.html