1.对Tv 和 Remote 类做如下修改:
a,让它们互为友元;
b.在 Remote类中添加一个状态变量成员,该成员描述遥控器是处于常规模式还是互动模式:
c,在 Remote 中添加一个显示模式的方法
d.在T类中添加一个对 Remote 中新成员进行切换的方法,该方法应仅当 TV 处于打开状态时才能运行。
编写一个小程序来测试这些新特性。
- #include
- #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.set_chan(s42, 10);
- grey.volup(s42);
- grey.volup(s42);
- s42.set_status(grey);
- cout << "\n42\" settings after using remote:\n";
- s42.settings();
- grey.show_status();
-
- Tv s58(Tv::On);
- s58.set_mode();
- grey.set_chan(s58, 28);
- s58.set_status(grey);
- cout << "\n58\" settings:\n";
- s58.settings();
- grey.show_status();
-
- return 0;
- }
- #ifndef TV_H_
- #define TV_H_
- #include
-
- class Remote; //先声明Remote类然后再定义, 以便编译器有足够的信息编译使用Remote类的方法;
-
- class Tv
- {
- public:
- friend class Remote;
- 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;
- void set_status(Remote &r); //对Remote类中状态成员进行切换的方法;
-
- private:
- int state;
- int volume;
- int maxchannel;
- int channel;
- int mode;
- int input;
- };
-
- class Remote
- {
- private:
- int mode;
- int status; //状态变量成员;
-
- public:
- friend class Tv;
- enum { Convention, Interaction }; //Convention表示常规模式, Interaction表示互动模式;
- Remote(int m = Tv::TV, int s = Convention) : mode(m), status(s) {} //默认为常规模式;
- 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_status() const;
- };
-
- inline void Tv::set_status(Remote &r)
- {
- if (ison())
- {
- r.status = Remote::Convention == r.status ? Remote::Interaction : Remote::Convention;
- }
- }
-
- inline void Remote::show_status() const
- {
- std::cout << "Status = " << (status == Convention ? "Convention" : "Interaction") << std::endl;
- }
-
- #endif
- #include
- #include "tv.h"
-
- bool Tv::volup()
- {
- if (volume < MaxVal)
- {
- volume++;
- return true;
- }
- return false;
- }
-
- bool Tv::voldown()
- {
- if (volume > MinVal)
- {
- volume--;
- return true;
- }
- return false;
- }
-
- void Tv::chanup()
- {
- channel = channel < maxchannel ? 1 + channel : 1;
- }
-
- void Tv::chandown()
- {
- channel = channel > 1 ? channel - 1 : 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 = ";
- cout << (mode == Antenna ? "antenna" : "cable") << endl;
- cout << "Input = ";
- cout << (input == TV ? "TV" : "DVD") << endl;
- }
- }
2.修改程序清单 15.11,使两种异常类型都是从头文件stdexcepi>提供的 logic_error 类派生出来的类让每个 what()方法都报告函数名和问题的性质。异常对象不用存储错误的参数值,而只需支持 what()方法
- #include
- #include
- #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;
- cout << " is " << z << endl;
- z = gmean(x, y);
- cout << "Geometric mean of " << x << " and " << y;
- cout << " is " << z << endl;
- cout << "Enter next set of numbers
: "
; - }
- catch (bad_hmean &bg)
- {
- cout << bg.what(); //调用what方法打印hmean中的异常;
- cout << "Try again.\n";
- continue;
- }
- catch (bad_gmean &hg)
- {
- cout << hg.what(); //调用what方法打印gmean中的异常;
- cout << "Sorry, you don't get to play any more.\n";
- break;
- }
- }
- 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);
- }
- #ifndef EXC_MEAN_H_
- #define EXC_MEAN_H_
- #include
- #include
- #include
- using namespace std;
-
- class bad_hmean : public logic_error
- {
- public:
- explicit bad_hmean(const string &s = "Error in bad_hmean object\nhmean() arguments should be a != -b\n") : logic_error(s) {}
- };
-
- class bad_gmean : public logic_error
- {
- public:
- explicit bad_gmean(const string &s = "Error in bad_hmean object\ngmean() arguments should be >= 0\n") : logic_error(s) {}
- };
-
- #endif
-
-
3.这个练习与编程练习2相同,但异常类是从一个这样的基类派生而来的:它是从 logic_crror 派生而来的,并存储两个参数值。异常类应该有一个这样的方法:报告这些值以及函数名。程序使用一个 catch块来捕获基类异常,其中任何一种从该基类异常派生而来的异常都将导致循环结束。
- #include
- #include
- #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;
- cout << " is " << z << endl;
- z = gmean(x, y);
- cout << "Geometric mean of " << x << " and " << y;
- cout << " is " << z << endl;
- cout << "Enter next set of numbers
: "
; - }
- catch (mean &m) //捕获基类以及派生类的任何异常;
- {
- cout << "Sorry, you don't get to play any more.\n";
- break;
- }
- }
- cout << "Bye!\n";
-
- return 0;
- }
-
- double hmean(double a, double b)
- {
- if (a == -b)
- {
- bad_hmean temp("hmean", a, b);
- temp.mesg();
- throw temp;
- }
- return 2.0 * a * b / (a + b);
- }
-
- double gmean(double a, double b)
- {
- if (a < 0 || b < 0)
- {
- bad_gmean temp("gmean", a, b);
- temp.mesg();
- throw temp;
- }
- return std::sqrt(a * b);
- }
- #ifndef EXC_MEAN_H_
- #define EXC_MEAN_H_
- #include
- #include
- #include
- using namespace std;
-
- class mean : public logic_error
- {
- private:
- double v1;
- double v2;
-
- public:
- mean(const string &s = "none", double a = 0.0, double b = 0.0) : logic_error(s), v1(a), v2(b) {}
- const double &val1() const { return v1; }
- const double &val2() const { return v2; }
- };
-
- class bad_hmean : public mean
- {
- public:
- bad_hmean(const string &s = "none", double a = 0.0, double b = 0.0) : mean(s, a, b) {}
- void mesg() const;
- };
-
- inline void bad_hmean::mesg() const
- {
- cout << "hmean(" << val1() << ", " << val2() << "):" << endl;
- cout << "hmean() arguments should be a != b" << endl;
- }
-
- class bad_gmean : public mean
- {
- public:
- bad_gmean(const string &s = "none", double a = 0.0, double b = 0.0) : mean(s, a, b) {}
- void mesg() const;
- };
-
- inline void bad_gmean::mesg() const
- {
- cout << "gmean(" << val1() << ", " << val2() << "):" << endl;
- cout << "gmean() arguments should be >= 0" << endl;
- }
-
- #endif
4.程序清单 15.16 在每个 ty 后面都使用两个 catch 块,以确保 nbad index 异常导致方法 abel_val()被调用。请修改该程序在每个 ty 块后面只使用一个 catch 块并使用 RTTI 来确保合适时调用 abel_val()
- #include
- #include "sales.h"
-
- int main()
- {
- using std::cin;
- using std::cout;
- 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();
- LabeledSales::nbad_index *ni;
- if (ni = dynamic_cast
(&bad)) //RTTI识别类型; - {
- cout << "Company: " << ni->label_val() << endl;
- cout << "bad index: " << ni->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();
- LabeledSales::nbad_index *ni;
- if (ni = dynamic_cast
(&bad)) //RTTI识别类型; - {
- cout << "Company: " << ni->label_val() << endl;
- cout << "bad index: " << ni->bi_val() << endl;
- }
- else
- {
- cout << "bad index: " << bad.bi_val() << endl;
- }
- }
- cout << "done\n";
-
- return 0;
- }