日期类C++实现
类的主要内容
1.成员变量:年 year,月month,日day
2.友元,重载实现本类的输出打印
3.isValid() 在构造函数中判断日期是否有效
4.在日期上往前往后添加一定的天数
5.计算两短日期的差值
类定义 date.h
#ifndef date_H
#define date_H
#include "iostream"
class date
{
public:
date(int _year = 2000, int _month=1, int _day=1);
~date(){}
friend std::ostream& operator <<(std::ostream &out,date &d);
bool isValid() const;
int getDaysOfMonth(const int &_year, const int &_month) const;
bool isLeapYear(const int &_year) const;
void show()const;
bool operator == (const date &d)const { return day == d.day && month == d.month && year == d.year; }
bool operator != (const date &d)const { return day != d.day || month != d.month || year != d.year; }
bool operator < (const date& d)const; //在.cpp具体实现
bool operator > (const date& d)const { return !(*this < d) && (*this != d); }
bool operator <= (const date& d)const { return !(*this > d); }
bool operator >= (const date& d)const { return !(*this < d); }
date& operator = (const date &d) { if(*this != d) {year = d.year; month = d.month;day = d.day;} return *this;}
date operator + (const int &days); //在.cpp具体实现
date& operator += (const int &days) { *this = *this + days; return *this; }
date operator - (const int &days); //在.cpp具体实现
date& operator -= (const int &days) { *this = *this - days; return *this; }
date& operator++() { return *this +=1; }
date operator++(int) { date tmp(*this); *this += 1; return tmp; }
date& operator--() { return *this -=1; }
date operator--(int) { date tmp(*this); *this -= 1; return tmp; }
int operator - (const date &d)const;
protected:
int year;
int month;
int day;
};
std::ostream& operator << (std::ostream &cout,date &d);
#endif // date_H
类实现 data.cpp
#include "date.h"
#include "assert.h"
#include "iomanip"
date::date(int _year, int _month, int _day):
year(_year),
month(_month),
day(_day)
{
if(!isValid())
assert(false);
}
bool date::isValid() const
{
return year > 0 &&
month > 0 && month <= 12 &&
day > 0 && day <= getDaysOfMonth(year, month);
}
int date::getDaysOfMonth(const int &_year, const int &_month) const
{
int arr[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(_year) && _month == 2)
++arr[2];
return arr[_month];
}
bool date::isLeapYear(const int &_year) const
{
return (_year%4 == 0 && _year%100 != 0)|| _year%400 == 0;
}
void date::show() const
{
std::cout << std::setw(4) << std::setfill('0') << year << "-"
<< std::setw(2) << std::setfill('0') << month << "-"
<< std::setw(2) << std::setfill('0') << day << std::endl;
}
/***************重载运算符**************/
bool date::operator < (const date& d)const
{
if((year < d.year) ||
(year == d.year && month < d.month) ||
(year == d.year && month == d.month && day < d.day))
return true;
return false;
}
date date::operator + (const int &days)
{
if(day < 0)
return (*this) - (-days);
date d(*this);
d.day += days;
while(d.isValid() == false)
{
//减去这个月的天数,月份加一
d.day -= getDaysOfMonth(d.year,d.month);
++d.month;
if(d.month > 12){
d.month = 1;
++d.year;
}
}
return d;
}
date date::operator - (const int &days)
{
if(day < 0)
return (*this) + (-days);
date d(*this);
d.day -= days;
while(d.isValid() == false)
{
//加上上个月的天数,月份减一
d.day += getDaysOfMonth(d.year,d.month -1);
--d.month;
if(d.month < 1){
d.month = 12;
--d.year;
}
}
return d;
}
int date::operator - (const date &d)const
{
int count = 0;
int sign = 1;
date max(*this),min(d);
if(*this < d){
max = d;
min = *this;
sign = -1;
}
while (max != min) {
min++;
count ++;
}
return count * sign;
}
/***************重载输出运算符 << **************/
std::ostream& operator << (std::ostream &cout,date &d)
{
cout << std::setw(4) << std::setfill('0') << d.year << "-"
<< std::setw(2) << std::setfill('0') << d.month << "-"
<< std::setw(2) << std::setfill('0') << d.day;
return cout;
}
代码有改进地方请及时指正,谢谢
个人邮箱:1484816616@qq.com