【C++】类与对象(日期计算器)

简介: 【C++】类与对象(日期计算器)

头文件

#include<assert.h>
using namespace std;

class Date
{
public:
    bool CheckInvalid() const;
    Date(int year = 1, int month = 1, int day = 1);
    bool operator<(const Date& d) const;
    bool operator<=(const Date& d) const;
    bool operator>(const Date& d) const;
    bool operator>=(const Date& d) const;
    bool operator==(const Date& d) const;
    bool operator!=(const Date& d) const;

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

    Date operator-(int day) const;
    Date& operator-=(int day);

    //++d1
    Date& operator++();

    //为了跟前置++区分,强行增加一个int形参,构成重载区分
    //d1++
    Date operator++(int);

    //日期-日期
    int operator-(const Date& d) const;

    //如果不声明和定义分离,本质就是内联
    int GetMonthDay(int year, int month) const
    {
        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&&((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) )
        {
            return 29;
        }

        return monthDays[month];
    }

    void Print() const
    {
        cout << _year << "/" << _month << "/" << _day << endl;
    }

    //void operator<<(ostream& out)
    //{
    //    out << _year << "年" << _month << "月" << _day << "日" << endl;
    //}

    //友元声明
    friend ostream& operator<<(ostream& out, const Date& d);
    friend istream& operator>>(istream& in, Date& d);

private:
    int _year;
    int _month;
    int _day;
};

ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in,  Date& d);

声明流插入<<和流提取>>时,应在类外面声明,不然this指针会占据第一个形参,Date就只能是左操作数了,打印时就会变成d1<<cout; ,不符合常规写法。在类外面声明时,为了不把private打开,可以进行友元声明,就可以在不打开private的情况下,访问private。

函数实现


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

    if (!CheckInvalid())
    {
        cout << "构造日期非法" << endl;
    }
}

bool Date::operator<(const Date& d) const
{
    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) const
{
    return *this < d || *this == d;
}

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

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

bool Date::operator==(const Date& d) const
{
    return _year == d._year
        && _month == d._month
        && _day == d._day;
}
bool Date::operator!=(const Date& d) const
{
    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) const
{
    //Date tmp(*this);
    Date tmp = *this;  //拷贝构造
    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) const
{
    Date tmp = *this;
    tmp -= day;
    return tmp;
}

Date& Date::operator-=(int day)
{
    _day -= day;
    while(_day < 0)
    {
        --_month;
        if (_month == 0)
        {
            --_year;
            _month = 12;
        }
        _day += GetMonthDay(_year, _month);
    }
    return *this;
}

//++d  ->d.operator++()  编译器自动转换
Date& Date::operator++()
{
    *this += 1;
    return *this;
}

//d++  ->d.operator(0)   C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递
Date Date::operator++(int)  //规定只能是int,形参名字可以不写
{
    Date tmp = *this;
    *this += 1;
    return tmp;
}

int Date::operator-(const Date& d) const
{
    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;
}

bool Date::CheckInvalid() const
{
    if (_year <= 0
        || _month < 1 || _month>12
        || _day<1 || _day>GetMonthDay(_year, _month))
    {
        return false;
    }
    else
    {
        return true;
    }
}

ostream& operator<<(ostream& out, const Date& d)
{
    out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    return out;
}

istream& operator>>(istream& in, Date& d)
{
    while (1)
    {
        cout << "请输入年月日:" << endl;
        in >> d._year >> d._month >> d._day;

        if (!d.CheckInvalid())
        {
            cout << "输入了无效日期,请重新输入" << endl;
        }
        else
        {
            break;
        }    
    }
    return in;
}

上方实现时,有日期+天数和日期+=天数。二者实现其中一个即可复用另一个。但是要先实现哪一个更好呢?

image.png

目录
相关文章
|
2天前
|
测试技术 C++
C++|运算符重载(3)|日期类的计算
C++|运算符重载(3)|日期类的计算
|
3天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0
|
4天前
|
C++ Linux
|
4天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
18 0
|
4天前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
21 1
|
4天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
9 1
|
4天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
16 0
|
4天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
18 0
【C++】string学习 — 手搓string类项目