【C++修炼之路】类与对象实战:实现一个日期类

【C++修炼之路】类与对象实战:实现一个日期类
Alt

🏝️专栏: 【C++修炼之路】
🌅主页: f狐o狸x

“于高山之巅,方见大河奔涌;于群峰之上,更觉长风浩荡” 


目录

一、日期类的核心功能

 二、日期类的定义

三、实现日期类比较大小

四、日期类加减

五、输入输出日期

六、附带功能


        经过前面两篇文章的学习,相信聪明的你应该已经初步了解类与对象了,现在我们将一起实现一个日期类,进一步加深我面对类的理解。

        在软件开发中,日期和时间的处理无处不在,从日程管理到金融计算,从数据分析到天气预报,日期类的设计都是开发者必须面对的挑战。在本文中,我们将从零开始,一步步实现一个功能完备的日期类。无论你是C++新手,还是想巩固面向对象编程基础,这个项目都会让你收获满满。

一、日期类的核心功能

        想象一下:如果你要实现你手机里的日历这个app,它应该有些什么功能呢?

        我认为主要功能如下:

 日期合法性校验 日期加减(支持天数、月数、年数) 日期差计算 重载运算符(+-==<< 等)星期的计算与格式化输出

 二、日期类的定义

        要想实现上面的功能,我们需要三个内部成员来表示日期(年、月、日),在通过各种函数来实现各种功能

class Date { //友元声明 friend ostream& operator<<(ostream& _cout, const Date& d); friend istream& operator>>(istream& _cin, Date& d); public: //构造函数 Date(int year = 1, int month = 1, int day = 1); //拷贝构造函数 Date(Date& d) { _year = d._year; _month = d._month; _day = d._day; } //打印日历 void PrintCalendar() { cout << _year << "-" << _month << "-" << _day << endl; } //操作符重载 bool operator<(const Date& x) const; bool operator==(const Date& x) const; bool operator<=(const Date& x) const; bool operator>(const Date& x) const; bool operator>=(const Date& x) const; bool operator!=(const Date& x) const; // 获取某年某月的天数 int GetMonthDay(int year, int month); Date& operator+=(int days); Date operator+(int days); Date& operator-=(int days); Date operator-(int days); Date& operator++(); Date operator++(int); Date& operator--(); Date operator--(int); // 日期类成员 private: int _year; // 年 int _month; // 月 int _day; // 日 };

三、实现日期类比较大小

        比较两天日期的大小我们用重载函数  bool operate<(const class& Date) const 和 bool operator==(const Date& x) const两个重载函数完成,剩下的可以用这两个表示

bool Date::operator<(Date& d) { if (_year < d._year) return true; else if (_year == d._year && _month < d._month) return true; else if (_year == d._year && _month == d._month && _day < d._day) return true; else return false; } bool Date::operator==(Date& d) { if (_year == d._year && _month == d._month && _day == d._day) return true; else return false; } bool Date::operator<=(Date& d) { return (*this) < d || (*this) == d; } bool Date::operator>(Date& d) { return !(*this <= d); } bool Date::operator>=(Date& d) { return !(*this < d); } bool Date::operator!=(Date& d) { return !(*this == d); }

四、日期类加减

        想完成日期类的加减,其实就是把日期里的天数都合法化,大于这个月的天数,就加一个月,日期小于等于0,就减少一个月,当月份加到13的时候,就重置为1,年份加1,同理,当月份减少到0的时候,就置为12,年份减1。这里我们还需要写一个函数来获取这个月的天数有多少天(主要是判断是否为闰年闰月,如果是就返回29天,其他就正常返回)

int Date::GetMonthDay(int year, int month) { int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) return 29; return days[month]; } Date& Date::operator+=(int days) { _day += days; while (_day > GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); _month++; if (_month == 12) { _year++; _month = 1; } } return *this; } Date Date::operator+(int days) const { Date tmp = (*this); tmp += days; return tmp; } Date& Date::operator-=(int days) { _day -= days; while (_day <= 0) { _day += GetMonthDay(_year, _month); _month--; if (_month == 0) { _month = 12; _year--; } } return *this; } Date Date::operator-(int days) const { Date tmp = *this; tmp -= days; return tmp; } Date& Date::operator++() { *this += 1; return *this; } Date Date::operator++(int) const { Date tmp = *this; tmp += 1; return tmp; } Date& Date::operator--() { *this -= 1; return *this; } Date Date::operator--(int) const { Date tmp = *this; tmp -= 1; return tmp; } int Date::operator-(const Date d) const { Date max = *this, min = d; int flag = 1; if (max < min) { flag = -1; max = d; min = *this; } int n = 0; while (max != min) { ++min; ++n; } return n * flag; }

五、输入输出日期

        这里需要用到友元函数,才能将按我们的习惯把日出输出出来

ostream& operator<<(ostream& _cout, const Date& d) { _cout << d._year << "-" << d._month << "-" << d._day ; return _cout; } istream& operator>>(istream& _cin, Date& d) { _cin >> d._year >> d._month >> d._day; return _cin; }

六、附带功能

        完成这些之后,我们可以通过库函数<ctime>去获得当天的日期,然后做一份倒计时出来

void TestCalendar1() { Date d1,d2; d1.GetTodayData(); cout << "今天是:" << d1 << endl; cout << "请输入蓝桥杯日期:" ; cin >> d2; cout << "距离蓝桥杯还有" << (d2 - d1) << "天,加油吧骚年" << endl; } int main() { TestCalendar1(); return 0; } 

         这里就是日期类的全部实现内容啦,如果你也自己实现一遍,你对类和对象的理解将会上升一个层次,加油学习吧!

Read more

Python 3安装requests库的详细教程

Python 3安装requests库的详细教程

目录 一、安装 requests 1. 使用 pip 安装 2. 验证安装 二、基本使用示例 1. 发送 GET 请求 2. 发送 POST 请求 3. 处理响应 三、常见问题及解决方法 1. 安装失败 2. SSL 证书验证失败 四、总结 参考链接 requests 是一个非常流行的 Python HTTP 库,用于发送各种 HTTP 请求,如 GET、POST、PUT、DELETE 等。它简单易用且功能强大,是开发中不可或缺的工具。本文将详细介绍如何在 Python

By Ne0inhk
使用 Miniforge3 管理 Python 环境的详细指南(基于最新实践和时效性信息,截至 2025 年)

使用 Miniforge3 管理 Python 环境的详细指南(基于最新实践和时效性信息,截至 2025 年)

以下是使用 Miniforge3 管理 Python 环境的详细指南(基于最新实践和时效性信息,截至 2025 年): 一、Miniforge3 简介 Miniforge3 是一个轻量级 Conda 环境管理工具,默认使用 conda-forge 软件源(社区维护的包更全且更新更快),尤其适配 ARM 架构(如 Apple M1/M2/M3 芯片)。相比 Anaconda,它更精简且兼容性更好。 二、安装步骤 1. 下载安装包 安装最新的 Mamba,建议通过安装 Miniforge 来实现,Miniforge 默认包含 Mamba * 推荐镜像源 * 南京大学镜像站 * 清华大学开源软件镜像站(https://mirrors.tuna.

By Ne0inhk

【Python 爬虫实战】抓取 BOSS 直聘

一、前言 在求职或行业调研过程中,我们常常需要批量获取招聘平台的岗位信息,手动复制粘贴效率极低。本文将通过 DrissionPage 框架实现BOSS 直聘大数据开发岗位的批量爬取,无需分析复杂的页面元素,直接监听接口数据包获取 JSON 数据,最终将结果存入 CSV 文件,全程代码简洁易懂,新手也能快速上手。 本次实战目标 1. 监听 BOSS 直聘岗位列表接口,获取结构化 JSON 数据 2. 提取岗位名称、公司、薪资、学历要求等核心信息 3. 将爬取结果批量存入 CSV 文件,方便后续数据分析 4. 实现自动翻页,爬取前 20 页的岗位数据 二、环境准备 1. 所需 Python 库 本次实战核心使用 DrissionPage 框架(

By Ne0inhk