当前位置: 首页 > 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.jsqmd.com/news/851466/

相关文章:

  • 深入解析浏览器端音频解密的5大核心技术:WebAssembly性能优化与安全架构
  • 告别手动调参!用Python脚本批量运行DSSAT模型,5分钟搞定上百个农田情景模拟
  • FreeRDP 终极指南:如何构建跨平台远程桌面解决方案
  • 2026年杭州抗变形建筑模板批发厂家有何独特优势? - GrowthUME
  • setup-java高级配置指南:如何自定义Java架构、包类型和本地安装
  • LFSR不止能生成随机数:盘点数字IC设计中那些意想不到的妙用(从PRBS测试到BIST设计)
  • 从芯片手册到CubeMX:手把手教你搞定STM32G431RBT6的时钟树与中断配置
  • 2026年自建房新趋势:支模木方如何引领绿色建筑革命 - GrowthUME
  • Newbie-Guideline数据库实战:SQL查询与ER模型设计的完整教程
  • 2026年AI搜索优化服务商怎么选?深度对比GEO方案与品牌 - GrowthUME
  • PHP Font Lib 实战:从零开始创建字体处理应用程序
  • FinalShell vs. Xshell:深度对比后,我为什么选它做主力SSH工具?附独家配置优化心得
  • TTK(Ops Test Tool Kit):昇腾CANN算子库的终极自动化测试框架完整指南
  • 保姆级教程:在Ubuntu 20.04上从零跑通VoxelNet(KITTI数据集+TensorFlow 1.14)
  • 2026年支模木方订做攻略:如何挑选靠谱厂家不踩坑 - GrowthUME
  • 3分钟搞定!Windows上安装Android应用的终极指南:APK Installer完全教程
  • 分享10款实测好用的降AI率软件(2026年3月版) - 殷念写论文
  • STM32CUBEMX+Keil AC6编译提速实战:解决LWIP和绝对地址警告的坑
  • Keil4编译出ERROR56别只点Rebuild!这3个隐藏设置没检查,文件路径对了也白搭
  • 2026年楼房支模优质厂家全解析:如何选择最合适的合作伙伴 - GrowthUME
  • 杭州海尚好不好?2026年5月亲测 - 界川
  • 从C/C++转战CAPL:我踩过的那些‘语法坑’和避坑指南(附实例代码)
  • 内容创作团队如何利用Taotoken聚合多模型生成多样化文案初稿
  • 网络工程师的AI新玩具:手把手教你用LLM微调打造专属‘网络诊断专家’
  • 2026年家用呼吸机口碑排行榜:这5大品牌最值得信赖 - 天涯视角
  • Zeu.js入门教程:5步创建你的第一个实时可视化组件
  • 终极Windows网络性能测试指南:5步方案实现iperf3高效部署
  • 厦门家装行业调研:金世琅——值得信任的全案整装公司 - GrowthUME
  • 保姆级教程:在Ubuntu 20.04上从源码编译PX4固件,并用Gazebo跑通你的第一个仿真
  • 如何永久免费解锁Cursor Pro高级功能:完整解决方案指南