1.
main
#include "TV.h"
int main()
{
using std::cout;
Tv s42;
Remote grey;
cout << "Initial settings for 42\" TV:\n";
s42.settings();
grey.show_r_mode();
s42.change_r_mode(grey);
s42.onoff();
s42.chanup();
cout << "\nAdjusted settings for 42\" TV:\n";
s42.chanup();
cout << "\nAdjusted settings for 42\" TV:\n";
s42.settings();
s42.change_r_mode(grey);
grey.set_chan(s42, 10);
grey.volup(s42);
grey.volup(s42);
cout << "\n42\" settings after using remote:\n";
s42.settings();
s42.change_r_mode(grey);
Tv s58(Tv::On);
s58.set_mode();
grey.set_chan(s58, 28);
cout << "\n58\" settings:\n";
s58.settings();
grey.show_r_mode();
s58.change_r_mode(grey);
return 0;
}
TV.h
#include <iostream>
class Remote;
class Tv
{
public:
enum {
Off, On };
enum {
MinVal, MaxVal = 20 };
enum {
Antenna, Cable };
enum {
TV, DVD };
friend class Remote;
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 change_r_mode(Remote & r);
private:
int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
};
class Remote
{
friend class Tv;
private:
int mode;
int r_mode;
public:
enum {
Regular, Interact };
Remote(int m = Tv::TV, int f = Regular) : mode(m), r_mode(f) {
}
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 show_r_mode() const {
std::cout << "Now the remote mode is " << (r_mode == Regular ? "Regular" : "Interact") << ".\n"; }
};
inline void Tv::change_r_mode(Remote & r)
{
if (ison