[C++] c++ 友元类 →→→→→进入此内容的聊天室

来自 , 2020-01-02, 写在 C++, 查看 127 次.
URL http://www.code666.cn/view/09b15d48
  1. #include <iostream>
  2. using namespace std;
  3. class TV
  4. {
  5. public:
  6.         friend class Tele;
  7.         TV() :on_off ( off ),volume ( 20 ),channel ( 3 ),mode ( tv ) {}
  8. private:
  9.         enum {on,off};
  10.         enum {tv,av};
  11.         enum {minve,maxve=100};
  12.         enum {mincl,maxcl=60};
  13.         bool on_off;
  14.         int  volume;
  15.         int channel;
  16.         int mode;
  17. };
  18. class Tele
  19. {
  20. public:
  21.         void OnOFF ( TV&t ) {t.on_off= ( t.on_off==t.on ) ?t.off:t.on;}
  22.         void SetMode ( TV&t ) {t.mode= ( t.mode==t.tv ) ?t.av:t.tv;}
  23.         bool VolumeUp ( TV&t );
  24.         bool VolumeDown ( TV&t );
  25.         bool ChannelUp ( TV&t );
  26.         bool ChannelDown ( TV&t );
  27.         void show ( TV&t ) const;
  28. };
  29. bool Tele::VolumeUp ( TV&t )
  30. {
  31.         if ( t.volume<t.maxve )
  32.         {
  33.                 t.volume++;
  34.                 return true;
  35.         }
  36.         else
  37.         {
  38.                 return false;
  39.         }
  40. }
  41. bool Tele::VolumeDown ( TV&t )
  42. {
  43.         if ( t.volume>t.minve )
  44.         {
  45.                 t.volume--;
  46.                 return true;
  47.         }
  48.         else
  49.         {
  50.                 return false;
  51.         }
  52. }
  53. bool Tele::ChannelUp ( TV&t )
  54. {
  55.         if ( t.channel<t.maxcl )
  56.         {
  57.                 t.channel++;
  58.                 return true;
  59.         }
  60.         else
  61.         {
  62.                 return false;
  63.         }
  64. }
  65. bool Tele::ChannelDown ( TV&t )
  66. {
  67.         if ( t.channel>t.mincl )
  68.         {
  69.                 t.channel--;
  70.                 return true;
  71.         }
  72.         else
  73.         {
  74.                 return false;
  75.         }
  76. }
  77. void Tele::show ( TV&t ) const
  78. {
  79.         if ( t.on_off==t.on )
  80.         {
  81.                 cout<<"电视现在"<< ( t.on_off==t.on?"开启":"关闭" ) <<endl;
  82.                 cout<<"音量大小为:"<<t.volume<<endl;
  83.                 cout<<"信号接收模式为:"<< ( t.mode==t.av?"AV":"TV" ) <<endl;
  84.                 cout<<"频道为:"<<t.channel<<endl;
  85.  
  86.         }
  87.         else
  88.         {
  89.                 cout<<"电视现在"<< ( t.on_off==t.on?"开启":"关闭" ) <<endl;
  90.         }
  91.  
  92. }
  93. int main()
  94. {
  95.         Tele t1;
  96.         TV t2;
  97.         t1.show ( t2 );
  98.         t1.OnOFF ( t2 );
  99.         t1.show ( t2 );
  100.         cout<<"调大声音"<<endl;
  101.         t1.VolumeUp ( t2 );
  102.         cout<<"频道+1"<<endl;
  103.         t1.ChannelUp ( t2 );
  104.         cout<<"转换模式"<<endl;
  105.         t1.SetMode ( t2 );
  106.         t1.show ( t2 );
  107.         system ( "pause" );
  108. }
  109.  

回复 "c++ 友元类"

这儿你可以回复上面这条便签

captcha