异常,继承,类结合

#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 ();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值