date类实现操作符重载

日期类对操作符重载巩固
data.h

#ifndef  _data_h
#define _data_h
class Date
{
private:
    int _year;
    int _month;
    int _day;
public:
    //默认成员函数
    Date(int year = 1900, int month = 1, int day = 1);//缺省构造函数
    Date(const Date& c);//拷贝构造函数
    Date& operator=(const Date& c);//赋值操作符重载
    ~Date();//析构函数
    void display();
    int  GetMonthDay(int year, int month);//获得当月天数
    friend ostream & operator<<(ostream& out, Date& d);//重载<<
    friend istream & operator>>(istream& in, Date& d);//重载>>
    Date operator+(int day);//日期对象加上一个天数
    Date operator-(int day);//日期对象减上一个天数
    int  operator-(Date  d);//两个日期对象相减求天数
    Date& operator++();//日期前置++
    Date operator++(int);//日期后置++
    Date AddOne(const Date& d);//日期加一天
    Date& operator+=(int day);//日期对象加等
    Date& operator-=(int day);//日期对象减等

    bool operator<(const Date& d);//比较*this对象是否比d日期对象小
    bool operator>(const Date& d);//比较*this对象是否比d日期对象大
    bool operator==(const Date& d);//比较*this对象与d日期对象是否相等
    bool operator<=(const Date& d);//比较*this对象是否比d日期对象小于等于
    bool operator>=(const Date& d);//比较*this对象是否比d日期对象大于等于
};

#endif
=========
#include <iostream>
#include "data.h"
using std::ostream;
using std::istream;
using std::endl;
using std::cout;

Date::Date(int year , int month , int day)//默认参数在头文件中定义一次就好了,无需再定文件中再写一次
{
    if (year<1900)
    {
        cout << "year error" << endl;
        return;
    }
    if (month <1 && month > 12)
    {
        cout << "month error" << endl;
        return;
    }
    if (day<1 || day>GetMonthDay(year, month))
    {
        cout << "day error" << endl;
        return;
    }
    //已确认日期有效
    _year = year;
    _month = month;
    _day = day;
    //cout << "构造函数" << endl;
}
Date::Date(const Date& c)
{
    _year = c._year;
    _month = c._month;
    _day = c._day;
    //cout << "拷贝构造" << endl;
}
Date& Date::operator=(const Date& c)//&表引用
{
    if (this != &c)//&表取地址
    {
        _year = c._year;
        _month = c._month;
        _day = c._day;
    }
    //cout << "赋值" << endl;
    return *this;
}
Date:: ~Date()
{
    //cout << "析构函数" << endl;
}
void Date::display()
{
    cout << _year << "-" << _month << "-" << _day << endl;
}
ostream& operator<<(ostream& out, Date& d)
{
    out << d._year << d._month << d._day;
    return out;
}
istream& operator>>(istream& in, Date& d)
{
    in >> d._year >> d._month >> d._day;
    return in;
}
int  GetMonthDay(int year, int month)
{
    int monthDay[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if ((year % 400 == 0 || year % 4 == 0 && year % 100 != 0) && month == 2)//是瑞年且2月
    {
        return monthDay[month - 1] + 1;
    }
    else
        return  monthDay[month - 1];
}
Date Date::operator+(int day)//加类域::
{
    Date tmp(*this);
    if (day < 0)
    {
        return tmp - (-day);//加一个负天等于减一个正天
    }
    tmp._day += day;
    while (tmp._day>GetMonthDay(tmp._year, tmp._month))//注意应是tmp对象和tmp._day比较
    {
        tmp._day -= GetMonthDay(tmp._year, tmp._month);
        if (tmp._month == 12)
        {
            ++tmp._year;
            tmp._month = 1;
        }
        else
        {
            ++tmp._month;
        }
    }
    return tmp;
}
Date Date:: operator-(int day)
{
    Date tmp(*this);
    if (day < 0)
    {
        return tmp + (-day);//加一个负天等于减一个正天
    }
    tmp._day -= day;
    while (tmp._day<1)//注意边界条件,一个月最小天数为1
    {
        if (tmp._month == 1)
        {
            --tmp._year;
            tmp._month = 12;
        }
        else
        {
            --tmp._month;
        }
        tmp._day += GetMonthDay(tmp._year, tmp._month);
    }
    return tmp;
}
int Date:: operator-(Date  d)
{
    int count = 0;
    int flag = -1;
    Date *pMax = &d;
    Date *pMin = this;
    Date tmp(*this);
    if (*this > d)
    {
        flag = 1;
        swap(pMax, pMin);
    }
    while (*pMin < *pMax)
    {
        ++(*pMin);
        ++count;
    }
    if (flag == -1)
        *this = tmp;//因为已修改this,需改回去
    return flag*count;
}
Date& Date::operator++()
{
    *this = *this + 1;
    return *this;
}
Date Date:: operator++(int)
{
    Date tmp(*this);
    *this = *this + 1;
    return tmp;
}
Date AddOne(const Date& d)
{
    Date tmp(d);
    tmp = tmp + 1;
    return tmp;
}
Date& Date::operator+=(int day)
{
    *this = *this + day;
    return *this;
}
Date& Date::operator-=(int day)
{
    *this = *this - day;
    return *this;
}

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;
}
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;
}
bool Date::operator<=(const Date& d)
{
    return (*this < d) || (*this == d);
}
bool Date::operator>=(const Date& d)
{
    return !(*this < d);
}
========
Testc.c
#include <iostream>
#include "data.h"
using namespace std;

void Test1()
{
    Date a(1993, 5, 6);
    Date b = a + 1000;
    b.display();
}


int main()
{
    Test1();
    system("pause");
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值