- 熟悉异常的使用场景,理解异常对于大型软件工程的作用及意义。
- 掌握异常的处理流程。
- 熟练运用throw、try和catch处理异常。
- 掌握标准程序库异常处理。
- 理解异常类继承与派生的作用。
题目:设计一个日期类Date,包含year,month,day三个整型数据成员,实现构造函数、获取和修改年(月、日)的函数,并实现相关异常处理功能。
要求:
- 实现一个异常基类DateException,包括错误码(code,整型)和错误信息(message,字符串型)两个数据成员和getCode及getMessage两个成员方法,分别获取错误码和错误信息。
- 基于基类DateException,派生DateYearException,DateMonthException和DateDayException三个子异常类,分别表示年、月、日异常。
- 在主函数中进行测试,每个异常测试3组数据。
- 注意:年和日的异常时相关的,比如闰年2月份的天数为29天,非闰年2月为28天。
提高(选做):
- 进一步派生DateYearException(DateMonthException、DateDayException)类,分别细致处理不同类型的年(月、日)异常,例如月份小于零、大于十二等不同类型异常。
- 使用异常处理的方式改进《实验五 运算符重载》时间运算符重载的相关实现代码。
- 使用异常处理的方式改进其他实验中的相关代码。
#include <iostream>
#include <string>
using namespace std;
enum ERROR { YearErr, MonthErr, DayErr };//年月日错误码分别为0,1,2
/* 日期类 */
class Date
{
public:
Date();
Date(int Year, int Month, int Day);
void GetDate()const;
void SetDate();
~Date() {}
private:
int year, month, day;//年月日
};
//无形参的构造函数,将日期初始化为1年1月1日
Date::Date()
{
year = month = day = 1;
}
//有形参的构造函数,用参数初始化日期
Date::Date(int Year, int Month, int Day)
{
year = Year;
month = Month;
day = Day;
}
//获得日期函数 GetDate
void Date::GetDate()const//防止函数改变私有变量
{
cout << "\n" << year << "年 " << month << "月 " << day << "日" << "\n" << endl;
}
/* 异常基类 */
class DateException
{
public:
DateException(){}
DateException(int Code, string Message);
int GetCode()const;
string GetMessage()const;
~DateException() {}
int code; //错误码
string message; //错误信息
};
//异常基类的构造函数
DateException::DateException(int Code, string Message)
{
code = Code;
message = Message;
}
//获取错误码函数 GetCode
int DateException::GetCode()const
{
return code;
}
//获取错误信息函数 GetMessage
string DateException::GetMessage()const
{
return message;
}
/* 年异常 */
class DateYearException :public DateException
{
public:
DateYearException(int Code0, string Message0);
~DateYearException(){}
};
//年异常的构造函数
DateYearException::DateYearException(int Code0, string Message0)
{
code = Code0;
message = Message0;
}
/* 月异常 */
class DateMonthException :public DateException
{
public:
DateMonthException(int Code1, string Message1);
~DateMonthException() {}
};
//月异常的构造函数
DateMonthException::DateMonthException(int Code1, string Message1)
{
code = Code1;
message = Message1;
}
/* 日异常 */
class DateDayException :public DateException
{
public:
DateDayException(int Code2, string Message2);
~DateDayException() {}
};
//日异常的构造函数
DateDayException::DateDayException(int Code2, string Message2)
{
code = Code2;
message = Message2;
}
//修改日期函数 SetDate
void Date::SetDate()
{
cin >> year >> month >> day;//
if (year < 1) {//认为年份最小为公元一年
throw DateYearException(YearErr, "Year_Error");
}
else if (month < 1 || month>12) {
throw DateMonthException(MonthErr, "Month_Error");
}
else if (day < 1) {
throw DateDayException(DayErr, "Day_Error");
}
else if (month == 2)
{
if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && day > 29) {
throw DateDayException(DayErr, "Day_Error");//闰年二月
}
else if (!(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && day > 28) {
throw DateDayException(DayErr, "Day_Error");//非闰年二月
}
}
else if (day > 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) {
throw DateDayException(DayErr, "Day_Error");
}
else if (day > 30 && (month == 4 || month == 6 || month == 9 || month == 11)) {
throw DateDayException(DayErr, "Day_Error");
}
}
int main()
{
Date date1; //创建日期对象
date1.GetDate();//显示日期
cout << "请依次输入年、月、日: " ;
try
{
date1.SetDate();
}
catch (const DateYearException YearError)
{
cerr << "\n" << "错误码:" << YearError.GetCode() << " " << "错误信息:" << YearError.GetMessage() << endl;
}
catch (const DateMonthException MonthError)
{
cerr << "\n" << "错误码:" << MonthError.GetCode() << " " << "错误信息:" << MonthError.GetMessage() << endl;
}
catch (const DateDayException DayError)
{
cerr << "\n" << "错误码:" << DayError.GetCode() << " " << "错误信息:" << DayError.GetMessage() << endl;
}
date1.GetDate();
date1.~Date();
system("pause");
return 0;
}
年异常测试
月异常测试
日异常测试