網站首頁 編程語言 正文
類的定義
#pragma once
#include < iostream>
using std::cout;
using std::cin;
using std::endl;
class Date
{
public:
// 獲取某年某月的天數
int GetMonthDay(int year, int month);
// 全缺省的構造函數
Date(int year = 0, int month = 1, int day = 1);
void Print();
//析構,拷貝構造,賦值重載函數可以不用寫,默認生成的就夠用
//日期+=天數
Date& operator+=(int day);
// 日+天數
Date operator+(int day);
// 日期-天數
Date operator-(int day);
// 日期-=天數
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
//后置--
Date operator--(int);
// 前置--
Date& operator--();
// >運算符重載
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 operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
確定某年某月有多少天
//因為閏年和平年二月份的天數不一樣,隨便加加減減會有問題,這個函數可以根據閏年和平年給出對應的天數
//因為后面需要頻繁調用它,設置成內聯函數
inline int Date::GetMonthDay(int year, int month) {
//設置成靜態變量,不用每次調用這個函數都開辟一個數組
static int arr[13] = {0,31,28,31,30,31,30,31,31,30,31, 30, 31};
int day = arr[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
day = 29;
return day;
}
構造函數
Date::Date(int year, int month, int day) {
//防止給的日期有錯誤
if (year >= 0 && month > 0 && month < 13
&& day >0 && day <= GetMonthDay(year, month)) {
_year = year;
_month = month;
_day = day;
}
else {
cout << "非法日期" << endl;
cout << year << "年" << month << "月" << day << "日" << endl;
}
}
打印日期
void Date::Print() {
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
日期+=天數
Date& Date::operator+=(int day) {
if (day < 0){
*this -= (-day);
}
else {
_day += day;
//如果天數大于月份對應的最大天數,就先減去這個月天數,然后月份++,然后再判斷月份對應的天數對不對,往復循環
while (_day > GetMonthDay(_year, _month)) {
_day -= GetMonthDay(_year, _month);
++_month;
if (_month > 12) {
++_year;
_month = 1;
}
}
//如果是傳值返回的話傳的是他的一個拷貝臨時變量,又需要調用拷貝構造函數,因為出了函數作用域對象還在,所以傳引用更好。
}
return *this;
}
日期+天數
Date Date::operator+(int day) {
Date ret(*this);
//復用operator+=函數
ret += day;
//ret是一個局部變量,出了作用域就銷毀了,傳不了引用
return ret;
}
日期-=天數
Date& Date::operator-=(int day) {
if (day < 0) {
*this += (-day);
}
else {
_day -= day;
while (_day <= 0) {
--_month;
if (_month < 1) {
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
}
return *this;
}
日期-天數
Date Date::operator-(int day) {
Date ret = *this;
ret -= day;
return ret;
}
前置++
//返回的是++后的值,可以復用+=函數,邏輯是一樣的,把day換成1即可
Date& Date::operator++() {
*this += 1;
return *this;
}
后置++
//返回的是++前的值,不能傳引用返回
//為了和前置++賦值重載函數構成函數重載,需要加個int ,不需要傳實參,因為沒用,如果不加參數那么前置++和后置++函數就一樣了,們在進行前置++或者后置++操作時,編譯器就不知道調用哪一個了
Date Date::operator++(int) {
Date ret(*this);
*this += 1;
return ret;
}
后置–
//返回原對象,然后對象再--
Date Date::operator--(int) {
Date ret = *this;
*this -= 1;
return ret;
}
前置–
返回--后的對象
Date& Date::operator--() {
*this -= 1;
return *this;
}
>運算符重載
d1 > d2 -> d1.operator>(&d1, d2)
bool Date::operator>(const Date& d) {
if (_year >= d._year)
{
if (_year > d._year)
return true;
else {
if (_month >= d._month) {
if (_month > d._month)
return true;
else {
if (_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);
}
計算兩個日期之間的間隔天數,日期減去日期
int Date::operator-(const Date& d) {
Date min = *this;
Date max = d;
int flag = -1;
int daycount = 0;
if (*this > d) {
min = d;
max = *this;
flag = 1;
}
while (min != max) {
++min;
daycount++;
}
return flag * daycount;
}
原文鏈接:https://blog.csdn.net/ll1175555/article/details/125697660
相關推薦
- 2022-08-03 C++?sort排序函數用法詳解_C 語言
- 2022-04-24 C語言時間函數的ctime()和gmtime()你了解嗎_C 語言
- 2021-12-13 一次現網問題定位-Redis連接不斷增長
- 2022-12-10 Python對XML文件實現增刪改查操作_python
- 2022-12-21 Docker教程之dockerfile構建centos鏡像_docker
- 2022-02-24 Go入門所踩過的坑:cannot find package "" in any of
- 2022-12-05 C#?md5?算法實現代碼_C#教程
- 2022-03-16 Linux系統中日志詳細介紹_Linux
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支