C++-练习-86

news/2024/11/17 7:26:23/

题目:

对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; }
};#endifinline 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;}elsereturn false;
}bool Tv::voldown()
{if (volume > MinVal){volume--;return true;}elsereturn false;
}void Tv::chanup()
{if (channel < maxchannel)channel++;elsechannel = 1;
}void Tv::chandown()
{if (channel > 1)channel--;elsechannel = 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.ppmy.cn/news/578860.html

相关文章

LeetCode-86

给你一个链表的头节点 head 和一个特定值 x &#xff0c;请你对链表进行分隔&#xff0c;使得所有小于 x 的节点都出现在大于或等于 x 的节点之前。你应当保留两个分区中每个节点的初始相对位置。 例如 输入&#xff1a;head [1,4,3,2,5,2], x 3 输出&#xff1a;[1,2,2,4,3,…

芯片的架构

在了解这些架构之前&#xff0c;我们应该先了解一下复杂指令集&#xff08;CISC&#xff09;和精简指令集&#xff08;RISC&#xff09;。怎么说这两个的区别呢&#xff1f;CISC的设计思路更加注重性能的发展&#xff0c;是一种高性能高功耗的芯片&#xff0c;在高密度的计算上…

android x 86

http://code.google.com/p/android-x86/

安卓android chrome86,UC浏览器86版本

UC浏览器86版本是一款功能更加强大的百度搜索引擎&#xff0c;提升流畅体验&#xff0c;不卡顿&#xff0c;使搜索更加快速。简约的搜索框和轻量设计使用户体验更加流畅&#xff0c;搜索结果更加清晰。首页还有实时热门新闻推荐&#xff0c;最新资讯视频浏览&#xff0c;还可以…

BestCoder #86

BestCoder #86 今年暑假最后一次BC了&#xff0c;结果B题少加了个判断终测WA了&#xff0c;很不爽...... 1001 Price List [hdu 5804]签到题 求出所有数的和sumsum&#xff0c;如果q > sumq>sum那么肯定记多了。 时间复杂度O(n)O(n)。 以上是引用官方题解... #include &…

86、87、88

#include <iostream> #include <stack> #include <queue> using namespace std; int main() { int a[] {5, 1, 4, 6}; cout << "存放整型元素的栈的操作&#xff1a;" << endl; stack<int> iStack; for (int i 0; i < 4; i…

leetcode 86

先找到第一个大于等于target的数p&#xff0c;然后从这个数开始往后遍历链表&#xff0c;若比target小则放在p之前 ListNode* partition(ListNode* head, int x) {ListNode* dummy new ListNode(-1);dummy->next head;ListNode *p dummy;ListNode *q head;while(p->…

x86-64和x86

因为X86-64的CPU&#xff0c;意思就是64位的cpu。   x86是英特尔首先开发制造的一种微处理器体系结构的泛称。该系列较早期的处理器名称是以86数字作为结尾来表示&#xff0c;因此其架构被称为“x86&#xff0c;把x86-32称为32-bit”。 由于处理器的发展遇到了瓶颈&#xf…