首页 > 编程语言 >C++-练习-88

C++-练习-88

时间:2024-11-17 20:14:39浏览次数:3  
标签:index 12 const int double 练习 Sales C++ 88

题目:

下列程序中每个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

相关文章

  • C++时间复杂度讲解
    它约等于算法中基本操作重复执行的次数(循环或递归的次数)不是行数!!!最多为O(5)!!!用乘号连接(在嵌套循环中),时间复杂度用O()表示。(O()只是符号)如:for(int=1;i<=n*10/8;i++){      for(intj=1;j<=n*10/2;k++){             for(intk=1;k<n*10;k++)......
  • 双非本 大一的蓝桥杯c++组备赛日记----普通人的极限在哪里?
    本文创作灵感:从开学到现在,刷到许许多多的让人热血沸腾、心生向往的视频,大都是MIT精致生活,清北狂人此类的。刷多之后心中躁动,跃跃欲试,可又知自己能力有限,面对神仙般的人物只能望其项背,每日累得吐血,但成效低微,心中茫然不已。又恰逢手贱误删文件,导致重新装了一遍vs。本人之前看的......
  • C++入门基础知识152—【关于C++ 赋值运算符重载】
    成长路上不孤单......
  • C++入门基础知识151—【关于C++ ++ 和 -- 运算符重载】
    成长路上不孤单......
  • c++小游戏5个
    这里给出5个简单的C++小游戏示例:猜数字游戏:计算机生成一个随机数,玩家需要猜出这个数字是多少。提示玩家猜测的数字是高了还是低了,直到猜中为止。#include<iostream>#include<cstdlib>#include<ctime>intmain(){srand(time(0));intsecretNumber=r......
  • 实习冲刺练习 第二十三天
    每日一题回文链表.-力扣(LeetCode)classSolution{public:boolisPalindrome(ListNode*head){if(head==nullptr)returnfalse;vector<int>v;while(head!=nullptr){//将链表的值存入数组中v.push_back(head->val);......
  • c++入门基础(一)
    文章目录一、C++输入和输出C++的第⼀个程序二、命名空间namespace的价值namespace的定义命名空间使用三、C++输入&输出四、缺省参数一、C++输入和输出C++的第⼀个程序C++兼容C语⾔绝⼤多数的语法,所以C语⾔实现的helloworld依旧可以运⾏//test.cpp#include<st......
  • 10.C++面向对象7(友元,匿名对象)
    ⭐本篇重点:友元,内部类,匿名对象⭐本篇代码:c++学习/03.c++类与对象-下篇·橘子真甜/c++-learning-of-yzc-码云-开源中国(gitee.com)目录一.友元1.1友元函数 1.2友元类 二.匿名对象 一.友元友元分为友元函数和友元类,它可以帮助我们突破封装的限制。在......
  • C++--多态语法介绍
    目录多态的概念多态的定义及实现多态的构成条件实现多态还有两个必须重要条件虚函数虚函数的重写/覆盖多态场景的⼀个选择题虚函数重写的⼀些其他问题协变析构函数的重写override和final关键字重载/重写/隐藏的对比纯虚函数和抽象类多态的原理虚函数表指针多态的原......
  • 练习
    练习1.1查阅你使用的编译器的文档,确定它所使用的文件名约定。编译并运行第2页的main程序。解:g++--std=c++11ch1.cpp-omain./main练习1.2改写程序,让它返回-1。返回值-1通常被当做程序错误的标识。重新编译并运行你的程序,观察你的系统如何处理main返回的错误标识。解:......