本次博客内容是实现一个日期类,其中包含日期类的拷贝构造,析构函数等,包括日期类的大小比较和日期相减的差值,值得注意的是,日期类的成员变量都是内置类型,因此默认成员函数其实是不用写的,不过这是一个练习和理解的过程,日后还需努力.
Date.h
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
void Print()const;
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d);
// 析构函数
~Date();
// 日期+=天数
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;
};
Date.cpp
#include"Date.h"
void Date:: Print()const
{
cout << _year << '/' << _month << '/' << _day << endl;
}
// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
const static int MonthDay[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 MonthDay[month];
}
// 全缺省的构造函数
Date:: Date(int year , int month , int day )
{
_year = year;
_month = month;
_day = day;
if (month < 1 || month > 12 || day < 1 || day > GetMonthDay(year, month))
{
cout << "非法日期" << endl;
}
}
// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return (*this);
}
// 析构函数
Date::~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
// 日期+=天数
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;
}
// 日期+天数
Date Date::operator+(int day)
{
Date tmp(*this);
tmp += day;
return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
if (day < 0)
{
return (*this) += (-day);
}
while (day > 0)
{
if (_day < day)
{
day -= _day;
if (_month == 1)
{
_month = 12;
_year--;
}
else
{
_month--;
}
_day = GetMonthDay(_year, _month);
}
else
{
_day -= day;
day = 0;
}
}
return (*this);
}
// 日期-天数
Date Date:: operator-(int day)
{
Date tmp(*this);
tmp -= day;
return tmp;
}
// 前置++
Date& Date::operator++()
{
return *this += 1;
}
// 后置++
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
// 后置--
Date Date::operator--(int)
{
return *this -= 1;
}
// 前置--
Date& Date::operator--()
{
Date tmp(*this);
*this -= 1;
return tmp;
}
// >运算符重载
bool Date::operator>(const Date& d)
{
if (_year < d._year)
{
return false;
}
else if (_year == d._year && _month < d._month)
{
return false;
}
else if (_year == d._year && _month == d._month && _day <= d._day)
{
return false;
}
else
{
return true;
}
}
// ==运算符重载
bool Date:: operator==(const Date& d)
{
if (_year == d._year && _month == d._month && _day == d._day)
{
return true;
}
else
{
return false;
}
}
// >=运算符重载
bool Date::operator >= (const Date& d)
{
if (!(*this < d))
{
return true;
}
else
{
return false;
}
}
// <运算符重载
bool Date::operator < (const Date& d)
{
if (!(*this > d || *this == d))
{
return true;
}
else
{
return false;
}
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{
if (!(*this > d))
{
return true;
}
else
{
return false;
}
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
if (!(*this == d))
{
return true;
}
else
{
return false;
}
}
// 日期-日期 返回天数
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
#include"Date.h"
void test1()
{
cout << "test1()" << endl;
Date d1;
d1.Print();
d1 += 100;
d1.Print();
Date d2 = d1 - 100;
d2.Print();
}
void test2()
{
cout << "test2()" << endl;
Date d1;
Date d2(d1);
d2.Print();
Date d3(d2 + 100);
d3.Print();
Date d4 =d3 - 100;
d4.Print();
}
void test3()
{
cout << "test3()" << endl;
Date d1(2023, 1, 1);
d1++;
d1.Print();
++d1;
d1.Print();
d1--;
d1.Print();
--d1;
d1.Print();
}
void test4()
{
cout << "test4()" << endl;
Date d1(2023, 2, 2);
Date d2(d1);
Date d3(d1 + 100);
cout << (d1 > d3) << endl;
cout << (d1 >= d3) << endl;
cout << (d1 == d3) << endl;
cout << (d1 <= d3) << endl;
cout << (d1 < d3) << endl;
cout << (d1 != d3) << endl << endl;
cout << (d1 > d2) << endl;
cout << (d1 >= d2) << endl;
cout << (d1 == d2) << endl;
cout << (d1 <= d2) << endl;
cout << (d1 < d2) << endl;
cout << (d1 != d2) << endl;
}
void test5()
{
cout << "test5()" << endl;
Date d1(2023, 1, 1);
Date d2(2000, 1, 1);
cout << (d1 - d2) << endl;
cout << (d2 - d1) << endl;
}
int main()
{
test1();
test2();
test3();
test4();
test5();
return 0;
}
运行测试
5个测试函数统一运行结果:
完结❀❀❀