《C++ Primer Plus》(第6版)第15章编程练习

《C++ Primer Plus》(第6版)第15章编程练习

1. Tv和Remote类

对Tv和Remote类做如下修改:

a. 让它们互为友元;
b. 在Remote类中添加一个状态变量成员,该成员描述遥控器是处于常规模式还是互动模式;
c. 在Remote中添加一个显示模式的方法;
d. 在Tv类中添加一个对Remote中新成员进行切换的方法,该方法应仅当TV处于打开状态时才能运行。

编写一个小程序来测试这些新特性。

程序:

tv.h:

#ifndef TV_H
#define TV_H

class Remote; // forward declaration

class Tv
{
private:
    int state;      // on or off
    int volume;     // assumed to be digitized
    int maxchannel; // maximum number of channels
    int channel;    // current channel setting
    int mode;       // broadcast or cable
    int input;      // TV or DVD

public:
    friend class Remote; // Remote can access Tv private parts
    enum
    {
        On,
        Off
    };
    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 inon() 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; // display all settings
    void set_rmode(Remote &r);
};

class Remote
{
private:
    int mode;  // control TV or DVD
    int rmode; // control Regular or Interact

public:
    friend class Tv;
    enum
    {
        Regular,
        Interact
    };
    Remote(int m = Tv::TV, int r = Regular) : mode(m), rmode(r) {}
    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_rmode() const;
};

inline void Tv::set_rmode(Remote &r)
{
    using std::cout;

    if (state == On)
        r.rmode = (r.rmode == Remote::Regular) ? Remote::Interact : Remote::Regular;
    else
        cout << "The tv is off, please turn it on first.\n";
}

#endif

tv.cpp:

#include <iostream>
#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
{
    using std::cout;
    using std::endl;

    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;
    }
}

void Remote::show_rmode() const
{
    using std::cout;
    using std::endl;

    cout << "Remote mode = " << (rmode == Regular ? "Regular" : "Interact") << endl;
}

use_tv.cpp:

// use_tv.cpp -- using the Tv and Remote classes
#include <iostream>
#include "tv.h"

int main()
{
    using std::cout;

    Tv s42;
    Remote grey;
    cout << "Initial settings for 42\" TV:\n";
    s42.settings();
    s42.onoff();
    s42.chanup();
    grey.show_rmode();
    s42.set_rmode(grey);
    cout << "\nAdjusted settings for 42\" TV:\n";
    s42.settings();
    s42.set_rmode(grey);
    grey.set_chan(s42, 10);
    grey.volup(s42);
    grey.volup(s42);
    cout << "\n42\" settings after using remote:\n";
    s42.settings();
    s42.set_rmode(grey);

    Tv s58(Tv::On);
    s58.set_mode();
    grey.set_chan(s58, 28);
    cout << "\n58\" settings:\n";
    s58.settings();
    grey.show_rmode();
    s58.set_rmode(grey);

    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

2. 异常

修改程序清单15.11,使两种异常类型都是从头文件提供的logic_error类派生出来的类。让每个what()方法都报告函数名和问题的性质。异常对象不用存储错误的参数值,而只需支持what()方法。

程序:

exc_mean.h:

#include <iostream>
#include <stdexcept>
#include <string>

class bad_hmean : public std::logic_error
{
public:
    double v1;
    double v2;
    bad_hmean(double a = 0, double b = 0, const std::string &s = "hmean() argument should not bo negative to each other.\n") : std::logic_error(s), v1(a), v2(b){};
};

class bad_gmean : public std::logic_error
{
public:
    double v1;
    double v2;
    bad_gmean(double a = 0, double b = 0, const std::string &s = "gmean() argument should be >= 0.\n") : std::logic_error(s), v1(a), v2(b){};
};

error.cpp:

#include <iostream>
#include <cmath>
#include "exc_mean.h"

double hmean(double a, double b);
double gmean(double a, double b);

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;

    double x, y, z;

    cout << "Enter two numbers: ";
    while (cin >> x >> y)
    {
        try
        {
            z = hmean(x, y);
            cout << "Harmonic mean of " << x << " and " << y
                 << " is " << z << endl;
            cout << "Geometric mean of " << x << " and " << y
                 << " is " << gmean(x, y) << endl;
            cout << "Enter next set of numbers <q to quit>:";
        }
        catch (bad_hmean &h)
        {
            cout << h.what();
            cout << "Values used: " << h.v1 << ", " << h.v2 << endl;
            cout << "Try again.\n";
            continue;
        }
        catch (bad_gmean &g)
        {
            cout << g.what();
            cout << "Values used: " << g.v1 << ", " << g.v2 << endl;
            cout << "Sorry, you don't get to play any more." << endl;
            break;
        }
    }
    cout << "Bye!" << endl;

    system("pause");
    return 0;
}

double hmean(double a, double b)
{
    if (a == -b)
        throw bad_hmean(a, b);
    return 2.0 * a * b / (a + b);
}

double gmean(double a, double b)
{
    if (a < 0 || b < 0)
        throw bad_gmean(a, b);
    return std::sqrt(a * b);
}

运行结果:

在这里插入图片描述

3. 派生异常

这个练习与编程练习2相同,但异常类是从一个这样的基类派生而来的:它是从logic_error派生而来的,并存储两个参数值。异常类应该有一个这样的方法:报告这些值以及函数名。程序使用一个catch块来捕获基类异常,其中任何一种从该基类异常派生而来的异常都将导致循环结束。

程序:

exc_mean.h:

// exc_mean.h  -- exception classes for hmean(), gmean()
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;

class bad : public logic_error
{
public:
    double v1;
    double v2;
    string name;
    explicit bad(double a = 0, double b = 0, const string &n = "hmean", const string &s = "Error in hmean()\n")
        : v1(a), v2(b), name(n), logic_error(s) {}
    void mesg();
    virtual ~bad() throw() {}
};

class bad_hmean : public bad
{
public:
    explicit bad_hmean(double a = 0, double b = 0, const string &n = "hmean", const string &s = "Error in hmean()\n")
        : bad(a, b, n, s) {}
    void mesg() { cout << "hmean(" << v1 << ", " << v2 << "): "
                       << "invalid arguments: a = -b.\n"; }
    virtual ~bad_hmean() throw() {}
};

class bad_gmean : public bad
{
private:
    string name;

public:
    explicit bad_gmean(double a = 0, double b = 0, const string &n = "gmean", const string &s = "Error in gmean()\n")
        : bad(a, b, n, s) {}
    string mesg() { return "gmean() arguments should be >= 0\n"; }
    virtual ~bad_gmean() throw() {}
};

error.cpp:

// error.cpp
#include <iostream>
#include <cmath> // or math.h, unix users may need -lm flag
#include "exc_mean.h"

// function prototypes
double hmean(double a, double b);
double gmean(double a, double b);

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;

    double x, y, z;

    cout << "Enter two numbers: ";
    while (cin >> x >> y)
    {
        try
        { // start of try block
            z = hmean(x, y);
            cout << "Harmonic mean of " << x << " and " << y
                 << " is " << z << endl;
            cout << "Geometric mean of " << x << " and " << y
                 << " is " << gmean(x, y) << endl;
            cout << "Enter next set of numbers <q to quit>: ";
        }                     // end of try block
        catch (bad_hmean &bg) // start of catch block
        {
            cout << bg.what();
            cout << "Error message:\n";
            bg.mesg();
            cout << "Try again.\n";
            continue;
        }
        catch (bad_gmean &hg)
        {
            cout << hg.what();
            cout << "Error message:\n"
                 << hg.mesg();
            cout << "Values used: " << hg.v1 << " , " << hg.v2 << endl;
            cout << "Sorry, you don't get to play any more.\n";
            break;
        } // end of catch block
    }
    cout << "Bye!\n";

    system("pause");
    return 0;
}

double hmean(double a, double b)
{
    if (a == -b)
        throw bad_hmean(a, b);
    return 2.0 * a * b / (a + b);
}

double gmean(double a, double b)
{
    if (a < 0 || b < 0)
        throw bad_gmean(a, b);
    return std::sqrt(a * b);
}

运行结果:

在这里插入图片描述

4. RTTI

程序清单15.16在每个try后面都使用两个catch块,以确保nbad_index异常导致方法label_val()被调用。请修改该程序,在每个try块后面只使用一个catch块,并使用RTTI来确保合适时调用label_val()。

程序:

sales.h:

#include <stdexcept>
#include <string>
using std::string;

class Sales
{
public:
    enum
    {
        MONTHS = 12
    };
    class bad_index : public std::logic_error
    {
    private:
        int bi; // bad index value
    public:
        explicit bad_index(int ix, const string &s = "Index error in Sales object\n");
        int bi_val() const { return bi; }
        virtual ~bad_index() throw() {}
    };
    explicit Sales(int yy = 0);
    Sales(int yy, const double *gr, int n);
    virtual ~Sales() {}
    int Year() const { return year; }
    virtual double operator[](int i) const;
    virtual double &operator[](int i);

private:
    double gross[MONTHS];
    int year;
};

class LabeledSales : public Sales
{
private:
    string label;

public:
    class nbad_index : public Sales::bad_index
    {
    private:
        string lbl;

    public:
        nbad_index(const string &lb, int ix, const string &s = "Index error in LabeledSales object\n");
        const string &label_val() const { return lbl; }
        virtual ~nbad_index() throw() {}
    };
    explicit LabeledSales(const string &lb = "none", int yy = 0);
    LabeledSales(const string &lb, int yy, const double *gr, int n);
    virtual ~LabeledSales() {}
    const string &Label() const { return label; }
    virtual double operator[](int i) const;
    virtual double &operator[](int i);
};

sales.cpp:

#include "sales.h"

Sales::bad_index::bad_index(int ix, const string &s) : std::logic_error(s)
{
    bi = ix;
}

Sales::Sales(int yy) : year(yy)
{
    for (int i = 0; i < MONTHS; i++)
        gross[i] = 0;
}

Sales::Sales(int yy, const double *gr, int n) : year(yy)
{
    int i;
    int lim = (n < MONTHS) ? n : MONTHS;
    for (i = 0; i < lim; i++)
        gross[i] = gr[i];
    for (; i < MONTHS; i++)
        gross[i] = 0;
}

double Sales::operator[](int i) const
{
    if (i < 0 || i >= MONTHS)
        throw bad_index(i);
    return gross[i];
}

double &Sales::operator[](int i)
{
    if (i < 0 || i >= MONTHS)
        throw bad_index(i);
    return gross[i];
}

LabeledSales::nbad_index::nbad_index(const string &lb, int ix, const string &s) : Sales::bad_index(ix, s)
{
    lbl = lb;
}

LabeledSales::LabeledSales(const string &lb, int yy) : Sales(yy)
{
    label = lb;
}

LabeledSales::LabeledSales(const string &lb, int yy, const double *gr, int n) : Sales(yy, gr, n)
{
    label = lb;
}

double LabeledSales::operator[](int i) const
{
    if (i < 0 || i >= MONTHS)
        throw nbad_index(Label(), i);
    return Sales::operator[](i);
}

double &LabeledSales::operator[](int i)
{
    if (i < 0 || i >= MONTHS)
        throw nbad_index(Label(), i);
    return Sales::operator[](i);
}

use_sales.cpp:

// use_sales.cpp  -- nested exceptions
#include <iostream>
#include "sales.h"
using namespace std;

int main()
{
    double vals1[12] = {1220, 1100, 1122, 2212, 1232, 2334, 2884, 2393, 3302, 2922, 3002, 3544};
    double vals2[12] = {12, 11, 22, 21, 32, 34, 28, 29, 33, 29, 32, 35};
    Sales sales1(2011, vals1, 12);
    LabeledSales sales2("Blogstar", 2012, vals2, 12);
    LabeledSales::nbad_index *nb;
    Sales::bad_index *b;

    cout << "First try block:\n";
    try
    {
        int i;
        cout << "Year = " << sales1.Year() << endl;
        for (i = 0; i < 12; ++i)
        {

            cout << sales1[i] << ' ';
            if (i % 6 == 5)
                cout << endl;
        }
        cout << "Year = " << sales2.Year() << endl;
        cout << "Label = " << sales2.Label() << endl;
        for (i = 0; i <= 12; ++i)
        {

            cout << sales2[i] << ' ';
            if (i % 6 == 5)
                cout << endl;
        }
        cout << "End of try block 1.\n";
    }
    catch (logic_error &bad)
    {
        cout << bad.what();
        if (nb = dynamic_cast<LabeledSales::nbad_index *>(&bad))
        {
            cout << "Company: " << nb->label_val() << endl;
            cout << "bad index: " << nb->bi_val() << endl;
        }
        else if (b = dynamic_cast<Sales::bad_index *>(&bad))
            cout << "bad index: " << b->bi_val() << endl;
    }
    cout << "\nNext try block:\n";
    try
    {
        sales2[2] = 37.5;
        sales1[20] = 23345;
        cout << "End of try block 2.\n";
    }
    catch (logic_error &bad)
    {
        cout << bad.what();
        if (nb = dynamic_cast<LabeledSales::nbad_index *>(&bad))
        {
            cout << "Company: " << nb->label_val() << endl;
            cout << "bad index: " << nb->bi_val() << endl;
        }
        else if (b = dynamic_cast<Sales::bad_index *>(&bad))
            cout << "bad index: " << b->bi_val() << endl;
    }
    cout << "done\n";

    system("pause");
    return 0;
}

运行结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UestcXiye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值