C++-练习-109
题目对Tv和Remote类进行如下修改a.让它们互为友元b.在Remote类中添加一个状态变量成员该成员描述遥控器使处于常规状态还是互动模式c.在Remote中添加一个显式模式的方法d.在Tv类中添加一个对Remote中新成员进行切换的方法该方法仅当Tv处于打开状态是才能运行编写一个小程序来测试这些新特性#ifndef QUEUE_H_ #define QUEUE_H_ #include iostream using namespace std; class Tv { public: friend class Remote; enum { Off, On }; enum { MinuVal, 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 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; }; class Remote { private: int mode; public: Remote(int m Tv::TV) : mode(m) {} 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(); } }; #e源代码test.h#ifndef QUEUE_H_ #define QUEUE_H_ #include iostream using namespace std; class Remote; 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 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_Remode(Remote r); private: int state; //是否开机 int volume; //音量 int maxchannel; //最大频道 int channel; //当前频道 int mode; //广播或有线 int input; //TV或DVD }; class Remote { public: enum { Normal, InterActive }; //状态类型 private: int mode; int work_mode; //添加状态变量 public: friend class Tv; Remote(int m Tv::TV,int work Normal) : mode(m) , work_mode(work) {} 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(); } int show_mode() const { return work_mode; } }; #endif inline void Tv::set_Remode(Remote r) { r.work_mode (r.work_mode Remote::Normal) ? Remote::InterActive : Remote::Normal; }test_function.cpp#include test.h 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 TV is (state Off ? Off : On) endl; if (state On) { cout Volume setting volume endl; cout Channel setting channel endl; cout Mode (mode Antenna ? antenna : cable) endl; cout Input (input TV ? TV : DVD) endl; } }test.cpp#include iostream #include test.h int main() { Tv s42; cout Initial ettings for 42\ TV:\n; s42.settings(); s42.onoff(); s42.chanup(); cout \nAdjusted settings for 42\ Tv:\n; s42.settings(); Remote grey; grey.set_chan(s42, 10); grey.volup(s42); grey.volup(s42); cout \n42\ settings after using remote:\n; s42.settings(); Tv s58(Tv::On); s58.set_mode(); grey.set_chan(s58, 28); cout \n58\ settings:\n; s58.settings(); cout \n\nRemote work_mode: (grey.show_mode() Remote::Normal ? Normal : InterActive) endl; s58.set_Remode(grey); cout Remote work_mode: (grey.show_mode() Remote::Normal ? Normal : InterActive) endl; return 0; }演示效果如果朋友你感觉文章的内容对你有帮助可以点赞关注文章和专栏以及关注我哈嘿嘿嘿我会定期更新文章的谢谢朋友你的支持哈
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2628243.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!