C++ 友元类
定义友元类
#ifndef PRO1_TV_H#define PRO1_TV_Hclass Tv {private: int state; int volume; int maxchannel; int channel; int mode; int input;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 setting()const;};class Remote{private: int mode;public: Remote(int m = Tv::TV):mode(m){} bool volup(Tv & t){ return t.volup(); } bool voldown(Tv & t){ return t.voldown(); } void onoff(Tv & t) { t.onoff(); } void chanup(Tv & t){ t.chanup(); } void chandown(Tv & t){ t.chandown(); } void set_chan(Tv & t, int c){ t.channel = c; } void set_mode(Tv & t){ t.set_mode(); } void set_input(Tv & t){ t.set_input(); }};#endif //PRO1_TV_H
友元类的使用
#include "tv.h"#include <iostream>using namespace std;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::setting() const { cout << "TV is " << (state == Off? "Off":"On") << endl; if (state == On) { cout << "Volume setting = " << volume << endl; cout << "Channel setting = " << channel << endl; cout << "Mode = " << (mode == Antenna ? "antenna" : "cable") << endl; cout << "Input = " << (input == TV ? "TV" : "VCR") << endl; }}
#include <iostream>#include "module12_friend_class/tv.h"using namespace std;// 友元类的使用void useFriendClass(){ Tv s27; cout << "Initial setting for 27\" TV:\n"; s27.setting(); s27.onoff(); s27.chanup(); cout << "\nAdjusted setting for 27\" TV:\n"; s27.setting(); Remote grey; grey.set_chan(s27, 10); grey.volup(s27); grey.volup(s27); cout << "\n27\" settings after using remote:\n"; s27.setting(); Tv s32(Tv::On); s32.set_mode(); grey.set_chan(s32, 28); cout << "\n32\" setting:\n"; s32.setting();}int main() { useFriendClass(); return 0;}
友元成员函数
#ifndef PRO1_TVFM_H#define PRO1_TVFM_Hclass Tv;class Remote {public: enum State{Off, On}; enum {MinVal, MacVal = 20}; enum {Antenna, Cable}; enum {TV, VCR};private: int mode;public: Remote(int m = TV):mode(m){} bool volup(Tv & t); bool voldown(Tv & t); void onoff(Tv & t); void chanup(Tv & t); void chandown(Tv & t); void set_chan(Tv & t, int c); void set_mode(Tv & t); void set_input(Tv & t);};class Tv{private: int state; int volume; int maxchannel; int channel; int mode; int input;public: friend void Remote::set_chan(Tv &t, int c); 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 setting()const;};// Remote methods as inline functionsinline bool Remote::volup(Tv &t) { return t.volup(); }inline bool Remote::voldown(Tv &t) { return t.voldown(); }inline void Remote::onoff(Tv &t) { t.onoff(); }inline void Remote::chanup(Tv &t) { t.chanup(); }inline void Remote::chandown(Tv &t) { t.chandown(); }inline void Remote::set_mode(Tv &t) { t.set_mode(); }inline void Remote::set_input(Tv &t) { t.set_input(); }inline void Remote::set_chan(Tv &t, int c) { t.channel = c; }#endif //PRO1_TVFM_H
#include "tvfm.h"