日期类是一种很常用的类,但是C++中没有封装,因此就要我们手动封装。
创建类一定要生成其默认的成员函数:构造函数,拷贝构造函数,运算符重载,析构函数
本文实现的日期类有以下功能:
1.判断两个日期的大小关系
2.日期加减天数
3.日期加等减等天数
4.日期的前置后置自加和自减
5.两个日期相差天数
6.输出日期
日期类的声明:
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
//构造
Date(int year = 2019, int month = 4, int day = 20)
{
if (year >= 0 && month > 0 && month < 13 && day>0 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "Invalid Date" << endl;
assert(0);
}
}
//拷贝构造
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//赋值运算符重载
Date& operator=(const Date& d)
{
if (this != &d)//不是自己给自己赋值
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//析构
~Date()
{
if (this)
{
_year = 0;
_month = 0;
_day = 0;
}
}
public:
//获取月为month的天数
inline int GetMonthDay(int year, int month);
//重载比较类运算符
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-(const Date& d);//日期之差
Date& operator++();//++d ==>d.operator(&d)
Date& operator--();
Date operator++(int);//d++ ==>d.operator(&d,0)
Date operator--(int);
void print();
//重载输入输出流
friend std::ostream& operator<<(std::ostream& _cout, const Date& d);
friend std::istream& operator>>(std::istream& _cin, Date& d);
private:
int _year;//年
int _month;//月
int _day;//日
};
日期类的定义:
#include"Date.h"
int Date::GetMonthDay(int year, int month)
{
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];
}
//重载比较类运算符
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;
}
}
else
{
return false;
}
}
bool Date::operator<(const Date& d)
{
return !(*this >= d);
}
bool Date::operator>=(const Date& d)
{
return *this > d|| *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;
}
//d1!=d2
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
//2019/4/20+20
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;
if (ret._month == 13)
{
++ret._year;
ret._month = 1;
}
}
return ret;//不能传引用
}
//d1+=10
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;
//复用+
/**this = *this + day;
return *this;*/
}
//d1-10
Date Date::operator-(int day)
{
Date ret(*this);
ret -= day;
return ret;
}
//2019/4/2-=10
Date& Date::operator-=(int day)
{
if (day < 0)
return *this += (-day);
_day -= day;
while (_day <= 0)
{
--_month;
_day += GetMonthDay(_year, _month);//31-8
if (_month == 0)
{
--_year;
_month = 12;
}
}
return *this;
}
//2019/4/2-2018/2/13日期之差
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (max < min)
{
flag = -1;
max = d;
min = *this;
}
int count = 0;
while (min < max)
{
++count;
++min;
}
return flag * count;
}
//前置++d ==>d.operator(&d)
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//后置d++ ==>d.operator(&d,0)
Date Date::operator++(int)
{
Date ret(*this);
*this += 1;
return ret;
}
Date Date::operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
void Date::print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
std::ostream& operator<<(std::ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day << endl;
return _cout;
}
std::istream& operator>>(std::istream& _cin, Date& d)
{
_cin >> d._year;
_cin >> d._month;
_cin >> d._day;
return _cin;
}