【C++ Primer】第十五章 友元、异常和其他 --之一--->友元和嵌套类

一,友元

1)可以将类作为友元,友元类的所有方法都可以访问原始类的私有成员和保护成员。

2)下面例子介绍了 电视类和遥控器类,其中遥控器类为电视类的友元类

3)注意:友元关系不具对称性。即 A 是 B 的友元,但 B 不一定是 A 的友元。 友元关系不具传递性。即 B 是 A 的友元,C 是 B 的友元,但是 C 不一定是 A 的友元。

#include <iostream> using namespace std; class TV { private: int state;// on or off int volume; //assumed to be digitized int maxchannel;// maximum number of channels int channel;//current channel settings int mode;//broadcast or cable int input;//tv or vcr public: friend class Remote;//遥控器类 enum{off,on}; //枚举 enum{minval,maxval=20}; enum{antenna,cable}; enum{tv,vcr}; TV(int s=off,int mc=100):state(s),volume(5),maxchannel(mc),channel(2),mode(cable),input(tv){} //构造函数 void onoff(){ state=(state == on)?off:on; } bool ison()const{ return state == on; } bool volup(); bool voldown(); void chanup(); void chandown(); void set_mode(){ mode = (mode == antenna)?cable:antenna; } void set_input(){ input=(input == tv)?vcr:tv; } void settings()const; } ; class Remote//遥控器友元类 { private: int mode; public: Remote(int m=TV::tv):mode(m){} //遥控的是电视而不是 vcr bool volup(TV &t){ //遥控器的操作都是来源于电视对自身的操作 ,所以友元遥控器的方法都是调用电视的成员函数 return t.volup(); } bool voldown(TV &t){ return t.voldown(); } void onoff(TV &t){ return t.onoff(); } void chanup(TV &t){ return t.chanup(); } void chandown(TV &t){ return t.chandown(); } void set_chan(TV &t,int c){ //唯一需要作为友元的方法,因为它用来访问TV类的private成员 t.channel=c; } void set_mode(TV &t){ t.set_mode(); } void et_input(TV &t){ t.set_input(); } }; /*下面是电视的成员方法实现*/ bool TV::volup() { if(volume<maxval) { volume++; return true; } else return false; } bool TV::voldown() { if(volume>minval) { volume--; return true; } else return false; } void TV::chanup() { if(channel<maxchannel) channel++; else channel=1; } void TV::chandown() { if(channel>1) channel--; else channel=maxchannel; } void TV::settings()const { cout<<"tv is "<<(state == off?"on":"off")<<endl; if(state == on) { cout<<"volume setting ="<<volume<<endl; cout<<"channel setting ="<<channel<<endl; cout<<"Mode ="<<(mode == antenna?"cable":"antenna")<<endl; cout<<"Input ="<<(input == tv?"vcr":"tv")<<endl; } } int main() { TV s27; cout<<"initinal settings for 27\" tv:\n"; s27.settings(); s27.onoff(); s27.chanup(); cout<<"adjust settings for 27\"tv:"; s27.settings(); return 0; } 输出为:

二,嵌套类

1)嵌套与包含的区别

包含意味着将类对象作为另一个类的成员

对类进行嵌套,不创建类成员,而是定义了一种类型,该类型仅在包含嵌套类的类中使用。struct (结构)实际上是一种嵌套类

2)嵌套类和访问权限

声明位置包含它的类是否可以使用从包含它的类派生出来的类,是否可以使用外部世界是否可以使用
私有部分
保护部分
共有部分


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值