题目:
对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;
}
演示效果:
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈