【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

C++ 模板进阶:特化、萃取与可变参数模板

C++ 模板进阶:特化、萃取与可变参数模板

C++ 模板进阶:特化、萃取与可变参数模板 💡 学习目标:掌握模板进阶技术的核心用法,理解模板特化的深层应用、类型萃取的实现原理,以及可变参数模板的灵活使用,提升泛型编程的实战能力。 💡 学习重点:模板特化的进阶场景、类型萃取工具的设计与应用、可变参数模板的展开技巧、折叠表达式的使用方法。 一、模板特化进阶:处理复杂类型场景 💡 模板特化不只是针对单一类型的定制,还能处理指针、引用、数组等复杂类型,实现更精细的类型适配逻辑。 1.1 指针类型的模板特化 通用模板默认处理普通类型,我们可以为指针类型单独编写特化版本,实现指针专属的逻辑。 #include<iostream>#include<string>usingnamespace std;// 通用模板:处理普通类型template<typenameT>classTypeProcessor{public:staticvoidprocess(T data){ cout

By Ne0inhk

在 Mac 上完美配置 VSCode 的 C/C++ 开发环境(GCC/G++ 详细教程 )

本文手把手教你如何在 macOS 系统上配置 VSCode 的 C/C++ 开发环境,解决各种常见问题,让你轻松开启 C/C++ 编程之旅! 前言 作为程序员,一个顺手的开发环境至关重要。VSCode 作为轻量级但功能强大的代码编辑器,配合 GCC/G++ 编译器,能够在 Mac 上提供优秀的 C/C++ 开发体验。本文将详细介绍从零开始的完整配置过程。 一、环境准备:安装编译工具 1.1 安装 Xcode Command Line Tools(推荐首选) 打开终端,执行以下命令: xcode-select --install 执行后会弹出安装对话框,点击"安装"即可。

By Ne0inhk

C++ 运算符重载全解析:+、=、>>、<<、++、==

什么是运算符重载?         运算符重载是 C++ 提供的一种语法扩展机制,使得自定义类也可以使用类似于内置类型的操作符,换句话说,你可以为你的类赋予自然的操作符语义,让代码更优雅、更贴近业务逻辑。  基本语法         重载函数本质上是一个特殊函数: 返回类型 operator符号(参数列表) 它可以是: * 成员函数 * 非成员函数(全局函数,或友元)  一、重载 + 运算符  为什么要重载 + 运算符?         假设你有一个二维向量类: class Vector2D { public: int x, y; };         现在你希望支持类似以下操作: Vector2D a(1, 2), b(3, 4); Vector2D c = a + b; // 理想行为         这是不可能的,除非你 告诉编译器“+”在你的类中应该怎么用 —— 也就是重载它。  如何重载

By Ne0inhk
OpenClaw Java — 用 Java 全栈实现一个 AI Agent Gateway

OpenClaw Java — 用 Java 全栈实现一个 AI Agent Gateway

项目简介 大家好,分享一下我最近在做的开源项目 OpenClaw Java —— 基于 Spring Boot 3.3 的 AI Agent Gateway 全栈实现,通过 WebSocket 自定义帧协议提供全功能 Agent 接口。 项目地址:https://github.com/yuenkang/openclaw-java 当前规模: 594 个 Java 源文件 + 17 个测试文件,约 88,500 行代码 为什么做这个项目? 目前 AI Agent 框架大多集中在 Python 和 TypeScript 生态,Java 社区相对缺少成熟的 Agent 运行时方案。

By Ne0inhk