头文件(.h)
#pragma once
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1);
// Date(const Date& d);
// Date& operator=(const Date& d);
// ~Date();
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);
Date operator+(int day);
Date& operator+=(int day);
Date operator-(int day);
Date& operator-=(int day);
int operator-( Date& d);
Date operator++(); // ++d => d.operator++(&d)
Date operator++(int); // d++ => d.operator(&d, 0);
Date operator--(); // --d
Date operator--(int); // d--
int GetMonthDay(int year, int month);
void Display();
friend istream& operator>>(istream& is, Date &d);
friend ostream& operator<<(ostream& os, const Date &d);
private:
int _year;
int _month;
int _day;
};
Date函数(主函数)
#include"Date.h"
#include<iostream>
using namespace std;
int Date::GetMonthDay(int year, int month)
{
static const 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;
}
return Monthday[month];
}
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
{
//assert(false);
std::cout << "Date Invalid" << std::endl;
}
}
bool Date::operator>(const Date& d)
{
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)
{
return !(*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 (_year == d._year) &&
(_month == d._month)&&
(_day == d._day);
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
Date Date::operator+(int day)
{
Date ret(*this);
ret._day += day;
while (ret._day > GetMonthDay(ret._year, ret._month))
{
ret._day -= GetMonthDay(ret._year, ret._month);
ret._month++;
while (ret._month > 12)
{
ret._month -= 12;
ret._year++;
}
}
return ret;
}
Date& Date::operator+=(int day)
{
(*this) = (*this) + day;
return *this;
}
Date Date::operator-(int day)
{
Date ret(*this);
ret._day -= day;
while (ret._day <= 0)
{
ret._day += GetMonthDay(ret._year, ret._month);
ret._month--;
while (ret._month < 1)
{
ret._year--;
ret._month += 12;
}
}
return ret;
}
Date& Date::operator-=(int day)
{
(*this) =(*this)- day;
return *this;
}
int Date::operator-( Date& d)
{
int day = 0;
while (!(*this == d))
{
if (*this > d)
{
d++;
day++;
}
else
{
d--;
day--;
}
}
return day;
}
Date Date::operator++() // ++d => d.operator++(&d)
{
(*this)++;
return *this;
}
Date Date::operator++(int)// d++ => d.operator(&d, 0);
{
Date ret(*this);
(*this) += 1;
return ret;
}
Date Date::operator--() // --d
{
(*this)--;
return *this;
}
Date Date::operator--(int) // d--
{
Date ret(*this);
(*this) -= 1;
return ret;
}
istream& operator>>(istream& is, Date &d)
{
cout << "请输入一个日期" << endl;
is >> d._year >> d._month >> d._day;
return is;
}
ostream& operator<<(ostream& os, const Date &d)
{
cout << d._year << "-" << d._month << "-" << d._day << endl;
return os;
}
void Date::Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
main函数(测试函数)
#include "Date.h"
#include<iostream>
using namespace std;
int main()
{
Date d1(2018, 10, 30),d2;
d2 = d1++;
cout << d2<< endl;
return 0;
}
注意:测试代码中的cout函数要重载必须重载