1.tv.h:
#pragma once
#include
using namespace std;
class Tv
{
public:
friend class Remote;
enum { Off,On };
enum{MinVal,MaxVal = 20};
enum{Antenna,Cable};
enum{TV,VCR};
Tv(int s = Off,int mc = 100):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) ? VCR : TV; }
void settings() const;
void set_remote_mode(Remote& r);
private:
int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
};
class Remote
{
public:
friend class Tv;
enum{USUAL,EXCHANGE};
Remote(int m = Tv::TV,int rm = USUAL):mode(m),remote_mode(rm){}
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 showMode() { cout<< "Remote mode is "<
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 {
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" : "VCR") << endl;
}
}
void Tv::set_remote_mode(Remote& r) {
if (ison()) {
r.remote_mode = Remote::EXCHANGE;
r.showMode();
}
}
main:
#include
#include "Tv.h"
using namespace std;
int main()
{
Tv tv;
Remote rm;
rm.showMode();
cout << "Initial settings for TV:\n";
tv.settings();
tv.onoff();
tv.chanup();
cout << "Adjusted settings for TV:\n";
tv.settings();
tv.set_remote_mode(rm);
rm.set_chan(tv, 12);
rm.volup(tv);
rm.volup(tv);
cout << "settings after using remote:\n";
tv.settings();
return 0;
}
2. exc_mean.h:
#pragma once
#include
#include
using namespace std;
class bad_hmean : public logic_error
{
public:
explicit bad_hmean(const string& n = "hmean",const string& s="Error in hmean()\n") :name(n), logic_error(s) {}
string mesg() { return "hmean() invalid arguments: a = -b\n"; }
virtual ~bad_hmean()throw(){}
private:
string name;
};
class bad_gmean : public logic_error
{
public:
explicit bad_gmean(const string& n = "gmean", const string& s = "Error in gmean()\n") :name(n), logic_error(s) {}
string mesg() { return "gmean() arguments should be >=0\n"; }
virtual ~bad_gmean()throw() {}
private:
string name;
};
main:
#include
#include
#include "exc_mean.h"
using namespace std;
double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
double x, y, z;
cout << "Enter two numbers:";
while (cin>>x>>y)
{
try{
z = hmean(x, y);
cout << "Harmoni mean of " << x << " and " << y << " is " << z << endl;
cout << "Geometric mean of " << x << " and " << y << " is " << gmean(x,y) << endl;
}
catch(bad_hmean& bg){
cout << bg.what();
cout << "Error message:\n" << bg.mesg() << endl;
cout << "Try again.\n";
cout << "Enter two numbers:";
continue;
}
catch(bad_gmean& hg){
cout << hg.what();
cout << "Error message:\n" << hg.mesg() << endl;
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);
}
3.exc_mean.h:
#pragma once
#include
#include
#include
using namespace std;
class exc :public logic_error
{
public:
explicit exc(double a,double b,const string& n = "exc", const string& s = "Error in exc()\n") :v1(a),v2(b),name(n), logic_error(s) {}
virtual string mesg() = 0;
virtual ~exc()throw() {}
string name;
double v1;
double v2;
};
class bad_hmean : public exc
{
public:
explicit bad_hmean(double a, double b, const string& n = "hmean", const string& s = "Error in hmean()\n") :exc(a,b,n,s) {}
string mesg() { return "hmean() invalid arguments: "+ to_string(v1) +" = -("+ to_string(v2) +")\n";}
virtual ~bad_hmean()throw() {}
private:
string name;
};
class bad_gmean : public exc
{
public:
explicit bad_gmean(double a, double b, const string& n = "gmean", const string& s = "Error in gmean()\n") :exc(a, b, n, s) {}
string mesg() {
double v = v2;
if (v1 < 0)
v = v1;
return "gmean() arguments "+to_string(v)+" should be >=0\n";
}
virtual ~bad_gmean()throw() {}
};
main:
#include
#include
#include "exc_mean.h"
using namespace std;
double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
double x, y, z;
cout << "Enter two numbers:";
while (cin >> x >> y)
{
try {
z = hmean(x, y);
cout << "Harmoni mean of " << x << " and " << y << " is " << z << endl;
cout << "Geometric mean of " << x << " and " << y << " is " << gmean(x, y) << endl;
}
catch (bad_hmean& bg) {
cout << bg.what();
cout << "Error message:\n" << bg.mesg() << endl;
cout << "Sorry,you don't get to play any more.\n";
break;
}
catch (bad_gmean& hg) {
cout << hg.what();
cout << "Error message:\n" << hg.mesg() << endl;
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(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);
}
4. sales.h:
#pragma once
#include
#include
using namespace std;
class sales
{
public:
enum { MONTHS = 12 };
class bad_index :public logic_error {
public:
explicit bad_index(int ix, const char* s = "Index error in Sales object\n"):logic_error(s),bi(ix){}
int bi_val() { return bi; }
private:
int bi;
};
explicit sales(int yy = 0);
sales(int yy, const double* gr, int n);
int Year()const { return year; }
virtual double operator[](int i)const throw(logic_error);
virtual double & operator[](int i)throw(logic_error);
private:
int year;
double gross[MONTHS];
};
class LabeldSales:public sales
{
public:
static const int STRLEN = 50;
class nbad_index :public sales::bad_index
{
public:
nbad_index(const char* lb, int ix, const char* s = "Index error in LabeldSales object\n");
const char* label_val() { return lbl; }
private:
char lbl[STRLEN];
};
explicit LabeldSales(const char* lb = "none", int yy = 0);
LabeldSales(const char* lb, int yy, const double* gr, int n);
virtual ~LabeldSales(){}
const char* Label()const { return label; }
virtual double operator[](int i)const throw(logic_error);
virtual double & operator[](int i)throw(logic_error);
private:
char label[STRLEN];
};
sales.cpp:
#include "sales.h"
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 size = (n< MONTHS)?n:MONTHS;
int i;
for(i = 0; i < size; ++i)
gross[i] = gr[i];
for (; i < MONTHS; ++i)
gross[i] = 0;
}
double sales::operator[](int i)const throw(logic_error) {
if (i < 0 || i >= MONTHS)
throw bad_index(i);
return gross[i];
}
double & sales::operator[](int i)throw(logic_error) {
if (i < 0 || i >= MONTHS)
throw bad_index(i);
return gross[i];
}
LabeldSales::nbad_index::nbad_index(const char* lb, int ix, const char* s) :sales::bad_index(ix, s) { strcpy_s(lbl, STRLEN, lb);}
LabeldSales::LabeldSales(const char* lb, int yy) : sales(yy) {strcpy_s(label, STRLEN, lb);}
LabeldSales::LabeldSales(const char* lb, int yy, const double* gr, int n):sales(yy,gr,n) {strcpy_s(label, STRLEN, lb);}
double LabeldSales::operator[](int i)const throw(logic_error) {
if (i < 0 || i >= MONTHS)
throw nbad_index(label,i);
return sales::operator[](i);
}
double & LabeldSales::operator[](int i)throw(logic_error) {
if (i < 0 || i >= MONTHS)
throw nbad_index(label,i);
return sales::operator[](i);
}
main:
#include
#include "sales.h"
using namespace std;
int main()
{
double vals1[12] = { 1256,8563,2644,6542,3556,3256,
6656,8456,3654,3665,3562,4335 };
double vals2[12] = { 25,36,98,25,32,35,
28,63,46,86,34,52 };
sales sales1(2004, vals1, 12);
LabeldSales sales2("Blogstar", 2005, vals2, 12);
cout << "Firat try block:\n";
LabeldSales::nbad_index* nb;
sales::bad_index* b;
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 (logic_error& bad) {
cout << bad.what();
if (nb = dynamic_cast(&bad)) {
cout << "Company: " << nb->label_val() << endl;
cout << "bad index: " << nb->bi_val() << endl;
}else if(b = dynamic_cast(&bad))
cout << "bad index: " << b->bi_val() << endl;
}
cout << "\nNext try block:\n";
try {
sales2[2] = 37.5;
sales1[20] = 2335;
cout << "End of try block 2.\n";
}
catch (logic_error& bad) {
cout << bad.what();
if (nb = dynamic_cast(&bad)) {
cout << "Company: " << nb->label_val() << endl;
cout << "bad index: " << nb->bi_val() << endl;
}
else if (b = dynamic_cast(&bad))
cout << "bad index: " << b->bi_val() << endl;
}
cout << "done\n";
return 0;
}