C++ 友元类

定义友元类

tv.h

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

友元类的使用

tv.cpp

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

main.cpp

  1. #include <iostream>
  2. #include "module12_friend_class/tv.h"
  3. using namespace std;
  4. // 友元类的使用
  5. void useFriendClass(){
  6. Tv s27;
  7. cout << "Initial setting for 27\" TV:\n";
  8. s27.setting();
  9. s27.onoff();
  10. s27.chanup();
  11. cout << "\nAdjusted setting for 27\" TV:\n";
  12. s27.setting();
  13. Remote grey;
  14. grey.set_chan(s27, 10);
  15. grey.volup(s27);
  16. grey.volup(s27);
  17. cout << "\n27\" settings after using remote:\n";
  18. s27.setting();
  19. Tv s32(Tv::On);
  20. s32.set_mode();
  21. grey.set_chan(s32, 28);
  22. cout << "\n32\" setting:\n";
  23. s32.setting();
  24. }
  25. int main() {
  26. useFriendClass();
  27. return 0;
  28. }

友元成员函数

tvfm.h

  1. #ifndef PRO1_TVFM_H
  2. #define PRO1_TVFM_H
  3. class Tv;
  4. class Remote {
  5. public:
  6. enum State{Off, On};
  7. enum {MinVal, MacVal = 20};
  8. enum {Antenna, Cable};
  9. enum {TV, VCR};
  10. private:
  11. int mode;
  12. public:
  13. Remote(int m = TV):mode(m){}
  14. bool volup(Tv & t);
  15. bool voldown(Tv & t);
  16. void onoff(Tv & t);
  17. void chanup(Tv & t);
  18. void chandown(Tv & t);
  19. void set_chan(Tv & t, int c);
  20. void set_mode(Tv & t);
  21. void set_input(Tv & t);
  22. };
  23. class Tv{
  24. private:
  25. int state;
  26. int volume;
  27. int maxchannel;
  28. int channel;
  29. int mode;
  30. int input;
  31. public:
  32. friend void Remote::set_chan(Tv &t, int c);
  33. enum {Off, On};
  34. enum {MinVal, MaxVal = 20};
  35. enum {Antenna, Cable};
  36. enum {TV, VCR};
  37. Tv(int s = Off, int mc = 100): state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV){}
  38. void onoff(){state = (state == On) ? Off : On;}
  39. bool ison()const { return state == On; }
  40. bool volup();
  41. bool voldown();
  42. void chanup();
  43. void chandown();
  44. void set_mode(){ mode = (mode == Antenna) ? Cable : Antenna; }
  45. void set_input(){ input = (input == TV) ? VCR : TV; }
  46. void setting()const;
  47. };
  48. // Remote methods as inline functions
  49. inline bool Remote::volup(Tv &t) { return t.volup(); }
  50. inline bool Remote::voldown(Tv &t) { return t.voldown(); }
  51. inline void Remote::onoff(Tv &t) { t.onoff(); }
  52. inline void Remote::chanup(Tv &t) { t.chanup(); }
  53. inline void Remote::chandown(Tv &t) { t.chandown(); }
  54. inline void Remote::set_mode(Tv &t) { t.set_mode(); }
  55. inline void Remote::set_input(Tv &t) { t.set_input(); }
  56. inline void Remote::set_chan(Tv &t, int c) { t.channel = c; }
  57. #endif //PRO1_TVFM_H

tvfm.cpp

  1. #include "tvfm.h"