码迷,mamicode.com
首页 > 编程语言 > 详细

C++——友元、异常和其他

时间:2015-07-25 12:15:11      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

一、友元

  类并非只能拥有友元函数,也可以将类作为友元。在这种情况下,友元类的所有方法都可以访问原始类的私有成员和保护成员。另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元。哪些函数、成员函数或类为友元是由类定义的,而不能从外部强加友情。因此,尽管友元被授予从外部访问类的私有部分的权限,但它们并不与面向对象的编程思想相违背;相反,它们提高了公有接口的灵活性。

  1、友元类

  一般来说,如果希望一个类型的对象发生变化时,另一个其他类型的对象也相应地跟着产生变化,可以使用友元类来实现。

  要想使一个类成为另一个类的友元类,只需要在另一个类的公有、私有或保护部分声明如下语句即可:

    friend  class  Classname;

  友元类声明的位置无关紧要。

  下面定义Tv类和Remote类,其中Remote是Tv的友元类,通过Remote对象可以控制TV对象: 

头文件:

 1 #include <stdio.h>
 2 #include <iostream>
 3 
 4 class Tv{
 5 public:
 6     friend class Remote;
 7     enum {Off,On};
 8     enum {MinVal,MaxVal = 20};
 9     enum {Antenna,Cable};
10     enum {TV, DVD};
11     
12     Tv(int s = Off, int mc = 125):state(s),volume(5),maxchannel(mc),channel(2),mode(Cable),input(TV){};
13     void onoff(){state ^= 1;}
14     bool ison(){return state == On;}
15     bool volup();
16     bool voldown();
17     void chanup();
18     void chandown();
19     void set_mode(){mode ^= 1;}
20     void set_input(){input ^= 1;}
21     void settings()const;
22 private:
23     int state;
24     int volume;
25     int maxchannel;
26     int channel;
27     int mode;
28     int input;
29 };
30 class Remote{
31 private:
32     int mode;
33 public:
34     Remote(int m = Tv::TV):mode(m){};
35     bool volup(Tv & t){return t.volup();}
36     bool voldown(Tv & t){return t.voldown();}
37     void onoff(Tv & t){t.onoff();}
38     void chanup(Tv & t){t.chanup();}
39     void chandown(Tv & t){t.chandown();}
40     void set_chan(Tv & t, int c){t.channel = c;};
41     void set_mode(Tv & t){t.set_mode();}
42     void set_input(Tv & t){t.set_input();}
43 };

 

实现文件:

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

 

main.cpp

 1 #include <iostream>
 2 #include "TV.h"
 3 
 4 using namespace std;
 5 
 6 int main(int argc, const char * argv[]) {
 7     Tv s42;
 8     cout << "Initial setting for 42\" TV:\n";
 9     s42.settings();
10     s42.onoff();
11     s42.chanup();
12     cout << "\nAdjusted settings for 42\" TV:\n";
13     s42.chanup();
14     cout << "\nAdjusted setting for 42\" TV:\n";
15     s42.settings();
16     
17     Remote grey;
18     grey.set_chan(s42, 10);
19     grey.volup(s42);
20     grey.volup(s42);
21     cout << "\n42\"settings after using remote :\n";
22     s42.settings();
23     
24     Tv s58(Tv::On);
25     s58.set_mode();
26     grey.set_chan(s58, 28);
27     cout << "\n58\" settings:\n";
28     s58.settings();
29     return 0;
30 }
31 
32 
33 输出结果:
34 Initial setting for 42" TV:
35 TV is Off
36 
37 Adjusted settings for 42" TV:
38 
39 Adjusted setting for 42" TV:
40 TV is On
41 Volume setting = 5
42 Channel setting = 4
43 Mode = cable
44 Input = TV
45 
46 42"settings after using remote :
47 TV is On
48 Volume setting = 7
49 Channel setting = 10
50 Mode = cable
51 Input = TV
52 
53 58" settings:
54 TV is On
55 Volume setting = 5
56 Channel setting = 28
57 Mode = antenna
58 Input = TV

 2、友元成员函数

  

 

C++——友元、异常和其他

标签:

原文地址:http://www.cnblogs.com/mupiaomiao/p/4674152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!