目录
日期是日常生活中经常用到的东西,下面我将给大家展示一下如何用C++封装一个日期类,并且重载其各种操作
定义日期类
属性
日期有3个属性 年 月 日,还以若干个方法
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函
// d2(d1)
Date(const Date& d);
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=const Date& d);
// 析构函数
~Date();
// 日期+=天
Date& operator+=(int day);
// 日期+天数
Date operator+(int day);
// 日期-天数
Date operator-(int day);
// 日期-=天数
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
// >运算符重载
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);
// 日期-日期 返回天数
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
方法实现
获取某年某月的天数
int Date::GetMonthDay(int year,int month)
{
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
return 31;
}
if(month == 4 || month == 6 || month == 9 || month == 11)
{
return 30;
}
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
return 29;
}
return 28;
}
构造函数
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
拷贝构造函数
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
赋值运算符的重载
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
析构函数
Date::~Date()
{
_year = 1900;
_month = 1;
_day = 1;
}
日期+=天数
Date& Date::operator+=(int day)
{
_day += day;
while(_day >= GetMonthDay(_year,_month))
{
_day -= GetMonthDay(_year,_month);
_month++;
if(_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
日期+天数
Date Date::operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
日期-天数
Date Date::operator-(int day)
{
Date ret(*this);
ret -= day;
return ret;
}
日期-=天数
Date& Date::operator-=(int day)
{
if(day < _day)
{
_day -= day;
return *this;
}
day -= _day;
while(day != 0)
{
_month--;
if(_month == 0)
{
_month = 12;
_year--;
}
_day = GetMonthDay(_year, _month);
if(day < _day)
{
_day -= day;
day = 0;
}
else
{
day -= _day;
}
}
return *this;
}
前置++
Date& Date::operator++()
{
_day++;
if(_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year,_month);
_month++;
if(_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
后置++
Date Date::operator++(int)
{
Date ret(*this);
operator++();
return ret;
}
后置--
Date Date::operator--(int)
{
Date ret(*this);
operator--();
return ret;
}
前置--
Date& Date::operator--()
{
_day--;
if(_day == 0)
{
_month--;
if(_month == 0)
{
_month = 12;
_year--;
}
_day = GetMonthDay(_year,_month);
}
return *this;
}
>运算符重载
bool Date::operator>(const Date& d)
{
return *this - d > 0;
}
==运算符重载
bool Date::operator==(const Date& d)
{
return *this - d == 0;
}
>=运算符重载
bool Date::operator>=(const Date& d)
{
return *this - d >= 0;
}
<运算符重载
bool Date::operator<(const Date& d)
{
return *this - d < 0;
}
<运算符重载
bool Date::operator<=(const Date& d)
{
return *this - d <= 0;
}
!=运算符重载
bool Date::operator!=(const Date& d)
{
return *this - d != 0;
}
日期-日期 返回天数
int Date::operator-(const Date& d)
{
int day = 0;
//获取当前月份的相差的日期值
int day1 = _day;
int day2 = d._day;
int m1 = _month - 1;
int m2 = d._month - 1;
while(m1 > 0)
{
day1 += GetMonthDay(_year, m1);
m1--;
}
while(m2 > 0)
{
day2 += GetMonthDay(d._year, m2);
m2--;
}
day = day1 - day2; //当前月份与日期之间相隔的日期数
//判断年份之间相差的日期数
int y1 = _year;
int y2 = d._year;
while(y2 < y1)
{
day += 365;
if(y2 % 4 == 0 && y2 % 100 != 0 || y2 % 400 == 0)
{
day++;
}
y2++;
}
while(y1 < y2)
{
day -= 365;
if(y1 % 4 == 0 && y1 % 100 != 0 || y1 % 400 == 0)
{
day--;
}
}
return day;
}
整体代码展示
#include <iostream>
using namespace std;
class Date
{
public:
//获取某年某月的天数
int GetMonthDay(int year, int month);
//全缺省构造函数
Date(int year = 1900, int month = 1, int day = 1);
//拷贝构造函数
Date(const Date &d);
//赋值运算符重载
Date &operator=(const Date &d);
//析构函数
~Date();
//日期+=天数
Date &operator+=(int day);
//日期+天数
Date operator+(int day);
//日期-天数
Date operator-(int day);
// 日期-=天数
Date &operator-=(int day);
// 前置++
Date &operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date &operator--();
// >运算符重载
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);
// 日期-日期 返回天数
int operator-(const Date &d);
private:
int _year;
int _month;
int _day;
};
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
int Date::GetMonthDay(int year,int month)
{
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
return 31;
}
if(month == 4 || month == 6 || month == 9 || month == 11)
{
return 30;
}
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
return 29;
}
return 28;
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
Date::~Date()
{
_year = 1900;
_month = 1;
_day = 1;
}
Date& Date::operator+=(int day)
{
_day += day;
while(_day >= GetMonthDay(_year,_month))
{
_day -= GetMonthDay(_year,_month);
_month++;
if(_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
Date Date::operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
Date Date::operator-(int day)
{
Date ret(*this);
ret -= day;
return ret;
}
Date& Date::operator-=(int day)
{
if(day < _day)
{
_day -= day;
return *this;
}
day -= _day;
while(day != 0)
{
_month--;
if(_month == 0)
{
_month = 12;
_year--;
}
_day = GetMonthDay(_year, _month);
if(day < _day)
{
_day -= day;
day = 0;
}
else
{
day -= _day;
}
}
return *this;
}
Date& Date::operator++()
{
_day++;
if(_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year,_month);
_month++;
if(_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
Date Date::operator++(int)
{
Date ret(*this);
operator++();
return ret;
}
Date& Date::operator--()
{
_day--;
if(_day == 0)
{
_month--;
if(_month == 0)
{
_month = 12;
_year--;
}
_day = GetMonthDay(_year,_month);
}
return *this;
}
Date Date::operator--(int)
{
Date ret(*this);
operator--();
return ret;
}
bool Date::operator>(const Date& d)
{
return *this - d > 0;
}
bool Date::operator==(const Date& d)
{
return *this - d == 0;
}
bool Date::operator>=(const Date& d)
{
return *this - d >= 0;
}
bool Date::operator<(const Date& d)
{
return *this - d < 0;
}
bool Date::operator<=(const Date& d)
{
return *this - d <= 0;
}
bool Date::operator!=(const Date& d)
{
return *this - d != 0;
}
int Date::operator-(const Date& d)
{
int day = 0;
//获取当前月份的相差的日期值
int day1 = _day;
int day2 = d._day;
int m1 = _month - 1;
int m2 = d._month - 1;
while(m1 > 0)
{
day1 += GetMonthDay(_year, m1);
m1--;
}
while(m2 > 0)
{
day2 += GetMonthDay(d._year, m2);
m2--;
}
day = day1 - day2; //当前月份与日期之间相隔的日期数
//判断年份之间相差的日期数
int y1 = _year;
int y2 = d._year;
while(y2 < y1)
{
day += 365;
if(y2 % 4 == 0 && y2 % 100 != 0 || y2 % 400 == 0)
{
day++;
}
y2++;
}
while(y1 < y2)
{
day -= 365;
if(y1 % 4 == 0 && y1 % 100 != 0 || y1 % 400 == 0)
{
day--;
}
}
return day;
}