//电视机将遥控器声明为友元类,遥控器可以控制电视机反过来则不行。就好像你愿意把秘密告诉你的朋友,这不不意味着你的朋友愿意把他的秘密告诉你
#include <iostream>
using namespace std;
class TV //电视机类
{
public:
friend class Tele; //声明遥控器类为电视机类的友元类
TV():on_off(off),volume(20),channel(1),mode(tv){}
private:
enum{on,off};
enum{tv,av};
enum{mincl,maxcl=60};
enum{minve,maxve=100};
bool on_off;
int channel;
int volume;
int mode;
};
class Tele //遥控器类
{
public:
void OnOff(TV&t){t.on_off=(t.on_off==t.on)?t.off:t.on;} //遥控器的开关
void SetMode(TV&t){t.mode=(t.mode==t.tv)?t.av:t.tv;} //av额tv模式切换
bool VolumeDown(TV&t); //减小音量
bool VolumeUp(TV&t); //增大音量
bool ChannelDown(TV&t); //减小频道
bool ChannelUp(TV&t); //增大频道
void Show(TV&t)const;
};
bool Tele::VolumeDown(TV&t)
{
if(t.volume>t.minve)
{
t.volume--;
return true;
}
else
return false;
}
bool Tele::VolumeUp(TV&t)
{
if(t.volume<t.maxve)
{
t.volume++;
return true;
}
else
return false;
}
bool Tele::ChannelUp(TV&t)
{
if(t.channel>t.maxcl)
{
t.channel++;
return true;
}
else
return false;
}
bool Tele::ChannelDown(TV&t)
{
if(t.channel>t.mincl)
{
t.channel--;
return true;
}
else
return false;
}
void Tele::Show(TV&t)const
{
if (t.on_off==t.on)
{
cout<<"电视现在开启"<<endl;
cout<<"音量大小为:"<<t.volume<<endl;
cout<<"信号接收模式"<<(t.mode==t.tv?"TV":"AV");
cout<<"当前频道为:"<<t.channel<<endl;
}
else
cout<<"电视现在关闭"<<endl;
}
int main()
{
Tele t1;
TV t2;
t1.Show(t2);
t1.OnOff(t2);
t1.Show(t2);
return 0;
}