日期类的实现
Date.h文件
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1);
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;
bool operator!=(const Date& d)const;
//内联函数 GetMonthDay
int GetMonthDay(int year, int month)const
{
assert(month > 0 && month < 13);
static int monthdays[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 monthdays[month];
}
Date& operator+=(int day);
Date operator+(int day)const;
Date& operator-=(int day);
Date operator-(int day)const;
Date& operator++();
Date operator++(int);
Date operator--(int);
Date& operator--();
Date& operator=(const Date& d);
//d1-d2
int operator-(const Date&);
bool CheackInvalid()const;
void print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
//友元声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
Date.cpp文件
#include"Date.h"
//构造函数
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
//运算符重载
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 (_year == d._year)
{
if (_month < d._month)
{
return true;
}
}
else if (_month == d._month)
{
if (_day < d._day)
{
return true;
}
}
return false;
}
bool Date::operator<=(const Date& d)const
{
return *this < d || *this == d;
}
bool Date::operator>(const Date& d)const
{
return !(*this <= d);
}
bool Date::operator>=(const Date& d)const
{
return !(*this < d);
}
bool Date::operator!=(const Date& d)const
{
return !(*this == d);
}
Date& Date::operator+=(int 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)const
{
Date tmp = *this;//这个语句是拷贝构造
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date:: operator-(int day)const
{
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--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
Date& Date:: operator--()
{
*this -= 1;
return *this;
}
Date& Date:: operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//d1-d2
int Date:: operator-(const Date& d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
int falg = -1;
Date max = d;
Date min = *this;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
bool Date::CheackInvalid()const
{
if (_year <= 0
|| _month < 1 || _month>12
|| _day<1 || _day>GetMonthDay(_year, _month))
{
return false;
}
else
{
return true;
}
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
while (1)
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
if (!d.CheackInvalid())
{
cout << "输入错误,请重新输入" << endl;
}
else
{
break;
}
}
return in;
}
test.cpp文件
#include"Date.h"
int main()
{
Date d1(2024, 5, 18);
Date d2 = d1 + 30;
d1.print();
d2.print();
d1 += 20;
d1.print();
d1 -= 40;
d1.print();
Date d4(2024, 1, 29);
Date d5(2024, 8, 1);
cout << d5 - d4 << endl;
return 0;
}
测试结果