题目:
下列程序中每个try后面都使用两个catch块,以确保nbad_index异常导致方法label_val()被调用。请修改该程序,在每个try块后面只使用一个catch块,并使用RTTI来确保合适时调用label_val()。
test.h
#ifndef QUEUE_H_
#define QUEUE_H_
#include <iostream>
using namespace std;
class Sales
{
public:
enum { MONTHS = 12};
class bad_index : public std::logic_error
{
private:
int bi;
public:
explicit bad_index(int ix, const std::string& s = "Index error in Sales object\n");
int bi_val() const { return bi; }
virtual ~bad_index() 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);
private:
double gross[MONTHS];
int year;
};
class LabeledSales : public Sales
{
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);
private:
string label;
};
#endif
test_function.cpp
#include "test.h"
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)
gross[i] = 0;
}
double Sales::operator[](int i) const
{
if (i < 0 || i >= MONTHS)
throw bad_index(i);
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);
return Sales::operator[](i);
}
test.cpp
#include <iostream>
#include "test.h"
int main()
{
double vals1[12] =
{
1220,1100,1122,2212,1232,2334,
2883,2345,2343,2312,1243,5455
};
double vals2[12] =
{
12,11,22,21,32,43,
12,23,54,32,12,11
};
Sales sales1(2011, vals2, 12);
LabeledSales sales2("Blogstar", 2012, 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 blok 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;
}
源代码:
这里只需要修改test.cpp
test.cpp
#include <iostream>
#include "test.h"
int main()
{
double vals1[12] =
{
1220,1100,1122,2212,1232,2334,
2883,2345,2343,2312,1243,5455
};
double vals2[12] =
{
12,11,22,21,32,43,
12,23,54,32,12,11
};
Sales sales1(2011, vals2, 12);
LabeledSales sales2("Blogstar", 2012, 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 (Sales::bad_index& bad)
{
if (typeid(LabeledSales::nbad_index) == typeid(bad))
{
LabeledSales::nbad_index* p = dynamic_cast<LabeledSales::nbad_index*>(&bad);
cout << p->what();
cout << "Company: " << p->label_val() << endl;
cout << "bad index: " << p->bi_val() << endl;
}
else if (typeid(Sales::bad_index) == typeid(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 blok 2\n";
}
catch (Sales::bad_index& bad)
{
if (typeid(LabeledSales::nbad_index) == typeid(bad))
{
LabeledSales::nbad_index* p = dynamic_cast<LabeledSales::nbad_index*>(&bad);
cout << p->what();
cout << "Company: " << p->label_val() << endl;
cout << "bad index: " << p->bi_val() << endl;
}
else if (typeid(Sales::bad_index) == typeid(bad))
{
cout << bad.what();
cout << "bad index: " << bad.bi_val() << endl;
}
}
cout << "done\n";
return 0;
}
演示效果:
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈
标签:index,12,const,int,double,练习,Sales,C++,88 From: https://blog.csdn.net/little_startoo/article/details/143672920