C++ Primer Plus第六版第十五章友元、异常和其他编程练习答案

本文提供了C++ Primer Plus第六版第十五章关于友元和异常的编程练习答案,包括main、TV.h、main.cpp、test.h及sales.h等文件的相关内容解析。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值