对于C++初学者,简单易懂,就不许要注释了。
#pragma once
#include<iostream>
using namespace std;
class Data
{
public:
/*Data(int year = 1990, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{
}*/
Data(int year, int month, int day)
{
if (year > 1989)
{
if (month > 0 && month < 13)
{
_year = year;
_month = month;
_day = _GetMonthDay(month);
}
else
{
_year = 1990;
_month = 1;
_day = 1;
}
}
}
Data(const Data& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//~Data()
//{}
Data& operator=(const Data& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
}
Data operator-(int days)
{
_day = _day - days;
return *this;
}
Data operator+(int days)
{
_day = _day + days;
return *this;
}
Data operator-(const Data& d)
{
_year = _year - d._year;
_month = _month - d._month;
_day = _day - d._day;
return *this;
}
Data& operator++()//前置
{
this->_day = this->_day + 1;
return *this;
}
Data operator++(int)//后置
{
Data tmp(*this);
this->_day = this->_day + 1;
return tmp;
}
Data& operator--()
{
this->_day = this->_day - 1;
return *this;
}
Data operator--(int)
{
Data tmp(*this);
this->_day = this-> _day - 1;
return tmp;
}
bool operator==(const Data& d)
{
if (_year == d._year &&
_month == d._month &&
_day == d._day)
{
return true;
}
else
{
return false;
}
}
bool operator!=(const Data& d)
{
if (_year == d._year &&
_month == d._month &&
_day == d._day)
{
return false;
}
else
return true;
}
bool operator<(const Data& d)
{
if (_year <= d._year)
{
if (_month <= d._month)
{
if (_day < d._day)
{
return true;
}
return false;
}
return false;
}
return false;
}
bool operator>(const Data& d)
{
if (_year >= d._year)
{
if (_month >= d._month)
{
if (_day > d._day)
{
return true;
}
return false;
}
return false;
}
return false;
}
bool operator<=(const Data& d)
{
if (_year <= d._year)
{
if (_month <= d._month)
{
if (_day <= d._day)
{
return true;
}
return false;
}
return false;
}
return false;
}
bool operator>=(const Data& d)
{
if (_year >= d._year)
{
if (_month >= d._month)
{
if (_day >= d._day)
{
return true;
}
return false;
}
return false;
}
return false;
}
friend ostream& operator<<(ostream& os, const Data& d);
friend bool IsLeap(const Data& d);
friend void PrintData(Data c);
friend int _GetMonthDay(int month);
private:
int _year;
int _month;
int _day;
//int _GetMonthDay(int month);
};
void PrintData(Data c)
{
cout << "Year: " << c._year << endl;
cout << "Month:" << c._month << endl;
cout << "Day: " << c._day << endl;
}
bool IsLeap(const Data& d)
{
if (d._year % 4 == 0)
{
if (d._year % 100 == 0)
{
if (d._year % 400 == 0)
{
return true;
}
return false;
}
return true;
}
return false;
}
ostream& operator<<(ostream& os, const Data& d)
{
os << d._year <<"-"<<d._month<<"-"<<d._day<< endl;
return os;
}
int _GetMonthDay(int month)
{
int array[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2)
{
int tmp = array[month] + 1;
return tmp;
}
else
return array[month];
}