注意:由于在Tv类中有函数state_switch()动用Remote类对象来访问成员,所以要有个提前声明给Remote类:
class Remote
不然就会在编译中出现关于函数state_switch没声明之类的错误。
#include
class Remote;
class Tv
{
friend class Remote;
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 ^= 1; }
bool ison() const { return state == On; }
bool volup();
bool voldown();
void chanup();
void chandown();
void state_switch(Remote &r);
void set_mode() { mode = (mode == Antenna) ? Cable : Antenna; }
void set_input() { input = (input == TV) ? DVD : TV; }
void settings() const;
void buzz();
private:
int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
};
class Remote
{
friend class Tv;
private:
int mode;
int re_mode;
public:
enum
{
Routine,
Interaction
};
Remote(int m = Tv::TV) : mode(m), re_mode(Routine) {}
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() const { std::cout << (re_mode == 0 ? "Routine" : "Interaction") << std::endl; }
void alter() { re_mode = (re_mode == Routine) ? Interaction : Routine; }
};
inline void Tv::state_switch(Remote &r)
{
if (r.re_mode == Remote::Interaction)
buzz();
else
std::cout << "Routine mode.\n";
}
inline void Tv::buzz()
{
std::cout << "Interaction Part." << std::endl;
std::cout << "\a";
}
方法声明
#include "tv.h"
//Tv methods
bool Tv::volup()
{
if (volume < MaxVal)
{
volume++;
return true;
}
else
return false;
}
bool Tv::voldown()
{
if (volume > 0)
{
volume--;
return true;
}
else
return false;
}
void Tv::chanup()
{
if (channel < maxchannel)
channel++;
else
channel = 1;
}
void Tv::chandown()
{
if (channel > 0)
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;
}
}
测试程序
#include
#include "tv.h"
int main()
{
using std::cout;
Tv s42;
cout << "Inital 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);
cout << "\n42\" settings after using remote:\n";
s42.settings();
cout << "\n";
grey.alter();
grey.show();
s42.state_switch(grey);
system("pause");
return 0;
}
测试程序是书本中的,稍微加了新增方法的演示。对于互动我也不知道怎么互动了,干脆给一个输出和一个响铃来作为提示“互动模式”了,注意:响铃真的很小声,我带了耳机才听到。
声明头文件
#include
#include
#include
class bad_hmean : public std::logic_error
{
public:
double v1;
double v2;
bad_hmean(double a = 0, double b = 0, const std::string &s = "hmean() argument should not bo negative to each other.\n") : std::logic_error(s), v1(a), v2(b){};
};
class bad_gmean : public std::logic_error
{
public:
double v1;
double v2;
bad_gmean(double a = 0, double b = 0, const std::string &s = "gmean() argument should be >=0.\n") : std::logic_error(s), v1(a), v2(b){};
};
测试程序
#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
<< " is " << z << endl;
cout << "Geometric mean of " << x << " and " << y
<< " is " << gmean(x, y) << endl;
cout << "Enter next set of numbers :"
;
}
catch (bad_hmean &h)
{
cout << h.what();
cout << "Values used: " << h.v1 << ", " << h.v2 << endl;
cout << "Try again.\n";
continue;
}
catch (bad_gmean &g)
{
cout << g.what();
cout << "Values used: " << g.v1 << ", " << g.v2 << endl;
cout << "Sorry,you don't get to play any more." << endl;
break;
}
}
cout << "Bye." << endl;
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);
}
原来的例子是声明定义类成员函数来输出提示信息,把异常类logic_error以基类加进来后就使用其自带的what函数来返回字符串提示,在程序catch语块中输出就行。
#include
#include
#include
class badbad : public std::logic_error
{
public:
double v1;
double v2;
badbad(const std::string &s, double a = 0, double b = 0) : std::logic_error(s), v1(a), v2(b){};
};
#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
<< " is " << z << endl;
cout << "Enter next set of numbers :"
;
}
catch (badbad &bb)
{
cout << bb.what();
cout << "Values used: " << bb.v1 << ", " << bb.v2 << endl;
cout << "Sorry,byebye." << endl;
break;
}
}
cout << "Enter two numbers: ";
while (cin >> x >> y)
{
try
{
z = gmean(x, y);
cout << "Geometric mean of " << x << " and " << y
<< " is " << z << endl;
cout << "Enter next set of numbers :"
;
}
catch (badbad &bb)
{
cout << bb.what();
cout << "Values used: " << bb.v1 << ", " << bb.v2 << endl;
cout << "Sorry,you don't get to play any more." << endl;
break;
}
}
cout << "Bye." << endl;
system("pause");
return 0;
}
double hmean(double a, double b)
{
if (a == -b)
throw badbad("Invalid argument for hmean().\n", a, b);
return 2.0 * a * b / (a + b);
}
double gmean(double a, double b)
{
if (a < 0 || b < 0)
throw badbad("Invalid argument for gmean().\n", a, b);
return std::sqrt(a * b);
}
#include
#include
#include
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")
: std::logic_error(s), bi(ix){};
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")
: Sales::bad_index(ix, s), lbl(lb){};
const std::string &label_val() const { return lbl; }
virtual ~nbad_index() throw() {}
};
explicit LabeledSales(const std::string &lb = "none", int yy = 0) : Sales(yy), label(lb){};
LabeledSales(const std::string &lb, int yy, const double *gr, int n) : Sales(yy, gr, n), label(lb){};
virtual ~LabeledSales() {}
const std::string &Label() const { return label; }
virtual double operator[](int i) const;
virtual double &operator[](int i);
private:
std::string label;
};
方法声明
#include "sales.h"
//Sales methods
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 methods
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);
}
测试程序
#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, 2544};
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);
Sales::bad_index *s1;
LabeledSales::nbad_index *s2;
cout << "First try block:\n";
try
{
int i;
cout << "Year " << sales1.Year() << endl;
for (int 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 (int i = 0; i <= 12; i++)
{
cout << sales2[i] << ' ';
if (i % 6 == 5)
cout << endl;
}
cout << "End of block 1.\n";
}
catch (std::logic_error &b)
{
cout << b.what();
if (s2 = dynamic_cast<LabeledSales::nbad_index *>(&b))
{
cout << "Company: " << s2->label_val() << endl;
cout << "Bad index: " << s2->bi_val() << endl;
}
else if (s1 = dynamic_cast<Sales::bad_index *>(&b))
cout << "Bad index: " << s1->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 &b)
{
cout << b.what();
if (s2 = dynamic_cast<LabeledSales::nbad_index *>(&b))
{
cout << "Company: " << s2->label_val() << endl;
cout << "Bad index: " << s2->bi_val() << endl;
}
else if (s1 = dynamic_cast<Sales::bad_index *>(&b))
cout << "Bad index: " << s1->bi_val() << endl;
}
cout << "Done." << endl;
system("pause");
return 0;
}