Date 日期类的实现(c++)
本文用c++实现日期类
将会实现以下函数
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=(const Date& d);Date& operator+=( int day);Date operator+(int day);Date operator-(int day);Date& operator-=( int day);Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);int operator-(const Date& d);
先放完整代码:
Date.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;class Date
{
public://日期类的拷贝构造赋值和析构都是不用写的Date(int year = 1900, int month = 1, int day = 1){_year = year;_month = month;_day = day;}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=(const Date& d);int GetMonthDay(int year, int month)//写在类中的本质就是inline{assert(month > 0 && month < 13);static int monthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//放在静态区,避免每次调用这个函数时,频繁创建这个数组if (month == 2){//四年一闰,百年不润,400润if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}}return monthDays[month];}//d1+100(日期加整数)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--(int);int operator-(const Date& d);void Print(){cout << _year << "/" << _month << "/" << _day << endl;}private:int _year;int _month;int _day;
};
Date.cpp
#include"Date.h"bool Date::operator==(const Date& d)
{return _day == d._day&& _month == d._month&& _year == d._year;
}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){return _day < d._day;}}return false;
}Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}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 !(*this < d);
}bool Date::operator!=(const Date& d)
{return !(*this == d);
}
//这里实现的是+=因为原值被修改
Date& Date::operator+=( int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}return *this;
}//在这里创建一个临时对象,作为返回值,出作用域就被销毁
Date Date::operator+(int day)
{Date tmp(*this);//Date tmp = *this;这里是拷贝构造/*tmp._day += day;while (tmp._day > GetMonthDay(tmp._year, tmp._month)){tmp._day -= GetMonthDay(tmp._year, tmp._month);++tmp._month;if (tmp._month == 13){++tmp._year;tmp._month = 1;}}*/tmp += day;return tmp;//除了作用域这个对象还在,才能用引用返回,否则,不能使用
}//Date Date::operator+(int day)
//{
// Date tmp(*this);//Date tmp = *this;这里是拷贝构造
// tmp._day += day;
// while (tmp._day > GetMonthDay(tmp._year, tmp._month))
// {
// tmp._day -= GetMonthDay(tmp._year, tmp._month);
// ++tmp._month;
// if (tmp._month == 13)
// {
// ++tmp._year;
// tmp._month = 1;
// }
// }
//
// return tmp;//除了作用域这个对象还在,才能用引用返回,否则,不能使用
//}
//
//Date& Date::operator+=( int day)
//{
// *this = *this + day;
//
// return *this;
//}
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-( int day)
{Date tmp(*this);tmp -= day;return tmp;
}
//++d ->d.operator++()
Date& Date::operator++()
{*this += 1;return *this;
}//d++,返回++前的值 ->d.operator++(0)(传啥都行,只是为了匹配int)
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}
Date& Date::operator++()
{*this -= 1;return *this;
}//d++,返回++前的值 ->d.operator++(0)(传啥都行,只是为了匹配int)
Date Date::operator++(int)
{Date tmp(*this);*this -= 1;return tmp;
}// d1 - d2
int Date::operator-(const Date& d)
{int flag = 1;Date max = *this;Date min = d;if (*this < d){int flag = -1;max = d;min = *this;}int n = 0;while (min != max){++min;++n;}return n * flag;
}
需要重点讲解的没啥,主要是:
要明白这个:
以及,贯彻全程序的函数
int GetMonthDay(int year, int month)//写在类中的本质就是inline{assert(month > 0 && month < 13);static int monthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };/*放在静态区,避免每次调用这个函数时,频繁创建这个数组 *///四年一闰,百年不润,400润if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}return monthDays[month];}