2021-07-05 C++ Primer Plus 第十四章 友元、异常和其它 编程练习

编程练习

1.对Tv和Remote类做如下修改:

a. 让它们互为友元;

b.在Remote类中添加一个状态变量成员,该成员描述遥控器是出于常规模式还是互动模式;

c.在Remote中添加一个显示模式的方法;

d.在Tv类中添加一个对Remote中新成员进行切换的方法,该方法应当仅次于TV处于打开状态下才能运行。

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

TV.h

// tv.h -- Tv and Remote classes
#ifndef TV_H_
#define TV_H_
class Remote;
class Tv
{
public:
    friend class Remote;   // Remote can access Tv private parts
    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; // display all settings
    void shift_mode(Remote &r) const;
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
};

class Remote
{
private:
    int mode;              // controls TV or DVD
    bool mode_status;      // false:normal,true:interaction
public:
    friend class Tv;
    Remote(int m = Tv::TV) : mode(m),mode_status(false) {}
    void show_status() const;
    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();}
};
#endif

TV.cpp

// tv.cpp -- methods for the Tv class (Remote methods are inline)
#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 Tv::shift_mode(Remote &r) const{
    if (state == 1)
        if (r.mode_status)
            r.mode_status = false;
        else
            r.mode_status = true;
    else std::cout << "TV is off" << std::endl;
}

void Remote::show_status() const{
    if (mode_status)
        std::cout << "Remote mode status: interaction"
                  << std::endl;
    else
        std::cout << "Remote mode status: normal"
                  << std::endl;
}

main.cpp

#include <iostream>
#include "TV.h"

int main()
{
    using std::cout;
    Tv s42;
    cout << "Initial settings for 42\" TV:\n";
    s42.settings();
    s42.onoff();
    s42.chanup();
    cout << "\nAdjusted settings for 42\" TV:\n";
    s42.settings();

    Remote grey;
    grey.show_status();
    grey.set_chan(s42, 10);
    grey.volup(s42);
    grey.volup(s42);
    cout << "\n42\" settings after using remote:\n";
    s42.settings();
    s42.shift_mode(grey);
    grey.show_status();

    Tv s58(Tv::On);
    s58.set_mode();
    grey.set_chan(s58,28);
    cout << "\n58\" settings:\n";
    s58.settings();
    // std::cin.get();
    return 0;
}

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

exc_mean.h

// exc_mean.h  -- exception classes for hmean(), gmean()
#include <iostream>
#include <execution>
using namespace std;
class bad_hmean : public invalid_argument
{
public:
    bad_hmean() : invalid_argument("a + b = 0") {}
};

class bad_gmean : public range_error
{
public:
    bad_gmean() : range_error("a < 0, b < 0") {}
};

error4.cpp

#ifndef ERROR_EXC_MEAN_H
#define ERROR_EXC_MEAN_H
#include "exc_mean.h"
#include <iostream>
#include <cmath> // or math.h, unix users may need -lm flag
// function prototypes
using namespace std;
double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
    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 &gh)    // start of catch block
        {
            cout << "Caught the error!" << endl;
            cout << gh.what() << endl;
            cout << "Try again.\n";
            continue;
        }
        catch (bad_gmean &bg)
        {
            cout << "Caught the error!" << endl;
            cout << bg.what() << endl;
//            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";
    return 0;
}

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

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

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

exc_mean.h

#include <iostream>
#include <exception>
using namespace std;
class bad : public logic_error
{
private:
    double v1,v2;
public:
    bad(double v1,double v2) : v1(v1), v2(v2), logic_error("logic error!") {}
    void show_error() { cout << "error value: " << v1 <<" "<<v2 << endl;}
};

error4.cpp

#ifndef ERROR_EXC_MEAN_H
#define ERROR_EXC_MEAN_H
#include "exc_mean.h"
#include <iostream>
#include <cmath> // or math.h, unix users may need -lm flag
// function prototypes
using namespace std;
double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
    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 &gh)    // start of catch block
        {
            cout << "Caught the error!" << endl;
            cout << gh.what() << endl;
            gh.show_error();
//            cout << ".\n";
            break;
        }
    }
    cout << "Bye!\n";
    return 0;
}

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

double gmean(double a, double b)
{
    if (a < 0 || b < 0)
        throw bad(a,b);
    return std::sqrt(a * b);
}
#endif //ERROR_EXC_MEAN_H

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

sales.h

#ifndef SALES_SALES_H
#define SALES_SALES_H
#include <stdexcept>
#include <string>

class Sales
{
public:
    enum {MONTHS = 12};   // could be a static const
    class bad_index : public std::logic_error
    {
    private:
        int bi;  // bad index value
    public:
        explicit bad_index(int ix,
                           const std::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
{
public:
    class nbad_index : public Sales::bad_index
    {
    private:
        std::string lbl;
    public:
        nbad_index(const std::string & lb, int ix,
                   const std::string & s = "Index error in LabeledSales object\n");
        const std::string & label_val() const {return lbl;}
        virtual ~nbad_index() throw() {}
    };
    explicit LabeledSales(const std::string & lb = "none", int yy = 0);
    LabeledSales(const std::string & lb, int yy, const double * gr, int n);
    virtual ~LabeledSales() { }
    const std::string & Label() const {return label;}
    virtual double operator[](int i) const;
    virtual double & operator[](int i);
private:
    std::string label;
};



#endif //SALES_SALES_H

sales.cpp

#include "sales.h"using std::string;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 lim = (n < MONTHS)? n : MONTHS;    int i;    for (i = 0; i < lim; ++i)        gross[i] = gr[i];    // for i > n and i < MONTHS    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);}

main.cpp

#include <iostream>#include "sales.h"int main(){    using std::cout;    using std::cin;    using std::endl;    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 );    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(Sales::bad_index & bad)    {        cout << bad.what();        if (typeid(bad)== typeid(LabeledSales::nbad_index))        {            cout << "Company: " << ((LabeledSales::nbad_index &)bad).label_val() << endl;            cout << "bad index: " << bad.bi_val() << endl;        }        else        {            cout << "bad index: " << bad.bi_val() << endl;        }    }    cout << "\nNext try block:\n";    try    {        sales2[2] = 37.5;        sales1[20] = 23345;        cout << "End of try block 2.\n";    }    catch(Sales::bad_index & bad)    {        cout << bad.what();        if (typeid(bad)== typeid(LabeledSales::nbad_index))        {            cout << "Company: " << ((LabeledSales::nbad_index &)bad).label_val() << endl;            cout << "bad index: " << bad.bi_val() << endl;        }        else        {            cout << "bad index: " << bad.bi_val() << endl;        }    }    cout << "done\n";    // std::cin.get();    return 0;}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,686评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,668评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,160评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,736评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,847评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,043评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,129评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,872评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,318评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,645评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,777评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,470评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,126评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,861评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,095评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,589评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,687评论 2 351

推荐阅读更多精彩内容