异常 类和继承

/////////////////////////////////////////
//
//sales.h
//
/////////////////////////////////////////
// sales.h  -- exceptions and inheritance
#include <stdexcept>
#include <cstring>

class Sales
{
public:
 enum {MONTHS = 12};   // could be a static const
 class bad_index : public std::logic_error
 {
 private:
  int bi;  // bad index value
 public:
  explicit bad_index(int ix,
   const char * s = "Index error in Sales object\n");
  int bi_val() const {return bi;}
 };
 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 throw(std::logic_error);
 virtual double & operator[](int i) throw(std::logic_error);
private:
 double gross[MONTHS];
 int year;
};

class LabeledSales : public Sales
{
public:
 static const int STRLEN = 50;  // could be an enum
 class nbad_index : public Sales::bad_index
 {
 private:
  char lbl[STRLEN];
 public:
  nbad_index(const char * lb, int ix,
   const char * s = "Index error in LabeledSales object\n");
  const char * label_val() {return lbl;}
 };
 explicit LabeledSales(const char * lb = "none", int yy = 0);
 LabeledSales(const char * lb, int yy, const double * gr, int n);
 virtual ~LabeledSales() { }
 const char * Label() const {return label;}
 virtual double operator[](int i) const throw(std::logic_error);
 virtual double & operator[](int i) throw(std::logic_error);
private:
 char label[STRLEN];
};

 

 

 

//////////////////////////////////////////////////////
//
//sales.cpp
//
/////////////////////////////////////////////////////
// sales.cpp -- Sales implementation
#include "sales.h"

Sales::bad_index::bad_index(int ix, const char * s )
: std::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 > n and i < MONTHS
 for ( ; i < MONTHS; ++i)
  gross[i] = 0;
}

double Sales::operator[](int i) const throw(std::logic_error)
{
 if(i < 0 || i >= MONTHS)
  throw bad_index(i);
 return gross[i];
}

double & Sales::operator[](int i) throw(std::logic_error)
{
 if(i < 0 || i >= MONTHS)
  throw bad_index(i);
 return gross[i];
}

LabeledSales::nbad_index::nbad_index(const char * lb, int ix,
          const char * s ) : Sales::bad_index(ix, s)
{
 std::strcpy(lbl, lb);
}

LabeledSales::LabeledSales(const char * lb, int yy)
: Sales(yy)
{
 std::strcpy(label, lb);
}

LabeledSales::LabeledSales(const char * lb, int yy, const double * gr, int n)
: Sales(yy, gr, n)
{
 std::strcpy(label, lb);
}

double LabeledSales::operator[](int i) const throw(std::logic_error)
{
 if(i < 0 || i >= MONTHS)
  throw nbad_index(Label(), i);
 return Sales::operator[](i);
}

double & LabeledSales::operator[](int i) throw(std::logic_error)
{
 if(i < 0 || i >= MONTHS)
  throw nbad_index(Label(), i);
 return Sales::operator[](i);
}

 

 

///////////////////////////////////////////////////////
//
//use_sales.cpp
//
//////////////////////////////////////////////////////
// use_sales.cpp  -- nested exceptions
#include <iostream>
#include "sales.h"

int main()
{
 using std::cout;
 using std::cin;
 using std::endl;

 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(2004, vals1, 12);
 LabeledSales sales2("Blogstar",2005, vals2, 12 );

 cout << "First try block:\n";
 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(LabeledSales::nbad_index & bad)
 {
  cout << bad.what();
  cout << "Company: " << bad.label_val() << endl;
  cout << "bad index: " << bad.bi_val() << endl;
 }
 catch(Sales::bad_index & bad)
 {
  cout << bad.what();
  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(LabeledSales::nbad_index & bad)
 {
  cout << bad.what();
  cout << "Company: " << bad.label_val() << endl;
  cout << "bad index: " << bad.bi_val() << endl;
 }
 catch(Sales::bad_index & bad)
 {
  cout << bad.what();
  cout << "bad index: " << bad.bi_val() << endl;
 }
 cout << "done\n";

 return 0;
}

 

posted @ 2007-03-11 00:50  Edward Xie  阅读(183)  评论(0)    收藏  举报