【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

相关推荐
PandaCave3 分钟前
vue工程运行、构建、引用环境参数学习记录
javascript·vue.js·学习
萝卜兽编程7 分钟前
优先级队列
c++·算法
yuwinter8 分钟前
鸿蒙HarmonyOS学习笔记(2)
笔记·学习·harmonyos
Bruce小鬼8 分钟前
QT文件基本操作
开发语言·qt
2202_7544215414 分钟前
生成MPSOC以及ZYNQ的启动文件BOOT.BIN的小软件
java·linux·开发语言
我只会发热21 分钟前
Java SE 与 Java EE:基础与进阶的探索之旅
java·开发语言·java-ee
red_redemption30 分钟前
自由学习记录(23)
学习·unity·lua·ab包
懷淰メ30 分钟前
PyQt飞机大战游戏(附下载地址)
开发语言·python·qt·游戏·pyqt·游戏开发·pyqt5
hummhumm44 分钟前
第 22 章 - Go语言 测试与基准测试
java·大数据·开发语言·前端·python·golang·log4j
宁静@星空1 小时前
006-自定义枚举注解
java·开发语言