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

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

a. 让他们互相称为友元;
b. 在Remote类中添加一个状态变量成员,该成员描述遥控器是否处于常规模式还是互动模式;
c. 在Remote类中添加一个显式模式的方法
d. 在Tv类中添加一个对Remote中新成员进行切换的方法,该方法今在Tv处于打开状态时才能运行。编写一个小程序来测试这些新特性。
本题不算难,首先需要加一个前向声明在Tv class的声明之前,这样才可以将Remote声明为Tv的友元类,添加状态成员为私有,修改Remote的构造函数,添加一个显式模式的方法,在Tv中添加对Remote进行切换的方法,同时在Remoter中调用该函数,使遥控器可以切换是否为互动模式,这里实现时需要用到*this指针。代码如下:

// tv.h -- class definition for Tv class and Remote class
// friend class

#ifndef TV_H_
#define TV_H_
class Remote;
class Tv
{
private:
    int state;
    int channel;
    int maxchannel;
    int volume;
    int mode;
    int input;
public:
    friend class Remote;
    enum {Off, On};
    enum {MaxChannel = 125};
    enum {MinVol, MaxVol = 25};
    enum {Antenna, Cable};
    enum {TV, DVD};
    Tv(int s = Off, int mc = MaxChannel) : state(s), channel(1), 
        maxchannel(mc), volume(5), mode(Cable), input(TV) { }
    void onoff() { state = (state == On) ? Off : On;}
    bool isOn() { return state == On; }
    bool volup();
    bool voldown();
    void chanup();
    void chandown();
    void set_mode() { mode = (mode == Cable) ? Antenna : Cable; }
    void set_input() { input = (input == TV) ? DVD : TV; }
    void set_rmode(Remote & r);     // set mode of Remote
    void settings() const;
};

class Remote
{
private:
    int mode;       // control TV or DVD
    int r_mode;     // control Normal or Interactive
public:
    friend class Tv;
    enum {Normal, Interactive};
    Remote(int m = Tv::TV, int rm =Normal) : mode(m), r_mode(rm) { }
    void onoff(Tv & t) { t.onoff(); }
    bool volup(Tv & t) { return t.volup(); }
    bool voldown(Tv & t) { return t.voldown(); }
    void chanup(Tv & t) { t.chanup(); }
    void chandown(Tv & t) { t.chandown(); } 
    void setchannel(Tv & t, int chan) { t.channel = chan; }
    void set_mode(Tv & t) { t.set_mode(); }
    void set_input(Tv & t) { t.set_input(); }
    void set_rmode(Tv & t);
    void display_mode() const;
};
#endif
// tv.cpp -- methods for Tv class and Remote class
#include <iostream>
#include "tv.h"

bool Tv::volup()
{
    if (volume < MaxVol)
    {
        volume++;
        return true;
    }
    else
        return false;
}

bool Tv::voldown()
{
    if (volume > MinVol)
    {
        volume--;
        return true;
    }
    else
        return false;
}

void Tv::chanup()
{
    if (channel < MaxVol)
        channel++;
    else
        channel = 1;
}

void Tv::chandown()
{
    if (channel > 1)
        channel--;
    else
        channel = MaxChannel;
}

void Tv::set_rmode(Remote & r)
{
    if (isOn())
        r.r_mode = (r.r_mode == Remote::Normal) ? 
            Remote::Interactive : Remote::Normal;
}

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::set_rmode(Tv & t)
{
    t.set_rmode(* this);
}

void Remote::display_mode() const
{
    using std::cout;
    using std::endl;
    cout << "Remote is " << (r_mode == Normal ? "Normal" : "Interactive") << endl;
}
// usetv.cpp -- test Tv and Remote class
// compile with tv.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();
    s42.chanup();
    cout << "\nAdjusted settings for 42\" TV:\n";
    s42.settings();

    Remote grey;

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

    Tv s58(Tv::On);
    s58.set_mode();
    grey.setchannel(s58, 28);
    cout << "\n58\" settings:\n";
	s58.settings();
	grey.display_mode();
	grey.set_rmode(s58);		// test set_rmode
    grey.display_mode();
    return 0;
}

运行结果如下:
image

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

本题考查的是logic_error派生类的定义,需要注意logic_error的构造函数需要一个字符串作为参数;定义好两个错误类之后,修改了程序清单15.11让错误指向logic_error的引用,统一输出信息what(),再根据可不可以转换成bad_hmean引用来决定终止还是继续尝试。代码如下:

// exc_mean.h -- class for exception of hmean and gmean
#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_
#include <stdexcept>
#include <iostream>
#include <string>

class bad_hmean : public std::logic_error
{
public:
    explicit bad_hmean(const std::string & s = "hmean(): Invalid argument a = -b")
         : logic_error(s) { }
    virtual const char * what();
    virtual ~bad_hmean() { }
};

class bad_gmean : public std::logic_error
{
public:
    explicit bad_gmean(const std::string & s = "gmean() : Arguments shoule be >= 0")
         : logic_error(s) { }
    virtual const char * what();
    virtual ~bad_gmean() { }
};

#endif

inline const char * bad_hmean::what()
{
    return logic_error::what();
}

inline const char * bad_gmean::what()
{
    return logic_error::what();
}
// error4_15_11.cpp -- using exception classes
#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::cout;
    using std::cin;
    using std::endl;

    double x, y, z;

    cout << "Enter two nembers: ";
    while (cin >> x >> y)
    {
        try{
            z = hmean(x, y);
            cout << "Harmonic mean of " << x << " and " << y
                 << " is " << z << endl;
			z = gmean(x, y);
            cout << "Geimetric mean of " << x << " and " << y
                 << " is " << z << endl;
            cout << "Enter next set of numbers <q to quit>: ";
        }
        catch (std::logic_error & le)
        {
            cout << le.what() << endl;
			try
			{
				le = dynamic_cast<bad_hmean &> (le);
			}
			catch(const std::bad_cast& bc)
			{
				cout << "Sorry, you don't get to play any more.\n";
            	break;
			}
			
            cout << "Try again.\n";
            continue;
        }
    }
    cout << "Bye!\n";
    return 0;
}

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

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

运行结果如下:
image

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

本题有不算难,与第二题类似,首先从logic_error派生出一个抽象类bad_meanABC,然后从该类派生出两个分别处理hmean()和gmean()的类,主函数修改catch部分即可。代码如下:

// exc_mean.h -- definition of exception of hmean() and gmean()
#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_
#include <string>
#include <stdexcept>
#include <iostream>

class bad_meanABC : public std::logic_error
{
private:
    double a_;
    double b_;
public:
    bad_meanABC(double a = 0, double b = 0, const std::string & s = "function name")
        : logic_error(s), a_(a), b_(b) { }
    double a_val() const { return a_; }
    double b_val() const { return b_; }
    virtual ~bad_meanABC() { }
    virtual void mesg() const = 0;
};

class bad_hmean : public bad_meanABC
{
public:
    bad_hmean(double a = 0, double b = 0, const std::string & s = "hmean") :
    bad_meanABC(a,b, s) { }
    ~ bad_hmean() { }
    virtual void mesg() const;
};

class bad_gmean : public bad_meanABC
{
public:
    bad_gmean(double a = 0, double b = 0, const std::string & s = "gmean") :
    bad_meanABC(a,b, s) { }
    ~ bad_gmean() { }
    virtual void mesg() const;
};
#endif

inline void bad_hmean::mesg() const
{
    std::cout << logic_error::what() 
        << "(" << a_val() << ", " << b_val() << "): ";
    std::cout << "Invalid argument a = -b \n";
}

inline void bad_gmean::mesg() const
{
    std::cout << logic_error::what() 
        << "(" << a_val() << ", " << b_val() << "): ";
    std::cout << "Arguments a b should be >=0\n";
}
// pe3.cpp -- inheritance logic_error
#include "exc_mean.h"
#include <cmath>
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;
			z = gmean(x, y);
			cout << "Geimetric mean of " << x << " and "
				 << y << " is " << z << endl;
			cout << "Enter next set of numbers <q to quit>: ";
		}
		catch(const bad_meanABC & bm)
		{
			bm.mesg();
			cout << "Sorry, you don't get to play any more.\n";
			break;
		}
	}
	return 0;
}

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

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

运行结果如下:

image

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

本题不算难,但比较恶心。首先是异常类是嵌套类,该开始没有找到合适的基类来将两个类都引用,后来找到了logic_error类,又发现使用dynamic_cast存在问题,原因是使用了bad进行转换,以前使用目标类型的引用可以转换,是因为异常类不是嵌套类,这里两个不同的异常类又属于不同的嵌套类成员,因此需要首先声明一个转换后类型的变量,例如对于LabeledSales::nbad_index,这里应该声明地址,声明引用的需要赋初始值,而且地址比引用更适合使用dynamic_cast转换。代码如下:

// sales.h -- exceptions and inheritance
#include <stdexcept>
#include <string>

#ifndef SALES_H_
#define SALES_H_
class Sales
{
protected:
    enum {MONTHS = 12};
public:
    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.cpp -- Sales implementation
#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 < 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);
}
// use_sales15_16.cpp -- nested excpetions
#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(std::logic_error & le)
    {
        cout << le.what();
        try{
            LabeledSales::nbad_index & bad = dynamic_cast<LabeledSales::nbad_index &>(le);
            cout << "Company: " << bad.label_val() << endl;
            cout << "bad index: " << bad.bi_val() << endl;
        }
        catch(std::bad_cast)
        {
            Sales::bad_index & bad = dynamic_cast<Sales::bad_index &>(le);
            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(std::logic_error & le)
    {
        cout << le.what();
        try{
            LabeledSales::nbad_index & bad = dynamic_cast<LabeledSales::nbad_index &>(le);
            cout << "Company: " << bad.label_val() << endl;
            cout << "bad index: " << bad.bi_val() << endl;
        }
        catch(std::bad_cast)
        {
            Sales::bad_index & bad = dynamic_cast<Sales::bad_index &>(le);
            cout << "bad index: " << bad.bi_val() << endl;
        }
    }
    return 0;
}

使用引用的动态转换代码与使用地址的代码只有主函数不同,就编程而言,可以使用地址动态转换时尽量使用地址,使用引用的代码如下:

// use_sales15_16.cpp -- nested excpetions
#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(std::logic_error & le)
    {
        cout << le.what();
        try{
            LabeledSales::nbad_index & bad = dynamic_cast<LabeledSales::nbad_index &>(le);
            cout << "Company: " << bad.label_val() << endl;
            cout << "bad index: " << bad.bi_val() << endl;
        }
        catch(std::bad_cast)
        {
            Sales::bad_index & bad = dynamic_cast<Sales::bad_index &>(le);
            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(std::logic_error & le)
    {
        cout << le.what();
        try{
            LabeledSales::nbad_index & bad = dynamic_cast<LabeledSales::nbad_index &>(le);
            cout << "Company: " << bad.label_val() << endl;
            cout << "bad index: " << bad.bi_val() << endl;
        }
        catch(std::bad_cast)
        {
            Sales::bad_index & bad = dynamic_cast<Sales::bad_index &>(le);
            cout << "bad index: " << bad.bi_val() << endl;
        }
    }
    return 0;
}

运行结果如下:
image

posted @ 2022-01-07 22:38  Fight!GO  阅读(68)  评论(0编辑  收藏  举报