網站首頁 編程語言 正文
日期類的實現
凡是要寫類必須要提到六大默認成員(六位大爺):構造函數、析構函數、拷貝構造函數、賦值重載函數、取地址重載函數(包括const對象和普通對象);那么這次的日期類又需要伺候哪幾位大爺呢?
日期類的實現中函數與函數之間有較強的耦合性,所以實現的邏輯順序一定要把握好,不然會暈頭轉向的!!! 下面是我的實現順序:
構造函數
Date(const Date& d)//拷貝構造函數
{
_year = d._year;
_month = d._month;
_day = d._day;
}
析構函數
~Date()//析構函數
{
_year = 1;
_month = 1;
_day = 1;
}
拷貝構造函數
Date(const Date& d)//拷貝構造函數
{
_year = d._year;
_month = d._month;
_day = d._day;
}
打印函數
void Print()//打印函數
{
cout << _year << "-" << _month << "-" << _day << endl;
}
這里我們還需要寫一個獲取月份對應天數的函數
獲取天數函數
int GetTrueDay(int year, int month)//得到正確月份天數
{
static int monthday[] = { 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;
}
else
{
return monthday[month];
}
}
這里就是大體框架了,接下來是各個細節部分
運算符重載區
判斷兩個日期是否相等(*this==d)
bool operator==(const Date& d) const;//等于
//---相等為真(返回1);不相同為假(返回0)
bool Date:: operator==(const Date& d) const//等于
{//年相等才判斷到月,月相等才判斷到年
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
判斷前一個日期是否大于后一個日期(*this>d)
bool operator>(const Date& d) const;//大于
//---相等為真(返回1);不相同為假(返回0)
bool Date:: operator>(const Date& d) const//大于
{//這里一樣的判斷順序依次是年---月---日
if (_year > d._year)
{
return true;
}
else if (_month > d._month)
{
return true;
}
else if (_day == d._day)
{
return true;
}
else
{
return false;
}
//大于
}
判斷前一個日期是否大于等于后一個日期(*this>=d)
這里直接重載!!!
bool operator>=(const Date& d) const//大于等于
{
return *this > d || *this == d;
}
判斷前一個日期是否小于后一個日期(*this<d)
這里直接重載!!!
bool operator<(const Date& d)const //判斷小于
{
return !(*this >= d);
}
判斷前一個日期是否小于等于后一個日期(*this<=d)
這里直接重載!!!
bool operator<=(const Date& d)const//小于等于
{
return !(*this > d);
}
賦值重載
前一個日期等于后一個日期(*this=d)—可以連續賦值
Date& operator=(const Date& d)//賦值重載
{
if (this!=&d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
else
{
return*this;
}
}
對日期減天數-不影響自身-用拷貝構造
Date operator-(int day) const;//減天數
Date Date:: operator-(int day) const//減天數-不影響本身-不用引用-用拷貝構造函數
{
Date tmp(*this);
tmp-= day;
return tmp;
}
對日期加天數-不影響自身-用拷貝構造
Date operator+(int day) const;//加天數
Date Date:: operator+(int day) const//加天數-不影響本身-不用引用-用拷貝構造函數
{
Date tmp(*this);
tmp+= day;
return tmp;
}
日期減等天數-影響自身-用引用
Date& operator-=(int day) ;//減等天數
Date& Date:: operator-=(int day) //減等天數- 影響本身-用引用-不加const
{
if (day < 0)
{
return *this += abs(day);
}
_day -= day;
while (_day<=0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetTrueDay(_year, _month);
}
return *this;
}
日期加等天數-影響自身-用引用
Date& operator+=(int day);//加天數
Date& Date:: operator+=(int day) //加天數- 影響本身-用引用-不加const
{
if (day < 0)
{
return *this -= abs(day);
}
_day += day;
while (_day > GetTrueDay(_year, _month))
{
_day -= GetTrueDay(_year,_month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
日期天數前置++【影響自身(自增)-用引用-不加const】
Date& operator++(); //天數前置++
Date& Date::operator++()//前置++-改變自身-用引用
{
return *this += 1;
}
日期天數后置++【不影響自身-用拷貝構造】
Date operator++(int);//后置++
Date Date::operator++(int)//后置++-不改變自身-用拷貝函數-括號里+int
{
Date tmp(*this);
*this += 1;
return tmp;
}
日期天數前置–【影響自身(自減)-用引用-不加const】
Date& operator--();//前置--
Date& Date::operator--()//前置-- --需要改變自身-用引用
{
return *this -= 1;
}
日期天數后置–【不影響自身-用拷貝構造】
Date operator--(int);//后置--
Date Date::operator--(int)//后置--,不需要改變自身-用構造函數-括號里+int
{
Date tmp(*this);
*this -= 1;
return tmp;
}
日期減日期(前一個日期減后一個日期-算差距天數)
int operator-(const Date& d)const;//日期減日期-算差距天數
int Date:: operator-(const Date& d) const//日期減日期-算差距天數-都不改變自身+const
{
Date max = *this;
Date min=d;
int flag = 1;
if (*this<d)
{
max = d;
min = *this;
flag = -1;//如果*this比d小則減出來是負數,所以要預備flag=-1
}
int n = 0;
while (min < max)//min++,max--,最后相等時,n++得出的就是差距天數
{
n++;
min++;
}
return flag * n;//*this比d小,得出來是負數-乘-1,*this比d大,得正數-乘1
}
流插入函數
friend ostream& operator<<(ostream& out, Date& d);//流插入友元聲明
ostream& operator<<(ostream& out, Date& d)//流插入
{
cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return cout;
}
流提取函數
friend istream& operator>>(istream& in, Date& d);//流提取友元聲明
istream& operator>>(istream& in, Date& d)//流提取
{
in>> d._year;
in >> d._month;
in >> d._day;
return in;
}
好啦,以上就是日期類實現各個模塊啦,下面是整體代碼!
整體代碼
Date.h
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
Date(const Date& d)//拷貝構造函數
{
_year = d._year;
_month = d._month;
_day = d._day;
}
~Date()//析構函數
{
_year = 1;
_month = 1;
_day = 1;
}
void Print()//打印函數
{
cout << _year << "-" << _month << "-" << _day << endl;
}
int GetTrueDay(int year, int month)//得到正確月份天數
{
static int monthday[] = { 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;
}
else
{
return monthday[month];
}
}
Date(int year = 1, int month = 1, int day = 1)//構造函數
: _year(year)
, _month(month)
, _day(day)
{
}
bool operator==(const Date& d) const;//等于
bool operator>(const Date& d) const;//大于
bool operator>=(const Date& d) const//大于等于
{
return *this > d || *this == d;
}
bool operator<(const Date& d)const //判斷小于
{
return !(*this >= d);
}
bool operator<=(const Date& d)const//小于等于
{
return !(*this > d);
}
Date& operator=(const Date& d)//賦值重載
{
if (this!=&d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
else
{
return*this;
}
}
Date operator-(int day) const;//減天數
Date operator+(int day) const;//加天數
Date& operator-=(int day) ;//減等天數
Date& operator+=(int day);//加天數
Date& operator++(); //天數前置++
Date operator++(int);//后置++
Date& operator--();//前置--
Date operator--(int);//后置--
int operator-(const Date& d)const;//日期減日期-算差距天數
friend ostream& operator<<(ostream& out, Date& d);//流插入友元聲明
friend istream& operator>>(istream& in, Date& d);//流提取友元聲明
private:
int _year;
int _month;
int _day;
};
Date.cpp
#include"Date.h"
bool Date:: operator==(const Date& d) const//等于
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool Date:: operator>(const Date& d) const//大于
{
if (_year > d._year)
{
return true;
}
else if (_month > d._month)
{
return true;
}
else if (_day == d._day)
{
return true;
}
else
{
return false;
}
}
Date Date:: operator-(int day) const//減天數-不影響本身-不用引用-用拷貝函數
{
Date tmp(*this);
tmp-= day;
return tmp;
}
Date Date:: operator+(int day) const//加天數-不影響本身-不用引用-用拷貝函數
{
Date tmp(*this);
tmp+= day;
return tmp;
}
Date& Date:: operator-=(int day) //減等天數- 影響本身-用引用-不加const
{
if (day < 0)
{
return *this += abs(day);
}
_day -= day;
while (_day<=0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetTrueDay(_year, _month);
}
return *this;
}
Date& Date:: operator+=(int day) //加天數- 影響本身-用引用-不加const
{
if (day < 0)
{
return *this -= abs(day);
}
_day += day;
while (_day > GetTrueDay(_year, _month))
{
_day -= GetTrueDay(_year,_month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
Date& Date::operator++()//前置++-改變自身-用引用
{
return *this += 1;
}
Date Date::operator++(int)//后置++-不改變自身-用拷貝函數-括號里+int
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date& Date::operator--()//前置-- --需要改變自身-用引用
{
return *this -= 1;
}
Date Date::operator--(int)//后置--,不需要改變自身-用構造函數-括號里+int
{
Date tmp(*this);
*this -= 1;
return tmp;
}
int Date:: operator-(const Date& d) const//日期減日期-算差距天數-都不改變自身+const
{
Date max = *this;
Date min=d;
int flag = 1;
if (*this<d)
{
max = d;
min = *this;
flag = -1;//如果*this比d小則減出來是負數,所以要預備flag=-1
}
int n = 0;
while (min < max)
{
n++;
min++;
}
return flag * n;//*this比d小,得出來是負數-乘-1,比大,乘1
}
ostream& operator<<(ostream& out, Date& d)//流插入
{
cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return cout;
}
istream& operator>>(istream& in, Date& d)//流提取
{
in>> d._year;
in >> d._month;
in >> d._day;
return in;
}
原文鏈接:https://blog.csdn.net/m0_71841506/article/details/127377668
相關推薦
- 2022-03-18 docker?創建容器時指定容器ip的實現示例_docker
- 2023-03-18 k8s?service?nodePort無法訪問的問題解決_云其它
- 2022-09-16 python解析照片拍攝時間進行圖片整理_python
- 2022-02-02 css 旋轉 animation動畫
- 2022-04-04 Python數據處理-導入導出excel數據_python
- 2022-04-01 Python格式化輸出之format用法詳解_python
- 2022-08-12 利用Python判斷文件的幾種方法及其優劣對比_python
- 2022-03-26 Flutter構建自定義Widgets的全過程記錄_Android
- 最近更新
-
- 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同步修改后的遠程分支