hello,家人们,今天咱们来实现一个日期类,好啦,废话不多讲,开干.
在实现日期类之前,首先我们得确定日期类中有哪些函数需要实现滴,那么为了令这个日期类更加得系统化,我们实现一个声明与定义分离的日期类
#pragma once
#include <iostream>
using namespace std;
#include <assert.h>
class Date
{
public:
//构造函数
Date(int year = 1, int month = 1, int day = 1);
//获取某年某月的天数
int Getmonthday(int year,int month)
{
assert(month > 0 && month < 13);
int MonthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
//判断是否是闰年,4年一闰,百年不闰,四百年一闰
if ((month == 2) && ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0))
return 29;
return MonthDays[month];
}
//打印日期
void Print();
//判断日期是合法
bool CheckInvalid()
{
if (_year <= 0 || _month < 1 || _month > 12 || _day < 1 || _day > Getmonthday(_year, _month))
return false;
return true;
}
//const成员函数,此时不能够对类的任何成员进行修改
bool operator>(const Date& d) const;
bool operator>=(const Date& d)const;
bool operator<(const Date& d)const;
bool operator==(const Date& d)const;
bool operator!=(const Date& d)const;
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);
//友元声明
friend ostream& operator<<(ostream& out, const Date & d);
friend istream& operator>>(istream& in, Date& d);
private:
int _year;
int _month;
int _day;
};4:上面三个函数后,接下来我们就得实现日期的等式比较了,分别是 > >= < <= == !=
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
bool Date::operator>(const Date & d) const
{
if (_year > d._year)
return true;
//年份相同,比较月份
else if(_year == d._year)
{
if (_month > d._month)
return true;
//年份月份,均相同则比较天数
else if(_month == d._month)
{
if (_day > d._day)
return true;
}
}
//其他情况均返回false;
return false;
}
bool Date::operator>=(const Date& d)const
{
////写法一
//return this->operator>(d) || this->operator==(d);
//写法二
return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)const
{
//>=进行取反则是<
return !this->operator>=(d);
}
bool Date::operator==(const Date& d)const
{
return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator!=(const Date& d)const
{
return !(*this == d);
}#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!CheckInvalid())
{
cout << "构造日期非法" << endl;
}
}
void Date::Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
bool Date::operator>(const Date & d) const
{
if (_year > d._year)
return true;
//年份相同,比较月份
else if(_year == d._year)
{
if (_month > d._month)
return true;
//年份月份,均相同则比较天数
else if(_month == d._month)
{
if (_day > d._day)
return true;
}
}
//其他情况均返回false;
return false;
}
bool Date::operator>=(const Date& d)const
{
////写法一
//return this->operator>(d) || this->operator==(d);
//写法二
return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)const
{
//>=进行取反则是<
return !this->operator>=(d);
}
bool Date::operator==(const Date& d)const
{
return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator!=(const Date& d)const
{
return !(*this == d);
}Date Date::operator+(int day)
{
//拷贝构造:同类型一个存在的对象进行初始化另一个要创建的对象(因为要在不改变原日期的基础上进行操作)
Date tmp = *this;
tmp += day;
return tmp;
}
Date& Date::operator+=(int day)
{
//先增加天数
_day += day;
//判断是否大于本年本月的天数
while(_day > Getmonthday(this->_year,this->_month))
{
_day -= Getmonthday(this->_year, this->_month);
_month++;
if(_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}Date& Date::operator-=(int day)
{
//先减天数
_day -= day;
//进行轮回,开始减月份
while (_day < 0)
{
_month--;
if(_month == 0)
{
_year--;
_month = 12;
}
//每减了一次月份后,再获取当月的天数
_day += Getmonthday(this->_year, this->_day);
}
return *this;
}
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}//前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
//后置++
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
//前置--
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//后置--
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}//日期 - 日期(日期-天数的函数重载形式)
int Date::operator-(const Date & d)
{
//找到小的那个日期
int flag = 1;
Date max = *this;
Date min = d;
if(*this < d)
{
max = d;
min = *this;
}
//标记加的天数
int days = 0;
//然后小的日期不断++,直到与max相等
while(min != max)
{
min++;
days++;
}
return days * flag;
}#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
int main()
{
Date d1(2012, 3, 8);
d1.Print();
Date d2(2013, 13, 22);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
int main()
{
Date d1(2012, 3, 8);
Date d2(2013, 11, 3);
Date d3(2024, 9, 10);
Date d4(2024, 9, 10);
cout << (d1 < d2) << endl;
cout << (d2 > d1) << endl;
cout << endl;
cout << (d3 >= d4) << endl;
cout << (d3 == d4) << endl;
cout << (d3 != d4) << endl;
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
void TestAdd()
{
Date d1(2022, 3, 8);
d1.Print();
Date d2(2023, 7, 6);
d2 += 100;
Date d3 = d1 + 15;
d2.Print();
d3.Print();
}
int main()
{
TestAdd();
}
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
void TestSub()
{
Date d1(2022, 3, 8);
Date d2(2023, 7, 6);
d1 -= 7;
d1.Print();
Date d3 = d2 - 16;
d3.Print();
}
int main()
{
//TestAdd();
TestSub();
}
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
void Test_Set_front_and_back()
{
Date d1;
Date d2(2023, 12, 29);
//前置++与--是先自变再使用
//后置++与--是先使用再自变
d1 = d2++;
d1.Print();
d2.Print();
cout << endl;
Date d3 = ++d2;
d3.Print();
d2.Print();
cout << endl;
Date d4 = d2;
d4.Print();
d2.Print();
cout << endl;
Date d5 = d4--;
d4.Print();
d5.Print();
cout << endl;
Date d6 = --d4;
d4.Print();
d6.Print();
}
int main()
{
Test_Set_front_and_back();
}

#pragma once
#include <iostream>
using namespace std;
#include <assert.h>
class Date
{
public:
//构造函数
Date(int year = 1, int month = 1, int day = 1);
//获取某年某月的天数
int Getmonthday(int year, int month)
{
assert(month > 0 && month < 13);
int MonthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
//判断是否是闰年,4年一闰,百年不闰,四百年一闰
if ((month == 2) && ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0))
return 29;
return MonthDays[month];
}
//打印日期
void Print();
//判断日期是合法
bool CheckInvalid()
{
if (_year <= 0 || _month < 1 || _month > 12 || _day < 1 || _day > Getmonthday(_year, _month))
return false;
return true;
}
//const成员函数,此时不能够对类的任何成员进行修改
bool operator>(const Date& d) const;
bool operator>=(const Date& d)const;
bool operator<(const Date& d)const;
bool operator==(const Date& d)const;
bool operator!=(const Date& d)const;
//日期加天数
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);
//友元声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
private:
int _year;
int _month;
int _day;
};#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!CheckInvalid())
{
cout << "构造日期非法" << endl;
}
}
void Date::Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
bool Date::operator>(const Date& d) const
{
if (_year > d._year)
return true;
//年份相同,比较月份
else if (_year == d._year)
{
if (_month > d._month)
return true;
//年份月份,均相同则比较天数
else if (_month == d._month)
{
if (_day > d._day)
return true;
}
}
//其他情况均返回false;
return false;
}
bool Date::operator>=(const Date& d)const
{
////写法一
//return this->operator>(d) || this->operator==(d);
//写法二
return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)const
{
//>=进行取反则是<
return !this->operator>=(d);
}
bool Date::operator==(const Date& d)const
{
return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator!=(const Date& d)const
{
return !(*this == d);
}
Date& Date::operator+=(int day)
{
//先增加天数
_day += day;
//判断是否大于本年本月的天数
while (_day > Getmonthday(this->_year, this->_month))
{
_day -= Getmonthday(this->_year, this->_month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day)
{
//拷贝构造:同类型一个存在的对象进行初始化另一个要创建的对象
Date tmp = *this;
tmp += day;
return tmp;
}
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
Date& Date::operator-=(int day)
{
//先减天数
_day -= day;
//进行轮回,开始减月份
while (_day <= 0)
{
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
//每减了一次月份后,再获取当月的天数
_day += Getmonthday(this->_year, this->_month);
}
return *this;
}
//前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
//后置++
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
//前置--
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//后置--
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
//日期 - 日期(日期-天数的函数重载形式)
int Date::operator-(const Date& d)
{
//找到小的那个日期
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
}
//标记加的天数
int days = 0;
//然后小的日期不断++,直到与max相等
while (min != max)
{
min++;
days++;
}
return days * flag;
}#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
void TestAdd()
{
Date d1(2022, 3, 8);
d1.Print();
Date d2(2023, 7, 6);
d2 += 100;
Date d3 = d1 + 15;
d2.Print();
d3.Print();
}
void TestSub()
{
Date d1(2022, 3, 8);
Date d2(2023, 7, 6);
d1 -= 7;
d1.Print();
Date d3 = d2 - 16;
d3.Print();
}
void Test_Set_front_and_back()
{
Date d1;
Date d2(2023, 12, 29);
//前置++与--是先自变再使用
//后置++与--是先使用再自变
d1 = d2++;
d1.Print();
d2.Print();
cout << endl;
Date d3 = ++d2;
d3.Print();
d2.Print();
cout << endl;
Date d4 = d2;
d4.Print();
d2.Print();
cout << endl;
Date d5 = d4--;
d4.Print();
d5.Print();
cout << endl;
Date d6 = --d4;
d4.Print();
d6.Print();
}
void TestDateSubDate()
{
Date d1(2022, 3, 8);
Date d2(2022, 4, 7);
Date d3(2024, 3, 8);
cout << d2 - d1 << endl;
cout << d3 - d1 << endl;
}
int main()
{
//TestAdd();
//TestSub();
//Test_Set_front_and_back();
TestDateSubDate();
}好啦,家人们,关于日期类实现这块的相关细节知识,博主就讲到这里了,如果uu们觉得博主讲的不错的话,请动动你们滴滴的小手给博主点个赞,你们滴鼓励将成为博主源源不断滴动力.