C++学习笔记
#include<iostream>
#include<limits>
using namespace std;
class TV
{
public:
TV():on_off(off),volume(20),channel(1),mode(tv){}
friend class Tele;
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;}
bool VolumeDown(TV&t);
bool VolumeUp(TV&t);
bool Tele::ChanneUp(TV&t);
bool Tele::ChanneDown(TV&t);
void Show(TV&t)const;
private:
};
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::ChanneUp(TV&t)
{
if(t.channel<t.maxcl)
{
t.channel++;
return true;
}
else
return false;
}
bool Tele::ChanneDown(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<<"电视开启\n";
cout<<"音量大小为:"<<t.volume<<endl;
cout<<"信号的接收模式为:"<<(t.mode==t.tv?"TV":"AV")<<endl;
cout<<"当前频道为"<<t.channel<<endl;
}
else
cout<<"电视现在关闭"<<endl;
}
int main()
{
Tele t1;
TV t2;
t1.Show(t2);
t1.OnOff(t2);
t1.Show(t2);
cout<<"调大声音\n";
t1.VolumeUp(t2);
cout<<"调大频道\n";
t1.ChanneUp(t2);
cout<<"转换模式\n";
t1.SetMode(t2);
t1.Show(t2);
return 0;
}
浙公网安备 33010602011771号