简单实现一个Date类,主要是运算符的重载!
首先在Date类的实现过程中,需要注意以下几点:
日期+天数:
1.加的天数为负数的情况
2.加之后的天数超过当月最大天数 ,月数+1,天数-已经加过的天数
3.月数+1之后大于12 年数+1
日期-天数:
1.减的天数为负数的情况
2.减之后的天数超过当月最大天数 ,月-1,天数-已经加过的天数
3.月数-1之后小于1 年数-1
在Getmonthyear时对二月应注意特殊判断:
if (IsLeapYear() && month == 2)
{
array[2] = 29;
}
首先定义Date.h
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
//构造函数
Date(int year = 0, int month = 10, int day = 1);
//拷贝构造函数
Date(const Date& d);
//判断是否为闰年
bool IsLeapYear()const;
//运算符重载
Date& operator=(const Date& d);
Date operator+(int day); //日期加天数
Date operator-(int day); //日期减天数
Date& operator++(); //前置++
Date operator++(int); //后置++
Date& operator--(); //前置--
Date operator--(int); //后置--
//比大小
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;
private:
int _year;
int _month;
int _day;
}
在Date.cpp中实现函数重载
#include"Date.h"
#include<assert.h>
#include<iostream>
using namespace std;
Date::Date(int year, int month, int day)
:_year(year),
_month(month),
_day(day)
{
_year = year;
_month = month;
_day = day;
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
bool IsLeapYear()
{
int year;
assert(year > 0);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
}
return false;
}
int GetMonthYear(int year, int month)
{
assert(month < 0 && month>13);
int array[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (IsLeapYear() && month == 2)
{
array[2] = 29;
}
return array[month];
}
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
istream& operator>>(istream& _cin, Date& d)
{
_cin >> d._year;
_cin >> d._month;
_cin >> d._day;
return _cin;
}
Date& Date::operator=(const Date& d) //赋值
{
if (this != &d) {
_year = d._year;
_month = d._month;
d._day;
}
return *this;
}
bool Date::operator==(const Date& d)const
{
return this->_year == d._year&&this->_month == d._month&&this->_day == d._day;
}
bool Date::operator!=(const Date& d)const
{
return this->_year != d._year||this->_month != d._month||this->_day != d._day;
}
bool Date::operator>(const Date& d)const //大于
{
if ((this->_year > d._year)
||(this->_year==d._year&&this->_month>d._month)
|| (this->_year == d._year&&this->_month == d._month&&this->_day > d._day)) {
return true;
}
return false;
}
bool Date::operator>=(const Date& d)const
{
return operator>(d) || operator==(d);
}
bool Date::operator<(const Date& d)const ///小于
{
if ((this->_year < d._year)
|| (this->_year == d._year&&this->_month<d._month)
|| (this->_year == d._year&&this->_month == d._month&&this->_day < d._day)) {
return true;
}
return false;
}
bool Date::operator<=(const Date& d)const
{
return operator<(d) || operator==(d);
}
//1.加的天数为负数的情况
//2.加之后的天数超过当月最大天数 ,月+1,天数-已经加过的天数
//3.月数+1之后大于12 年数+1
//4.剩余天数>
Date Date::operator+(int day)
{
if (day < 0) {
return *this - (-day);
}
Date tmp = *this;
tmp._day += day;
while (tmp._day > GetMonthYear(tmp._year,tmp._month))
{
tmp._day -= GetMonthYear(tmp._year, tmp._month);
tmp._month += 1;
if (tmp._month == 13) {
tmp._month = 1;
tmp._year += 1;
}
}
return tmp;
}
Date Date::operator-(int day)
{
if (day < 0) {
return *this - (0 - day);
}
Date tmp = *this;
tmp._day -= day;
while (tmp._day > GetMonthYear(tmp._year, tmp._month))
{
tmp._day -= GetMonthYear(tmp._year, tmp._month);
tmp._month -= 1;
if (tmp._month == 0) {
tmp._month = 12;
tmp._year -= 1;
}
}
return tmp;
}
Date& Date::operator++() //前置++ this++
{
/*Date tmp = *this;
tmp._day += 1;
if (tmp._day > GetMonthYear(tmp._year, tmp._month) {
tmp._month += 1;
*/
*this = *this + 1;
return *this;
}
Date Date::operator++(int)
{
Date tmp = *this;
*this = *this + 1;
return tmp;
}
Date& Date::operator--() //前置-- this--
{
*this = *this - 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp = *this;
*this = *this - 1;
return tmp;
}
这时就基本实现了Date类的基本功能,后续会添加Time类,继续完善。