c++日期类的实现级运算符的重载

    今天写了简单的日期类,有兴趣的可以看一下

    其声明Date.h:

#define _CRT_SECURE_NO_WARNINGS 1

#ifndef   __ONCEDATE__

#define   __ONCEDATE__

#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 1900, int mouth = 1, int day = 1);//构造函数
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);
Date operator++();
Date operator++(int);
Date operator--();
Date operator--(int);
void display();//打印函数,野可以重载cout
int operator-(const Date& d);//代码中没有实现,有兴趣的可以试一下
private:
bool IsLeapYear(int year);//判断闰年的私有类函数
int GetMonthDay(int year, int month);//获取月份的私有类函数
int _year;
int _mouth;
int _day;
};
#endif

定义如下:

#include <iostream>
#include "Date.h"
using namespace std;
 void Date  :: display()
{
cout << _year << " " << _mouth << " "
<< _day << endl;
}
 Date::Date(int year , int mouth, int day )
 {
 if ((year >= 0) &&
 (mouth > 0 && mouth<13) &&
 (day>0 && day <= GetMonthDay(year, mouth)))
 {
 _year = year;
 _mouth = mouth;
 _day = day;
 }
 else
 {
 cout << "日期错误,改为默认日期" << endl;
 _year = 1900;
 _mouth = 1;
 _day = 1;
 }
 }
bool Date:: operator == (const Date& d)
 {
 return this->_year == d._year
 && this->_mouth == d._mouth
 && this->_day == d._day;
 }
bool Date:: operator <(const Date& d)
{
if (_year < d._year)
return true;
if (_year == d._year)
{
if (_mouth == d._mouth)
{
if (_day == d._day)
{
return false;
}
else if (_day < d._day)
return true;
}
else if (_mouth < d._mouth)
return true;
}
return false;
}
bool Date:: operator <=(const Date& d)
{
if (*this<d || *this == d)
return true;
return false;
}
bool Date:: operator >(const Date& d)
{
if (!(*this <= d))
return true;
return false;
}
bool Date:: operator >=(const Date& d)
{
if (!(*this < d))
return true;
return false;
}
bool Date:: operator !=(const Date& d)
{
return !(*this == d);
}
Date Date:: operator+ (int day)//日期加天数,因为定义的天数为int型,所以可能加一个负的天数
{   //故在循环中添加了判断处理,若负责执行减操作,反之执行加操作
Date DestDate(*this);
DestDate._day += day;
while (DestDate._day > GetMonthDay(DestDate._year, DestDate._mouth) ||
DestDate._day <= 0)
{
if (DestDate._day>0)
{
DestDate._day -= GetMonthDay(DestDate._year, DestDate._mouth);
if (DestDate._mouth == 12)
{
DestDate._mouth = 0;
DestDate._year++;
}
DestDate._mouth++;
}
else
{
DestDate._day += GetMonthDay(DestDate._year, DestDate._mouth);
if (DestDate._mouth == 1)
{
DestDate._mouth = 13;
DestDate._year--;
}
DestDate._mouth--;
}
}
return DestDate;
}
Date Date:: operator+= (int day)
{
*this = *this + day;
return *this;
}
Date Date:: operator- (int day)
{
day = -day;
return *this + day;
}
Date Date:: operator-= (int day)
{
*this = *this - day;
return *this;
}
Date Date:: operator++()
{
Date DestDate(*this);
*this += 1;
return DestDate;
}
Date Date:: operator++(int)
{
*this += 1;
return *this;
}
Date Date:: operator--()
{
Date Destdate(*this);
--*this;
return Destdate;
}
Date Date:: operator--(int)
{
*this -= 1;
return *this;
}
int Date:: operator-(const Date& d)//日期-日期的实现主要先比较日期的大小,再用计数器
{   //统计小日期加多少天月大日期变为同一日期,最后输出结果
Date high(d);
Date low;
int count = 0;
int flag = 1;
if (*this > d)
{
low = high;
high = *this;
}
else
{
flag = 0;
low = *this;
}
while (low != high)
{
low++;
count++;
}
if (!flag)
count = -count;
return count;
}
bool Date::IsLeapYear(int year)
{
if (((year % 4 == 0) && (year % 100)) ||
(year % 400 == 0))
{
return true;
}
return false;
}
int Date::GetMonthDay(int year, int month)//用一个数组存储1-12月所占天数
{
int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = monthArray[month];
if (month == 2 && IsLeapYear(year))
{
day += 1;
}
return day;
}

    如有不足之处希望批评指正,如有疑惑野可以留言探讨

本文出自 “pawnsir的IT之路” 博客,请务必保留此出处http://10743407.blog.51cto.com/10733407/1736461

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值