C++ Primer Plus 第六版 第十五章 练习题参考答案


习题一
习题要求:修改Tv和Remote类满足题设要求
练习目标:了解友元类的前向声明语法

//tv.h
#ifndef TV_H_
#define TV_H_
#include<iostream>
#include<string>

class Remote;
class Tv
{
    friend class Remote;
private:
    int state;
    int volume;
    int maxchannel;
    int channel;
    int mode;
    int input;
public:
    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 set_remote_mode(Remote & r);
    void settings() const;
};
class Remote
{
    friend class Tv;
private:
    int mode;
    int remote_mode;
    enum {custom,interact}; 
public:
    Remote(int m = Tv::TV) : mode(m), remote_mode(custom) {}
    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_mode(Tv &t);
};
#endif // !EMP_H_
//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::set_remote_mode(Remote & r)
{
    if(state == On)
        r.remote_mode = (r.remote_mode == r.custom) ? r.interact : r.custom;
}
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_mode(Tv &t)
{
    using std::cout;
    using std::endl;
    cout << "Tv is " << (t.state == t.Off ? "Off" : "On") << endl;
    if (t.state == t.On)
        cout << "Remoter setting = " << remote_mode << endl;
}
//main.cpp
#include <iostream>
#include<cstring>
#include"tv.h"

int main()
{
    using namespace std;
    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.set_chan(s42, 10);
    grey.volup(s42);
    grey.volup(s42);
    grey.show_mode(s42);
    cout << "\nn42\" settings after using remote:\n";
    s42.settings();
    s42.set_remote_mode(grey);
    grey.show_mode(s42);
    system("pause");
    return 0;
}

习题二
习题要求:修改程序清单15.11,使得异常类满足题设要求
练习目标:了解并掌握异常类stdexcept使用,头文件请参考习题三。

//main.cpp
#include <iostream>
#include<cstdlib>
#include<stdexcept>
#include"exc_mean.h"

double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
    using std::endl;
    using std::cout;
    using std::cin;

    double x, y, z;

    cout << "Enter two nums: ";
    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_gmean &oe)
        {
            cout << "Caught the gmean exception!\n";
            cout << oe.what() << endl;
            cout << "Try again.<q to quit>\n";
            continue;
        }
        catch (bad_hmean &oe)
        {
            cout << "Caught the hmean exception!\n";
            cout << oe.what() << endl;
            cout << "Enter next set of numbers <q to quit>: ";
            continue;
        }
    }
    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);
}

习题三
习题要求:修改程序清单15.11,使得异常类满足题设要求。
练习目标:了解并掌握异常类stdexcept使用。
注意事项:仅需改动头文件,并在方法文件中调用方法即可。此外,请注意,logic_error是不存在默认构造函数的,请熟悉本例中它的初始化方法,或者可以在派生类函数的构造函数前添加关键字explicit以禁止隐式转换。

#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_
#include<iostream>

class bad_hmean :public std::logic_error
{
private:
    double v1;
    double v2;
public:
    bad_hmean(double a = 0, double b = 0) : logic_error(""),v1(a), v2(b){}
    const char * what() { return "bad arguments to hmean(),sum is nod allowed being zero!"; }
    void what_n() { std::cout << "the wrong nums is: " << v1 << " ," << v2 << "\n"; }
};
class bad_gmean:public std::logic_error
{
public:
    double v1;
    double v2;
    bad_gmean(double a = 0, double b = 0) :logic_error(""),v1(a), v2(b){}
    const char * what() { return "bad argumens to gmean(),they should be positive!"; };
    void what_n() { std::cout << "the wrong nums is: " << v1 << " ," << v2 << "\n"; }
};

#endif // !EMP_H_

习题三
习题要求:修改程序清单15.16,使得捕获异常类满足题设要求,即仅需一个catch块实现捕获两种异常
练习目标:了解并掌握异常类stdexcept使用,并且学会使用 RTTI判断类的类型,以及基类和派生类之间的转换,本例中使用的是 typeid()==typeid() 用于判断异常类类型,之后利用 (expression) = static_cast<> (expression) 用于基类和派生类之间的类型转换,以达到利用不同派生类所定义的不同方法的目的。

//15-14.h
#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_
#include<cstring>
#include<stdexcept>

class Sales
{
public:
    enum {MONTHS = 12};
    class bad_index : public std::logic_error
    {
    private:
        int bi;
    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 
//15-15.cpp
#include "exc_mean.h"

Sales::bad_index::bad_index(int ix, const std::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 < 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 std::string & lb, int ix, const std::string & s):Sales::bad_index(ix,s)
{
    lbl = lb;
}

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

LabeledSales::LabeledSales(const std::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);
}
//15-16.cpp
#include <iostream>
#include<stdexcept>
#include<string>
#include"exc_mean.h"

int main()
{
    using std::endl;
    using std::cout;
    using std::cin;
    using std::string;
    double vals[12] =
    {
        1220,1100,1122,2212,1232,2334,
        2884,2394,3302,2912,3002,3544
    };
    double vals2[12] =
    {
        12,11,22,21,32,34,
        28,29,33,29,32,35
    };
    Sales sales1(2011, vals, 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 (std::logic_error & bad)
    {
        if (typeid(LabeledSales::nbad_index) == typeid(bad))
        {
            LabeledSales::nbad_index *nbad = static_cast<LabeledSales::nbad_index *> (&bad);
            cout << nbad->what();
            cout << "Company: " << nbad->label_val() << endl;
            cout << "bad_index: " << nbad->bi_val() << endl;
        }
        else
        {
            Sales::bad_index *nbad = static_cast<Sales::bad_index *> (&bad);
            cout << nbad->what();
            cout << "bad index: " << nbad->bi_val() << endl;
        }
    }
    cout << "\nNext try block:\n";
    try
    {
        sales2[2] = 37.5;
        sales1[20] = 23345;
        cout << "End of try block 2.\\n";
    }
    catch (std::logic_error & bad)
    {
        if (typeid(LabeledSales::nbad_index) == typeid(bad))
        {
            LabeledSales::nbad_index *nbad = static_cast<LabeledSales::nbad_index *> (&bad);
            cout << nbad->what();
            cout << "Company: " << nbad->label_val() << endl;
            cout << "bad_index: " << nbad->bi_val() << endl;
        }
        else
        {
            Sales::bad_index *nbad = static_cast<Sales::bad_index *> (&bad);
            cout << nbad->what();
            cout << "bad index: " << nbad->bi_val() << endl;
        }
    }
    cout << "done\n";
    system("pause");
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。