《C++初阶之类和对象》【经典案例:日期类】
《C++初阶之类和对象》【经典案例:日期类】
往期《C++初阶》回顾:
/------------ 入门基础 ------------/
【C++的前世今生】
【命名空间 + 输入&输出 + 缺省参数 + 函数重载】
【普通引用 + 常量引用 + 内联函数 + nullptr】
/------------ 类和对象 ------------/
【类 + 类域 + 访问限定符 + 对象的大小 + this指针】
【类的六大默认成员函数】
【初始化列表 + 自定义类型转换 + static成员】
【友元 + 内部类 + 匿名对象】
前言:
(✧∇✧)ノ hi~ ,小伙伴们早上好呀!(๑˃̵ᴗ˂̵)و 昨天终于考完试啦~🎉 今天就要收拾行李回家咯 🧳在踏上归途之前,博主决定为大家带来类和对象专题的最后一节精华内容:【经典案例:日期类】的实现!✨
这个案例将串联我们之前学过的所有重要概念,绝对是检验学习成果的完美实践!💪
经过前面关于 C++ 类和对象的系统学习 📚✨,我们已经逐步掌握了:
- 类的定义 💡
- 对象的创建与使用 🔄
- 成员变量和成员函数的设计 💻
这些知识就像搭建高楼的基石 🏢⛏️,为我们接下来的实践操作奠定了坚实的基础 💪!现在,是时候将这些理论运用到实际中,亲手实现一个"日期类"啦~ (๑•̀ㅂ•́)و✧
------------代码实现------------
设计指南
我们要实现的日期类(Date)应该具备以下功能:
- 存储年、月、日信息
- 支持日期的合法性验证
- 实现日期的基本运算(如:加减天数)
- 支持日期的比较操作
- 提供多种格式的日期输出
程序代码
头文件:Data.h
#pragma once
//任务1:包含需要使用的头文件
#include <iostream>
#include <assert.h>
using namespace std;
//任务2:实现日期类
class Date
{
/*------------------------------第四部分:使用友元函数进行运算符重载------------------------------*/
friend ostream& operator<<(ostream& out, const Date& d); //输出流重载
friend istream& operator>>(istream& in, Date& d); //输入流重载
public:
/*------------------------------第一部分:定义默认函数------------------------------*/
/*--------------实现:默认构造函数--------------*/
Date(int year,int month,int day) //全缺省的默认构造函数
:_year(year)
,_month(month)
,_day(day)
{
}
/*------------------------------第二部分:Date类的成员函数------------------------------*/
/*--------------实现:“打印日期”的成员函数--------------*/
void Pirnt()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
/*--------------实现:“获取某年的某月的天数”的成员函数--------------*/
int GetMonthDay(int year, int month) //注意:该成员函数使用频繁,所以选择类内实现,自动为内联函数
{
//1.使用断言保证传入的日期的合法性
assert(month >= 1 && month <= 12);
//2.定义一个静态数组存储一年的每月的天数
static int MonthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//3.判断年是闰年还是平年
//1)能被4整除且不能被100整除的年正好是闰年
//2)能被400整除的年也是闰年
//特殊的情况 (二月的天数)
if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 29;
}
//正常的情况
else
{
return MonthDayArray[month];
}
}
/*--------------声明:“判断一个日期是否合法”的成员函数--------------*/
bool checkDate();
/*------------------------------第三部分:使用成员函数进行运算符重载------------------------------*/
/*--------------声明使用成员函数重载的“比较运算符”--------------*/
bool operator<(const Date& d)const;
bool operator<=(const Date& d)const;
bool operator>(const Date& d)const;
bool operator>=(const Date& d)const;
bool operator==(const Date& d)const;
bool operator!=(const Date& d)const;
/*--------------声明使用成员函数重载的“加减运算符”--------------*/
Date& operator+=(int day);
Date operator+(int day)const;
Date& operator-=(int day);
Date operator-(int day)const;
int operator-(const Date& d)const;
/*--------------声明使用成员函数重载的“自增/自减运算符”--------------*/
Date& operator++(); //前置++
Date operator++(int); //后置++
Date& operator--(); //前置--
Date operator--(int); //后置--
private:
int _year;
int _month;
int _day;
};
//任务3:声明流运算符重载
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
源文件:
Data.cpp
#include "Date.h"
/*---------------------------------实现:“检查一个日期是否合法”---------------------------------*/
bool Date::checkDate()
{
if (_month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month))
{
return false;
}
else
{
return true;
}
}
/*---------------------------------“比较”运算符的重载函数的实现---------------------------------*/
/*---------------------1.实现:运算符<的重载函数---------------------*/
bool Date::operator<(const Date& d)const
{
//1.比较年份
if (_year < d._year)
{
return true;
}
//2.年份相同比较月份
else if (_year == d._year)
{
if (_month < d._month)
{
return true;
}
//3.年月相同比较日
else if (_month == d._month)
{
return _day < d._day;
}
}
return false;
}
/*---------------------2.实现:运算符<=的重载函数---------------------*/
bool Date::operator<=(const Date& d)const
{
return *this < d || *this == d;
}
/*---------------------3.实现:运算符>的重载函数---------------------*/
bool Date::operator>(const Date& d)const
{
return !(*this <= d);
}
/*---------------------4.实现:运算符>=的重载函数---------------------*/
bool Date::operator>=(const Date& d)const
{
return !(*this < d);
}
/*---------------------5.实现:运算符==的重载函数---------------------*/
bool Date::operator==(const Date& d)const
{
只要两个日期的:年、月、日中有一个不相等,这两个日期就不相等
//if (_year != d._year || _month != d._month || _day != d._day)
//{
//return false;
//}
return _year == d._year && _month == d._month && _day == d._day;
}
/*---------------------6.实现:运算符!=的重载函数---------------------*/
bool Date::operator!=(const Date& d)const
{
return !(*this == d);
}
/*---------------------------------“加减”运算符的重载函数的实现---------------------------------*/
/*---------------------1.实现:运算符+=的重载函数(日期 += 天数)---------------------*/
Date& Date::operator+=(int day)
{
//1.判断要加上的天数是不是负数
if (day < 0)
{
return *this -= (-day);
}
//2.直接将天数加在日期的天数上
_day += day;
//3.使用while循环修正日期的天数
while (_day > GetMonthDay(_year, _month))
{
//3.1:直接将不合法的天数减去当前日期的这一月应有的天数
_day -= GetMonthDay(_year, _month);
//3.2:调整月份
_month++;
//3.3:修正月份
if (_month > 12)
{
_year++;
_month = 1;
}
}
return *this;
}
/*---------------------2.实现:运算符+的重载函数(日期 + 天数)---------------------*/
Date Date::operator+(int day)const
{
Date tmp = *this;
tmp += day;
return tmp;
}
/*---------------------3.实现:运算符-=的重载函数(日期 -= 天数)---------------------*/
Date& Date::operator-=(int day)
{
//1.判断要减去的天数是不是负数
if (day < 0)
{
return *this += (-day);
}
//2.直接将天数减去日期的天数
_day -= day;
//3.使用while循环修正日期的天数
while (_day <= 0)
{
//3.1:调整月份
_month--;
//3.2:修正月份
if(_month<0)
{
_year--;
_month = 12;
}
//3.3:直接将不合法的天数加上当前日期的上一月应有的天数
_day += GetMonthDay(_year, _month);
}
return *this;
}
/*---------------------4.实现:运算符-的重载函数(日期 - 天数)---------------------*/
Date Date::operator-(int day)const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
/*---------------------5.实现:运算符-的重载函数(日期 - 日期)---------------------*/
int Date::operator-(const Date& d) const
{
//1.使用假设法:假设调用者的日期更大,被调者d的日期更小
Date max = *this;
Date min = d;
//2.调整max和min的日期
int flag = 1; //flag的设置很关键:日期之差没有负值,可以使用flag进行调整
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
//3.定义变量记录两个日期之间的天数差值
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
/*---------------------------------“自增/自减”运算符的重载函数的实现---------------------------------*/
/*---------------------1.实现:运算符前置++的重载函数---------------------*/
Date& Date::operator++()
{
*this += 1;
return *this;
}
/*---------------------2.实现:运算符后置++的重载函数---------------------*/
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
/*---------------------3.实现:运算符前置--的重载函数---------------------*/
Date& Date::operator--()
{
*this -= 1;
return *this;
}
/*---------------------4.实现:运算符后置--的重载函数---------------------*/
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
/*---------------------------------“流运算符”运算符的重载函数的实现---------------------------------*/
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
if (!d.checkDate())
{
cout << "输出的日期不合法!!!";
}
return in;
}
Test.cpp
#include "Date.h"
void printTestResult(const char* testName, bool passed)
{
cout << testName <<"\t" << "[" << (passed ? "成功" : "失败") << "]" << endl;
}
int main()
{
cout << "===== 日期类测试 =====" << endl;
/*----------------测试1: 日期合法性----------------*/
Date valid(2024, 5, 15); //合法日期
Date invalid(2024, 2, 30); //不合法日期
printTestResult("日期合法性检查", valid.checkDate() && !invalid.checkDate());
/*----------------测试2: 比较运算符----------------*/
Date d1(2024, 5, 15);
Date d2(2024, 5, 16);
bool cmpResult = (d1 < d2) && (d1 <= d2) && !(d1 > d2) && !(d1 >= d2);
printTestResult("比较运算符", cmpResult);
/*----------------测试3: 加法运算----------------*/
Date d3 = d1 + 30;
Date expectedAdd(2024, 6, 14);
printTestResult("日期+天数", d3 == expectedAdd);
/*----------------测试4: 减法运算----------------*/
Date d4 = d1 - 25;
Date expectedSub(2024, 4, 20);
printTestResult("日期-天数", d4 == expectedSub);
Date d5(2024, 4, 15);
int sub = d1 - d5;
printTestResult("日期-日期", sub == 30);
/*----------------测试5: 自增运算符----------------*/
Date d6 = d1;
Date postInc = d6++; //注意这里的细节:先进行后置++的测试,再进行前置++的测试,这样输出的结果会更直观一点
Date preInc = ++d6;
printTestResult("自增运算符", (postInc == Date(2024, 5, 15)) && (preInc == Date(2024, 5, 17)));
/*----------------测试6: 流输出----------------*/
cout << "流输出测试: " << d1;
/*----------------测试7: 流输入(手动验证)----------------*/
cout << "请输入日期 (YYYY MM DD): ";
Date input(2025, 5, 15);
cin >> input;
cout << "输入结果: " << input;
return 0;
}
运行结果:
------------核心操作------------
操作一:获取某年的某月的天数
/*--------------实现:“获取某年的某月的天数”的成员函数--------------*/
int GetMonthDay(int year, int month) //注意:该成员函数使用频繁,所以选择类内实现,自动为内联函数
{
//1.使用断言保证传入的日期的合法性
assert(month >= 1 && month <= 12);
//2.定义一个静态数组存储一年的每月的天数
static int MonthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//3.判断年是闰年还是平年
//1)能被4整除且不能被100整除的年正好是闰年
//2)能被400整除的年也是闰年
//特殊的情况 (二月的天数)
if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 29;
}
//正常的情况
else
{
return MonthDayArray[month];
}
}
操作二:判断一个日期是否合法
/*---------------------------------实现:“检查一个日期是否合法”---------------------------------*/
bool Date::checkDate()
{
if (_month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month))
{
return false;
}
else
{
return true;
}
}
操作三:运算符+=的重载函数(日期 += 天数)
/*---------------------1.实现:运算符+=的重载函数(日期 += 天数)---------------------*/
Date& Date::operator+=(int day)
{
//1.判断要加上的天数是不是负数
if (day < 0)
{
return *this -= (-day);
}
//2.直接将天数加在日期的天数上
_day += day;
//3.使用while循环修正日期的天数
while (_day > GetMonthDay(_year, _month))
{
//3.1:直接将不合法的天数减去当前日期的这一月应有的天数
_day -= GetMonthDay(_year, _month);
//3.2:调整月份
_month++;
//3.3:修正月份
if (_month > 12)
{
_year++;
_month = 1;
}
}
return *this;
}
操作四:运算符-=的重载函数(日期 -= 天数)
/*---------------------3.实现:运算符-=的重载函数(日期 -= 天数)---------------------*/
Date& Date::operator-=(int day)
{
//1.判断要减去的天数是不是负数
if (day < 0)
{
return *this += (-day);
}
//2.直接将天数减去日期的天数
_day -= day;
//3.使用while循环修正日期的天数
while (_day <= 0)
{
//3.1:调整月份
_month--;
//3.2:修正月份
if(_month<0)
{
_year--;
_month = 12;
}
//3.3:直接将不合法的天数加上当前日期的上一月应有的天数
_day += GetMonthDay(_year, _month);
}
return *this;
}
操作五:运算符-的重载函数(日期 - 日期)
/*---------------------5.实现:运算符-的重载函数(日期 - 日期)---------------------*/
int Date::operator-(const Date& d) const
{
//1.使用假设法:假设调用者的日期更大,被调者d的日期更小
Date max = *this;
Date min = d;
//2.调整max和min的日期
int flag = 1; //flag的设置很关键:日期之差没有负值,可以使用flag进行调整
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
//3.定义变量记录两个日期之间的天数差值
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
原文地址:https://blog.csdn.net/2301_80221951/article/details/149126247
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!