C++ primer plus第六版课后编程题答案15.4


sales.h

#ifndef STALES_h_
#define STALES_h_
#include <stdexcept>
#include <string>
using namespace std;
class Sales{
public:
	enum{MONTHS=12};
private:
	double gross[MONTHS];
	int year;
public:
	class bad_index:public logic_error
	{
	private:
		int bi;
	public:
		explicit bad_index(int ix,const string &s="Index error in Sales object\n");
		int bi_val()const {return bi;};
		virtual ~bad_index()throw(){};//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);

};
class LabeledSales:public Sales
{
private:
	string label;
public:
	class nbad_index:public Sales::bad_index
	{
	private:
		string lbl;
	public:
		nbad_index(const string &lb,int ix,const string &s="Index error in LabeledSales object\n");
		const string &label_val()const{return lbl;};
		virtual ~nbad_index()throw(){};
	};
	explicit LabeledSales(const string &lb="none",int yy=0);
	LabeledSales(const string &lb,int yy,const double *gr,int n);
	virtual ~LabeledSales(){};
	const string &Label()const {return label;};
	virtual double operator[](int i)const;
	virtual double &operator[](int i);

};
#endif

sales.cpp

#include "sales.h"
#include <iostream>
using namespace std;
Sales::bad_index::bad_index(int ix,const string &s):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++)//考虑n<months 
			gross[i]=0;
}
double Sales::operator[](int i)const
{
	if(i<0||i>=MONTHS)
		throw (new bad_index(i));//这样子才是抛出指针
		//throw *bad_index(i);//不能throw一个指针类型?
	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 string &lb,int ix,const string &s):Sales::bad_index(ix,s)
{
	lbl=lb;
}
LabeledSales::LabeledSales(const string &lb,int yy):Sales(yy)
{
	label=lb;
}
LabeledSales::LabeledSales(const 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);
		throw (new nbad_index(Label(),i));
	return Sales::operator[](i);
}


main154.cpp

#include <iostream>
#include "sales.h"
void main154()
{
	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:"<<endl;
	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."<<endl;
	}
	catch(Sales::bad_index *bad)//用指针
	{
		
		LabeledSales::nbad_index *p;
		if(p=dynamic_cast<LabeledSales::nbad_index*>(bad))
		{
			cout<<p->what();
			cout<<"Company:"<<p->label_val()<<endl;
			cout<<"bad index:"<<p->bi_val()<<endl;
			//p->label_val();
		}
		else
		{
			cout<<bad->what();
			cout<<"bad index:"<<bad->bi_val()<<endl;
			//	cout<<"bad index:"<<bad.bi_val()<<endl;
		}
	}
	try{
		sales2[12]=37.5;
		sales1[20]=23345;
		cout<<"Enter of try block 2"<<endl;
	}
	catch(Sales::bad_index *bad)//用指针
	{
		LabeledSales::nbad_index *p;
		if(p=dynamic_cast<LabeledSales::nbad_index*>(bad))
		{
			cout<<p->what();
			cout<<"Company:"<<p->label_val()<<endl;
			cout<<"bad index:"<<p->bi_val()<<endl;
			//p->label_val();
		}
		else
		{
			cout<<bad->what();
			cout<<"bad index:"<<bad->bi_val()<<endl;
		}
	}



}

 

 


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注