#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class Sales
{
public:
explicit Sales(int yy);
Sales(int yy,const double *gr,int n);
int Year(){return m_year;}
virtual double operator[](int index) const;
virtual double & operator [](int index);
enum{MONTHS =12};
class bad_index:public logic_error
{
public:
bad_index(int ix,const string& s = "Index error in Sales object\n");
int bi_val(){return bi;}
private:
int bi;
};
private:
int m_year;
double m_Gross[MONTHS];
};
class LabelSales:public Sales
{
public:
LabelSales(const string &lb = "none", int yy = 0);
LabelSales(const string &lb,int yy,double *gr,int n);
const string& Label() const{return label;}
virtual double operator[](int index) const;
virtual double& operator [](int index);
class nbad_index:public Sales::bad_index
{
public:
nbad_index(const string &lb,int ix,const string &s = "Index error in LabelSales object\n") ;
const string& Lbl_Val() const {return lbl;}
private:
string lbl;
};
private:
string label;
};
Sales::bad_index::bad_index(int ix, const string &s):bi(ix),logic_error(s){}
Sales::Sales(int yy)
{
this->m_year =yy;
for (int i = 0; i < MONTHS; ++i) {
m_Gross[i] = 0;
}
}
Sales::Sales(int yy, const double *gr, int n)
{
m_year =yy;
int lim = (n<MONTHS)?n:MONTHS;
for (int i = 0; i < lim; ++i) {
m_Gross[i] = gr[i];
}
}
double Sales::operator [](int index) const
{
if(index<0||index>=MONTHS)
throw bad_index(index);
return m_Gross[index];
}
//double & Sales::operator [](int index)
//{
// if(index<0 || index >=MONTHS)
// throw bad_index(index);
// return m_Gross[index];
//}
LabelSales::nbad_index::nbad_index(const string &lb, int ix, const string &s)
:Sales::bad_index(ix,s)
{
lbl = lb;
}
LabelSales::LabelSales(const string &lb, int yy):Sales(yy)
{
label = lb;
}
LabelSales::LabelSales(const string &lb, int yy, double *gr, int n):Sales(yy,gr,n)
{
label = lb;
}
double LabelSales::operator[](int index) const
{
if(index<0||index>=MONTHS)
{
throw nbad_index(Label(),index);
}
return Sales::operator [] (index);
}
//double& LabelSales::operator[](int index)
//{
// if(index<0||index>=MONTHS)
// {
// throw nbad_index(Label(),index);
// }
// return Sales::operator [] (index);
//}
void test()
{
double val1[12] =
{
1000,1001,1002,1003,1004,1005,
1006,1007,1008,1009,1010,1011
};
double val2[12] =
{
10,11,12,13,14,15,
16,17,18,19,20,21
};
Sales sales1(2011,val1,12);
LabelSales sales2("BlogStar",2012,val2,12);
cout<<"First try block:\n";
try
{
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 try block 1.\n";
}
catch(Sales::bad_index &bad)
{
cout<<bad.what ();
cout<<"bad index:"<<bad.bi_val ()<<endl;
}
catch(LabelSales::nbad_index &bad)
{
cout<<bad.what ();
cout<<"Company: "<<bad.Lbl_Val ()<<endl;
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 ();
cout<<"bad index:"<<bad.bi_val ()<<endl;
}
catch(LabelSales::nbad_index &bad)
{
cout<<bad.what ();
cout<<"Company:"<<bad.Lbl_Val ()<<endl;
cout<<"bad index:"<<bad.bi_val ()<<endl;
}
cout <<"done\n";
return ;
}
int main()
{
test ();
}
异常,继承,类结合
最新推荐文章于 2024-08-26 17:31:44 发布