【C++ Primer Plus习题】15.4

大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←

问题:








解答:

main.cpp

cpp 复制代码
#include <iostream>
#include "sales.h"
using namespace std;

int main()
{
	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:\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" << endl;
	}
	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:" << endl;
	try
	{
		sales2[2] = 37.5;
		sales1[20] = 23345;
		cout << "End of try block 2." << endl;
	}
	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;
}

sales.h

cpp 复制代码
#pragma once
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;

class Sales
{
public:
	enum{MONTHS=12};
	class bad_index :public logic_error
	{
	private:
		int bi;
	public:
		explicit bad_index( int ix,const string & s = "Index error int 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 objecrt\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;
	};

sales.cpp

cpp 复制代码
#include "sales.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);
}

运行结果:

考查点:

  • 异常
  • 继承
  • []运算符重载
  • dynamic_cast

2024年9月12日21:33:42

相关推荐
南宫生26 分钟前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
高山我梦口香糖42 分钟前
[react]searchParams转普通对象
开发语言·前端·javascript
sanguine__42 分钟前
Web APIs学习 (操作DOM BOM)
学习
冷眼看人间恩怨1 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
信号处理学渣1 小时前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客1 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
Lenyiin1 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin
jasmine s1 小时前
Pandas
开发语言·python
biomooc2 小时前
R 语言 | 绘图的文字格式(绘制上标、下标、斜体、文字标注等)
开发语言·r语言
骇客野人2 小时前
【JAVA】JAVA接口公共返回体ResponseData封装
java·开发语言