//tvfm1.h
#ifndef TVFM
#define TVFM
#include<iostream>
class Tv
{
public:
friend class Remote;
enum{ Off,On};
enum{ MinVal,MaxVal =20};
enum{ Antenna,Cable};
enum{ TV,DVD};
Tv(int s = Off,int mc =125):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 voldowm();
void chanup();
void chandown();
void set_mode(){mode=(mode==Antenna)? Cable : Antenna;}
void set_input(){input =(input ==TV)? DVD:TV;}
void settings()const;
void setR(Remote &r);
private:
int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
};
class Remote
{
friend class Tv;
private:
int mode;
int RemoteMode;
public:
enum{RemoteOn,RemoteOff};
Remote(int m =Tv::TV,int rm=RemoteOn):mode(m){};
bool volup(Tv &t){return t.volup();}
bool voldown(Tv& t){ return t.voldowm();}
void onoff(Tv& t){ t.onoff();}
void chanup(Tv& t){t.chanup();}
void chandowm(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();}
void show()
{
std::cout<<"RemoteMode:"<<(RemoteMode==RemoteOn?"RemoteOn":"RemoteOff")<<std::endl;
}
void change()
{
RemoteMode=RemoteOn? RemoteOff:RemoteOn;
}
};
#endif
//tvfm1.cpp
#include"tvfm1.h"
#include<iostream>
using namespace std;
bool Tv::volup()
{
if(volume<MaxVal)
{
volume++;
return true;
}
else
return true;
}
bool Tv::voldowm()
{
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::settings()const
{
cout<<"Tv is "<<(state==Off?"off":"on")<<endl;
if(state ==On)
{
cout<<"Volum setting="<<volume<<endl;
cout<<"Channel setting="<<channel<<endl;
cout<<"Mode="<<(mode==Antenna?"Antenna":"cable")<<endl;
cout<<"Input="<<(input==TV?"Tv":"DVD")<<endl;
}
}
void Tv::setR(Remote &r)
{
if(state==On)
r.change();
else
cout<<"Tv is not on!"<<endl;
}
//main.cpp
#include"tvfm1.h"
#include<iostream>
void main()
{
Remote grey;
Tv t11(Tv::On);
t11.settings();
grey.set_chan(t11,28);
std::cout<<"t11 settings"<<std::endl;
t11.settings();
grey.show();
t11.setR(grey);
grey.show();
system("pause");
}
15.1
最新推荐文章于 2023-09-10 22:24:13 发布