【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

相关推荐
AI街潜水的八角5 分钟前
基于C++的决策树C4.5机器学习算法(不调包)
c++·算法·决策树·机器学习
q5673152322 分钟前
在 Bash 中获取 Python 模块变量列
开发语言·python·bash
阿伟来咯~32 分钟前
记录学习react的一些内容
javascript·学习·react.js
JSU_曾是此间年少34 分钟前
数据结构——线性表与链表
数据结构·c++·算法
许野平1 小时前
Rust: 利用 chrono 库实现日期和字符串互相转换
开发语言·后端·rust·字符串·转换·日期·chrono
也无晴也无风雨1 小时前
在JS中, 0 == [0] 吗
开发语言·javascript
Suckerbin1 小时前
Hms?: 1渗透测试
学习·安全·网络安全
狂奔solar1 小时前
yelp数据集上识别潜在的热门商家
开发语言·python
水豚AI课代表1 小时前
分析报告、调研报告、工作方案等的提示词
大数据·人工智能·学习·chatgpt·aigc
聪明的墨菲特i1 小时前
Python爬虫学习
爬虫·python·学习