//第一题
//main.cpp
#include "TV.h"
int main()
{
Tv s42;
std::cout << "Initial settings for 42\" TV:\n";
s42.settings();
s42.onoff();
s42.chanup();
std::cout << "\nAdjusted settings for 42\" Tv:\n";
s42.settings();
Remote grey;
grey.set_chan(s42, 10);
grey.volup(s42);
grey.volup(s42);
std::cout << "\n42\" settings after using remote:\n";
s42.settings();
grey.ShowAMode();
s42.ChangeAMode(grey);
grey.ShowAMode();
return 0;
}
//TV.h
#ifndef TV_H_
#define TV_H_
#include <iostream>
class Tv
{
private:
int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
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 ^= 1; }
bool ison() const { return state == ON; }
bool volup();
bool voldown();
void chanup();
void chandown();
void set_mode() { mode ^= 1; }
void set_input() { input ^= 1; }
void settings() const;
void ChangeAMode(Remote &r);
};
class Remote
{
private:
int mode;
int AMode;
public:
friend class Tv;
Remote(int m = Tv::TV, int n = NORMAL) : mode(m), AMode(n) {}
enum { NORMAL, INTERACTIVE };
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(); }
void ShowAMode() const { std::cout << "现在是: " << (AMode == NORMAL ? "常规模式" : "交互模式") << std::endl; }
};
#endif
//TV.cpp
#include "TV.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
{
std::cout << "TV is " << (state == OFF ? "OFF" : "ON&#
C++ Primer Plus第六版 第十五章 编程练习答案
最新推荐文章于 2024-02-24 23:04:37 发布