知识点23

前置++和后置++重载:

class Date
{
public:
    //......

    Date& operator++()    //前置++
    {
        _day += 1;
        while(_day > GetMonthDay(_year, _month))
        {
            _day -= GetMonthDay(_year, _month);
            ++month;
            if(_month = 13)
            {
                ++_year;
                _month = 1;
            }
        }
    return *this;
    }

    Date operator++(int)    //后置++,增加参数是为了占位,与前置++构成重载
    {
        Date temp = *this;
        ++*this;
        return temp;    
    }

    //......
};

注意:前置++返回+1之后的结果,this指针指向的对象函数结束后不会销毁,故可以用引用返回提高效率;后置++返回+1之前结果,需要保存旧值,temp是临时对象,因此只能以值的形式返回。

知识点24

const 成员:将 const 修饰的“成员函数”称之为 const 成员函数,const 修饰类成员函数,实际修饰该成员函数隐含的 this 指针,表明在成员函数中不能对任何成员函数进行修改。由于 this 指针不能显式写出,所以 const 就加在函数之后来表示修饰。

class Date
{
public:
    void Print() const
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

    //......
};

成员函数后面加 const 后普通和 const 对象都可以调用,因此只要成员函数内部不修改成员变量都可以加上。

知识点25

取地址及 const 取地址操作符重载:这两个默认成员一般不需要重新定义,编译器会默认生成。

class Date
{
public:
    Date* operator&()
    {
        return this;
    }

    const Date* operator&() const
    {
        return this;
    }

    //......

};

当我们对普通对象使用&时,会调用普通版本的取址运算符;而对const对象使用&时,会调用const版本的取址运算符。

日期类的实现:

//Date.h

#include<iostream>
using namespace std;


class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1);
    
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl; 
    }

    Date(const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

    bool operator==(const Date& x);
    bool operator<(const Date& x);
    bool operator<=(const Date& x);
    bool operator>(const Date& x);
    bool operator>=(const Date& x);
    bool operator!=(const Date& x);

    int GetMonthDay(int year, int month);

    Date& operator+=(int day);
    Date operator+(int day);

    Date& operator++();
    Date operator++(int);

private:
    int _year;
    int _month;
    int _day;

};
//Date.cpp

#include 'Date.h'

Date::DAte(int year, int month, int day)
{
    _year = year;
    _month = month;
    _day = day;
}

bool Date::operator==(const Date& x)
{
    return _year == x.year
        && _month == x.moth
        && _day == x._day;
}

bool Date::operator<(const Date& x)
{
    if(_year < x._year)
    {
        return ture;
    }
    else if(_year == x._year && _month < x._month)
    {
        return ture;
    }
    else if(_year == x._year && _month == x._month && _day < x._day)
    {
        return ture;
    }

    return false;
}

bool Date::operator<=(const Date& x)
{
    return *this < x || *this == x;
}

bool Date::operator>(const Date& x)
{
    return !(*this <= x);
}

bool Date::operator>=(const Date& x)
{
    return !(*this < x);
}

bool Date::operator!=(const Date& x)
{
    return !(*this == x);
}

int Date::GetMonthDay(int year, int month)
{
    static int daysArr[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if(month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    {
        return 29;
    }
    else
    {
        return daysArr[month];
    }
}

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)
{
    *this = tmp;
    tmp += day;
    return tmp;
    /* tmp._day += day;
    while(tmp._day > GetMonthDay(tmp._year, tmp._month))
    {
        tmp._day -= day;
        ++tmp._month;
        if(tmp._month == 13)
        {
            ++tmp._year;
            tmp._month = 1; 
        }
    }       
    return tmp;
    */
}

//前置++
Date& Date::operator++()
{
    *this += 1;
    return *this;
}

//后置++
Date Date::operator++(int)
{
    Date tmp = *this;
    *this += 1;

    return tmp;
}

更多推荐