好文档 - 专业文书写作范文服务资料分享网站

C primer plus(第6版)中文版编程练习答案第15章

天下 分享 时间: 加入收藏 我要投稿 点赞

1、 //tv.h

#ifndef TV_H_ #define TV_H_

#include using namespace std;

class Tv { friend class Remote; public: enum { Off, On }; enum { MinVal, MaxVal = 20 }; enum { Antenna, Cable }; enum { TV, DVD }; enum { USUAL, EXCHANGE }; Tv(int s = Off, int mc = 125) :state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV){} ~Tv(){} void onoff(){ state = (state == On) ? Off : On; } bool ison()const{ return state == On; } bool volup(); bool voldown(); void chanup(); void chandown(); void set_mode(){ mode = (mode == Antenna) ? Cable : Antenna; } void set_input(){ input = (input == TV) ? DVD : TV; } void settings()const; void set_rmode(Remote &r); private: int state; int volume; int maxchannel; int channel; int mode; int input; };

class Remote {

private:

friend class Tv; enum { USUAL, EXCHANGE }; int mode; int fmode; public: Remote(int m = Tv::TV, int f = USUAL) :mode(m), fmode(f){} bool volup(Tv &t){ return t.volup(); } bool voldown(Tv &t){ return t.voldown(); } void onoff(Tv &t){ t.onoff(); } void chanup(Tv &t){ t.chanup(); } void chandown(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 mode_show()const{ cout << \};

inline void Tv::set_rmode(Remote &r) { if (ison()) { r.fmode = Remote::EXCHANGE; r.mode_show(); } }

#endif //tvfm.h

#ifndef TVFM_H_ #define TVFM_H_ #include using namespace std;

class Tv;

class Remote {

public: enum State{ Off, On }; enum { MinVal, MaxVal = 20 }; enum { Antenna, Cable }; enum { TV, DVD }; private: int mode;

public: Remote(int m = TV) :mode(m){} bool volup(Tv &t); bool voldown(Tv &t); void onoff(Tv &t); void chanup(Tv &t); void chandown(Tv &t); void set_chan(Tv &t, int c); void set_mode(Tv &t); void set_input(Tv &t); };

class Tv {

public: friend void Remote::set_chan(Tv &t, int c); enum State{ 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){} ~Tv(){} void onoff(){ state = (state == On) ? Off : On; } bool ison()const{ return state == On; } bool volup(); bool voldown(); void chanup(); void chandown(); void set_mode(){ mode = (mode == Antenna) ? Cable : Antenna; } void set_input(){ input = (input == TV) ? DVD : TV; } void settings()const;

private: int state; int volume; int maxchannel; int channel; int mode; int input; };

inline bool Remote::volup(Tv &t){ return t.volup(); }

inline bool Remote::voldown(Tv &t){ return t.voldown(); } inline void Remote::onoff(Tv &t){ t.onoff(); } inline void Remote::chanup(Tv &t){ t.chanup(); }

inline void Remote::chandown(Tv &t){ t.chandown(); } inline void Remote::set_chan(Tv &t, int c){ t.channel = c; } inline void Remote::set_mode(Tv &t){ t.set_mode(); } inline void Remote::set_input(Tv &t){ t.set_input(); }

#endif //tv.cpp

#include \//#include \bool Tv::volup() { if (volume < MaxVal) { volume++; return true; } else return false; }

bool Tv::voldown() { 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 << \ if (state == On) { cout << \ cout << \ cout << \ cout << \ } }

//use_tv.cpp #include \int main() { Tv s42; Remote grey; grey.mode_show(); cout << \ s42.settings(); s42.onoff(); s42.chanup(); cout << \ s42.settings(); s42.set_rmode(grey); grey.set_chan(s42, 10); grey.volup(s42); grey.volup(s42); cout << \ s42.settings(); Tv s58(Tv::On); s58.set_mode(); grey.set_chan(s58, 28); cout << \ s58.settings(); s58.set_rmode(grey); system(\ return 0; }

C primer plus(第6版)中文版编程练习答案第15章

1、//tv.h#ifndefTV_H_#defineTV_H_#includeusingnamespacestd;classTv{friendclassRemote;public:enum{Off,On};enum{MinVal,MaxVal=20};e
推荐度:
点击下载文档文档为doc格式
5g4r75djo34n25q6ny0j2r4yi9c8on003tk
领取福利

微信扫码领取福利

微信扫码分享