当前位置: 首页 > news >正文

默认成员函数的练习之实现日期类

实现日期类

// Date.h
#define  _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<assert.h>using namespace std;class Date
{
public://构造函数Date(int year = 1, int month = 1, int day = 1);// 因为日期类没有涉及资源开销,所以不需要写拷贝构造和析构函数//打印void Print();// 赋值重载Date& operator=(const Date& d);//操作符重载bool operator<(const Date& d);bool operator<=(const Date& d);bool operator>(const Date& d);bool operator>=(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);//查找每个月的天数,直接定义在类中,默认其是内联函数(因为要多次调用)int GetMonthDay(int year, int month){assert(month > 0 && month < 13);//用static修饰,固定在静态区,避免每次调用函数都创建一个数组static int monthDayArray[13] = { -1, 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;}elsereturn monthDayArray[month];}//日期加等整数Date& operator+=(int day);//日期加整数Date operator+(int day);//日期减等整数Date& operator-=(int day);//日期减整数Date operator-(int day);//日期前置++Date& operator++();//日期后置++Date operator++(int); //日期前置--Date& operator--();//日期后置--Date operator--(int);//日期减日期int operator-(const Date& d);private:int _year;int _month;int _day;
};
// Date.cpp
#define  _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"Date::Date(int year, int month, int day) // 1.要加上访问限定符 2. 声明写缺省值,实现不写
{_year = year;_month = month;_day = day;
}void Date::Print()
{cout << _year << "/" << _month << "/" << _day << endl;
}Date& Date::operator=(const Date& d)// 1.加上返回值,可实现连续赋值 2.使用引用传参减少拷贝构造函数的调用
{if (this != &d) // 防止出现自己给自己赋值的情况{_year = d._year;_month = d._month;_day = d._day;}return *this;
}bool Date::operator<(const 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;}return false;
}bool Date::operator==(const Date& d)
{return _year == d._year&& _month == d._month&& _day == d._day;
}bool Date::operator<=(const Date& d)
{return *this < d || *this == d; // 函数复用,减少代码冗余
}bool Date::operator>(const Date& d)
{return !(*this <= d);
}bool Date::operator>=(const Date& d)
{return !(*this < d);
}bool Date::operator!=(const Date& d)
{return !(*this == d);
}Date& Date::operator+=(int day)
{if (day < 0){return *this -= -day; // 传入的日期为负数,复用-=(下面实现)}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this; // 返回增加day天数后的日期
}Date Date::operator+(int day) // 这里就不可以用引用做返回值了,原因是tmp出了函数作用域就被销毁
{Date tmp = *this; // 拷贝构造return tmp += day;
}Date& Date::operator-=(int day)
{if (day < 0){return *this += -day;}_day -= day;while (_day < 0){_month--;if (_month == 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month); // 这个要放在后面}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;return tmp -= day;
}Date& Date::operator++()
{return *this += 1;
}Date Date::operator++(int) // 参数的目的是构成函数重载
{Date tmp = *this;*this += 1;return tmp; // 注意返回值
}Date& Date::operator--()
{return *this -= 1;
}Date Date::operator--(int)
{Date tmp = *this;*this -= 1;return tmp; 
}int Date::operator-(const Date& d)
{//假设一个大的Date max = *this;Date min = d;int flag = 1;if (*this < d) // 说明假设错误{max = d;min = *this;flag = -1;}int n = 0;while (min != max){++min;++n;}return n * flag;
}
//test.cpp
#define  _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"int main()
{Date d1(2024, 9, 27);d1.Print();Date d2 = d1; // 使用默认的拷贝构造//Date d2(d1);d2.Print();Date d3(2024, 9, 28);//d3 = d1; // 测试赋值重载//d3.Print();Date d4(2024, 9, 30);d4 = d3 = d1; // 连续赋值d4.Print();bool ret = d3 > d4;cout << ret << endl; // 0Date d5(2024, 10, 1);Date ret2 = d5 -= 100;ret2.Print(); // 2024/6/23Date year(2025, 1, 29);int ret3 = d3 - year;cout << ret3 << endl;Date birth(2005, 8, 23); // 可计算你在这个世界上已经存活多少天int ret4 = d3 - birth;cout << ret4 << endl;return 0;
}

相关文章:

  • Linux 学习笔记(十六)—— 重定向与缓冲区
  • Growthly Quest 增长工具:助力 Web3 项目实现数据驱动的增长
  • MySQL vs PostgreSQL:2024年深度对比与选择指南
  • 后端返回内容有换行标识,前端如何识别换行
  • 14.安卓逆向-frida基础-编写hook脚本2
  • 【Python】数据可视化之分布图
  • 在C#中实现WebSocket的单聊和分频道聊天
  • 域 缺省参数 函数重载 引用
  • 【Golang】Go语言中如何面向对象?
  • 【研赛A题成品论文】24华为杯数学建模研赛A题成品论文+可运行代码丨免费分享
  • GO Serial 学习与使用
  • 大模型微调4:Alpaca模型微调、Adalora、Qlora
  • mysql学习教程,从入门到精通,SQL LIKE 运算符(28)
  • C++教程(三):c++常用的配置文件类型
  • 基于nodejs+vue的宠物医院管理系统
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • [iOS]Core Data浅析一 -- 启用Core Data
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • ES10 特性的完整指南
  • exif信息对照
  • Laravel核心解读--Facades
  • MySQL主从复制读写分离及奇怪的问题
  • Python代码面试必读 - Data Structures and Algorithms in Python
  • 前端面试题总结
  • 学习Vue.js的五个小例子
  • 自定义函数
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • #大学#套接字
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • #数据结构 笔记一
  • (2024,RWKV-5/6,RNN,矩阵值注意力状态,数据依赖线性插值,LoRA,多语言分词器)Eagle 和 Finch
  • (el-Transfer)操作(不使用 ts):Element-plus 中 Select 组件动态设置 options 值需求的解决过程
  • (含答案)C++笔试题你可以答对多少?
  • (佳作)两轮平衡小车(原理图、PCB、程序源码、BOM等)
  • (十五)、把自己的镜像推送到 DockerHub
  • .NET : 在VS2008中计算代码度量值
  • .net 8 发布了,试下微软最近强推的MAUI
  • .net 受管制代码
  • .net 写了一个支持重试、熔断和超时策略的 HttpClient 实例池
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • .NetCore项目nginx发布
  • .NET的数据绑定
  • .Net中的集合
  • @Query中countQuery的介绍
  • @RunWith注解作用
  • [ 隧道技术 ] 反弹shell的集中常见方式(二)bash反弹shell
  • [001-03-007].第07节:Redis中的管道
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [20150904]exp slow.txt
  • [ai笔记3] ai春晚观后感-谈谈ai与艺术
  • [AUTOSAR][诊断管理][ECU][$37] 请求退出传输。终止数据传输的(上传/下载)
  • [BT]BUUCTF刷题第4天(3.22)