当前位置: 首页 > news >正文

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.gsyq.cn/news/1329052.html

相关文章:

  • MIT Cheetah-Software编译手记:搞定Qt5.10.0路径、LCM依赖与那些诡异的C++报错
  • LM317电源模块的“隐藏参数”与实战避坑:为什么你的空载电压总是不稳?
  • 告别JS逆向调试烦恼:WT-JS_DEBUG_V1.8.3保姆级安装与实战AES解密教程
  • 规避“造轮子”陷阱:企业级 Web 表格组件选型与 TCO 避坑指南
  • ChinesePinyin-CodeCompletionHelper项目维护与社区贡献指南:打造更好的中文编程体验
  • Windows11项目安全警示:常见系统漏洞与防范措施
  • OpenHarmony特性配置实战:从原理到开发板系统裁剪
  • 运算放大器失调电压(Vos)深度解析:从原理、影响、测量到选型与电路设计实战
  • ComfyUI-Impact-Pack终极指南:如何快速实现专业级AI图像增强
  • 别再手动找体边面了!用NX二次开发UF_MODL_ask_feat_函数一键搞定
  • YOLO-ONNX-Java图像缩放算法:保持宽高比与边界填充的工程实践
  • 李开复与苏姿丰炉边对话:2026年AI替代的不是岗位,而是整个部门
  • qb-web组件架构详解:Vue+TypeScript的最佳实践指南
  • 无王无帝定乾坤,来自田间第一人 大道无形定江山
  • CANN/Ascend C开发工具包
  • 德州动力滚筒输送机厂家实测排行:多维度品质对比解析 - 奔跑123
  • CANN/asc-devkit:SIMD-API核内同步Mutex锁
  • AWBus-lite:嵌入式轻量级软件总线与拓扑感知通信架构实践
  • 保姆级教程:在Ubuntu 20.04上从源码编译PX4固件,并用Gazebo跑通你的第一个仿真
  • 保姆级教程:在Ubuntu 20.04上从零跑通VoxelNet(KITTI数据集+TensorFlow 1.14)
  • 从芯片手册到CubeMX:手把手教你搞定STM32G431RBT6的时钟树与中断配置
  • 如何免费解锁Cursor AI Pro功能:开源工具完全指南
  • CANN/asc-devkit 切片数据搬运
  • 暗黑破坏神2存档编辑器的5个核心问题与完美解决方案
  • 3分钟学会:B站视频下载神器BiliDownload终极使用指南
  • PlatformIO国内安装避坑全记录:解决Python环境、pip换源与网络下载慢的问题
  • 打破Mac与Windows的存储壁垒:Free-NTFS-for-Mac完整读写指南
  • LumenPnP开源贴片机:从DIY到小批量生产的完整技术方案
  • 终极Windows和Office激活指南:KMS智能激活工具三步永久激活方案
  • 如何在浏览器中实现音乐文件格式转换与本地解密:Unlock Music完整指南