1.对Tv和Remote类做如下修改:
a.让它们互为友元;
b.在Remote类中添加一个状态变量成员,该成员描述遥控器是处于常规模式还是互动模式;
c.在Remote中添加一个显示模式的方法;
d.在Tv类中添加一个对Remote中新成员进行切换的方法,该方法应仅当Tv处于打开状态时才能进行。
编写一个小程序来测试这些特性。
实现:
tv.h
#pragma once
#include <iostream>
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_remote_mode(Remote& r);
private:
int state;//on or off
int volume;//
int maxchannel;
int channel;//current channel
int mode;//broadcast or cable
int input;//TV or DVD
};
class Remote
{
private:
enum{Normal, Interactive};
int mode;//controls TV or DVD
int remote_mode;//常规模式还是互动模式
public:
friend class Tv;
Remote(int m = Tv::TV, int rm = Normal) : mode(m), remote_mode(rm) {}
bool volup(Tv& t) { return t.volup(); }
bool voldowm(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(); }
int show_remote_mode()const { return remote_mode; }
void set_remote_mode();
};
tv.cpp
#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::set_remote_mode(Remote& r)
{
if (state == On)
{
r.set_remote_mode();
}
}
void Remote::set_remote_mode()
{
remote_mode = (remote_mode == Normal) ? Interactive : Normal;
}
2.修改程序清单15.11,使两种异常类型都是从头文件<stdexcept>
提供的logic_error类派生出来的类。让每个what()方法都报告函数名和问题的性质。异常对象不用存储错误的参数值,而只需支持what()方法。
实现:
main.cpp
#include <iostream>
#include <stdlib>
#include <stdexcept>
#include <cmath>
using std::logic_error;//
using std::cin;
using std::cout;
using std::endl;
double hmean(double a, double b);
double gmean(double a, double b);
class bad_hmean : public logic_error
{
public:
bad_hmean(const string& what_arg = "hmean, invalid arguments") : logic_error(what_arg){}
};
class bad_gmean : public logic_error
{
public:
bad_gmean(const string& what_arg = "gmean, invalid arguments") : logic_error(what_arg) {}
};
int main(void)
{
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 << "Geometric mean of " << x << " and " << y << " is " << z << endl;
}
catch (bad_hmean& bh)
{
cout << bh.what();
cout << "Enter a new pair of arguments:";
continue;
}
catch (bad_gmean& bg)
{
cout << bg.what();
//cout << "Value used:" << bg.v1 << ", " << bg.v2 << endl;
cout << "Sorry, quit now!" << endl;
break;
}
cout << "Enter next set of numbers:";
}
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 sqrt(a * b);
}
3.这个练习于编程练习2相同,但异常类是一个这样的基类派生而来的:它是从logic_error派生而来的,并存储两个参数值。异常类应该有一个这样的方法:报告这些值以及函数名。程序使用一个catch块来捕获基类异常,其中任何一种从该基类异常派生而来的异常都将导致循环结束。
实现:
main.cpp
#include <iostream>
#include <stdlib>
#include <stdexcept>
#include <cmath>
using std::logic_error;//
using std::cin;
using std::cout;
using std::endl;
double hmean(double a, double b);
double gmean(double a, double b);
class bad_mean : public logic_error
{
private:
double v1;
double v2;
public:
bad_mean(double a = 0, double b = 0, const string& what_arg = "mean, invalid arguments") : v1(a), v2(b), logic_error(what_arg) {}
virtual void show()const;
};
class bad_hmean : public bad_mean
{
public:
bad_hmean(double a = 0, double b = 0, const string& what_arg = "hmean, invalid arguments"):bad_mean(a,b,what_arg){}
void show()const;
};
class bad_gmean : public bad_mean
{
public:
bad_gmean(double a = 0, double b = 0, const string& what_arg = "gmean, invalid arguments") :bad_mean(a, b, what_arg) {}
void show()const;
};
void bad_mean::show() const
{
cout << "v1 = " << v1 << ", v2 = " << v2 << endl;
}
void bad_hmean::show() const
{
cout << what() << endl;
bad_mean::show();
}
void bad_gmean::show() const
{
cout << what() << endl;
bad_mean::show();
}
int main(void)
{
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 << "Geometric mean of " << x << " and " << y << " is " << z << endl;
}
catch(bad_mean &b)
{
if (typeid(bad_hmean) == typeid(b))
{
b.show();
break;
}
if (typeid(bad_gmean) == typeid(b))
{
b.show();
break;
}
}
cout << "Enter next set of numbers:";
}
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 sqrt(a * b);
}
4.程序清单15.16在每个try后面都使用两个catch块,以确保nbad_index异常导致方法label_val()被调用。请修改该程序,在每个try块后面只使用一个catch块,并使用RTTI来确保合适时调用label_val()。
实现:
main.cpp
#include <iostream>
#include "sales.h"
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);
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)
{
if (typeid(LabeledSales::nbad_index) == typeid(bad))
{
LabeledSales::nbad_index* p = dynamic_cast<LabeledSales::nbad_index*>(&bad);
cout << bad.what();
cout << "Company: " << p->label_val() << endl;
cout << "bad index: " << bad.bi_val() << endl;
}
if (typeid(Sales::bad_index) == typeid(bad))
{
cout << bad.what();
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)
{
if (typeid(LabeledSales::nbad_index) == typeid(bad))
{
LabeledSales::nbad_index* p = dynamic_cast<LabeledSales::nbad_index*>(&bad);
cout << bad.what();
cout << "Company: " << p->label_val() << endl;
cout << "bad index: " << bad.bi_val() << endl;
}
if (typeid(Sales::bad_index) == typeid(bad))
{
cout << bad.what();
cout << "bad index: " << bad.bi_val() << endl;
}
}
cout << "done\n";
return 0;
}