千家信息网

C++ Date类的实现方法有哪些

发表于:2025-02-22 作者:千家信息网编辑
千家信息网最后更新 2025年02月22日,本篇内容主要讲解"C++ Date类的实现方法有哪些",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C++ Date类的实现方法有哪些"吧!一、Date类相
千家信息网最后更新 2025年02月22日C++ Date类的实现方法有哪些

本篇内容主要讲解"C++ Date类的实现方法有哪些",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C++ Date类的实现方法有哪些"吧!

一、Date类相关接口

接口展示:

class Date{         //输出操作符重载        friend ostream& operator<<(ostream& _cout, const Date& d);        //输出操作符重载        friend istream& operator>>(istream& _cin, Date& d);public:    // 获取某年某月的天数    int GetMonthDay(int year, int month);    // 全缺省的构造函数    Date(int year=1988, int month=1, int day=1);    // 拷贝构造函数    Date(const Date& d);    // 赋值运算符重载    Date& 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--(int);    // 前置--    Date& operator--();    // >运算符重载    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);    // 日期-日期 返回两个日期之间相隔的具体天数    int operator-(const Date& d);    //日期展示    void print()    {        cout << _year << " " << _month << " " << _day << endl;      }private:    int _year;    int _month;    int _day;};

二、具体接口函数实现

注意:

因为对于定义在类里面的函数会自动设成内联函数,而只有一些简单的函数才建议设成内联函数,所以实现函数时我们是声明和定义分离(在类里面声明,类外定义)

在类外实现函数接口需要加上类域名称

1、获取月份天数

注意:

闰年二月与平年二月的天数不同

实现代码:

//获取月份天数int Date::GetMonthDay(int year, int month){        //设置平年月天数数组        static int monthdays[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//设置成静态避免重复创建        int day = monthdays[month];        //对闰年二月的处理        if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))        {                day = 29;        }        return day;}

2、Date打印

注:打印函数比较简单,设成内联函数很适合,可以直接在类里定义

实现代码:

void Date::Print(){        cout << _year << "年" << _month << "月" << _day << "日" << endl;}

3、Date构造函数

注意:

对于构造函数建议写成全缺省函数(便于无参数初始化),但是只能定义和声明其中一个写缺省

考虑初始化的日期是否合理

实现代码:

//构造函数//类里声明Date(int year = 0, int month = 1, int day = 1);//定义Date::Date(int year, int month, int day){        // 检查日期的合法性        if (year >= 0                && month > 0 && month < 13                && day > 0 && day <= GetMonthDay(year, month))        {                _year = year;                _month = month;                _day = day;        }        else        {                // 严格来说抛异常更好                cout << "非法日期" << endl;                cout << year << "年" << month << "月" << day << "日" << endl;                exit(-1);        }}

4、Date析构函数

注:对于像Date一样的类来说,析构函数(没有需要清理的空间资源),拷贝函数和赋值重载函数(能够完成成员变量浅拷贝)都不用自己写,编译器默认生成的已经足够使用

实现代码:

//析构函数Date::~Date(){        _year = 1;        _month = 0;        _day = 0;}

5、Date拷贝构造函数

实现代码:

//拷贝构造Date::Date(const Date& d){        _year = d._year;        _month = d._month;        _day= d._day;}

6、Date赋值重载函数

注意:

对于赋值操作符来说,是需要能支持连续赋值的操作,这里我们返回Date本身来进行接下来的继续赋值

实现代码:

//赋值运算符重载Date& Date::operator=(const Date& d){        _year = d._year;        _month = d._month;        _day = d._day;        return *this;}

效果图:

7、Date+=天数

注意:

  1. +=表示会修改Date本身的数据

  2. 处理传入负数天数

  3. 处理好天数进位,月份进位

实现代码:

//日期+=天数Date& Date::operator+=(int day){        if (day < 0)//处理特殊情况        {                *this -= -day;//复用Date-=天数        }        else        {                _day += day;                while (_day > GetMonthDay(_year, _month))//处理数据合理性                {                        _day -= GetMonthDay(_year, _month);                        _month++;                        if (_month > 12)                        {                         _year++;                                _month = 1;                        }                }        }        return *this;//返回引用,即对象本身}

8、Date+天数

注意:

+天数表示不会修改Date本身的数据(使用const修饰,避免修改)

逻辑与Date+=天数基本一致,可以进行复用

实现代码:

Date Date::operator+(int day) const{        Date tmp = *this;//赋值重载        tmp += day;//复用+=重载        return tmp;//返回值(拷贝构造)}

9、Date-=天数

注意:

  1. +=表示会修改Date本身的数据

  2. 处理传入负数天数

  3. 考虑日期的借位,月份的借位

实现代码:

//日期-=天数Date& Date::operator-=(int day){        if (day < 0)        {                *this += -day;//复用Date+=天数        }        else        {                _day -= day;                while (_day <= 0)//处理数据合理性                {                        _month--;                        if (_month <= 0)                        {                                _year--;                                _month = 12;                        }                        _day += GetMonthDay(_year, _month);                }        }        return *this;}

10、Date-天数

注意:

  1. -天数不会修改Date本身的数据(使用const修饰,避免修改)

  2. 逻辑与Date-=天数基本一致,可以进行复用

实现代码:

Date Date::operator-(int day) const{        Date tmp = *this;        tmp -= day;        return tmp;}

11、++Date

注意:

前置++表示,Date先增后使用

实现代码:

//++DateDate& Date::operator++(){        *this += 1;//复用Date+=天数        return *this;}

12、Date++

注意:

语法规定,因为与前置命名相同的缘故,这里的后置函数多一个参数来与前置函数形成重载

后置++表示先使用后自增

实现代码:

//Date++Date Date::operator++(int){        Date tmp = *this;//保存一份日期        *this += 1;//自增当前日期        return tmp;//返回自增前的日期}

13、-Date

实现代码:

//--DateDate& Date::operator--(){        *this -= 1;        return *this;}

14、Date-

实现代码:

//Date--Date Date::operator--(int){        Date tmp = *this;        *this -= 1;        return tmp;}

15、日期比较

注:可以多次复用

实现代码:

//日期比较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 _year == d._year && _month == d._month && _day == d._day;}bool Date::operator<(const Date& d) const{        return !(*this >= d);}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);}

16、Date相减

实现代码:

 //日期减日期 int Date::operator-(const Date& d) const {   //确定日期的大小   Date max = *this;   Date min = d;   if (*this < d)//复用日期比较   {           max = d;           min = *this;   }   int day = 0;   while (max != min)   {           ++min;           ++day;   }   return day; }

17、日期输入\日期输出

注意:

  1. 对于输入操作符,我们习惯是cin>>date,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)

  2. 虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员

实现代码:

//输出操作符重载ostream& operator<<(ostream& _cout, const Date& d){        _cout << d._year << "年" << d._month << "月" << d._day << "日" ;        return _cout;}//输出操作符重载istream& operator>>(istream& _cin, Date& d)> {        _cin >> d._year >> d._month >> d._day;        return _cin;}

效果图:

  1. date,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)

  2. 虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员

实现代码:

//输出操作符重载ostream& operator<<(ostream& _cout, const Date& d){        _cout << d._year << "年" << d._month << "月" << d._day << "日" ;        return _cout;}//输出操作符重载istream& operator>>(istream& _cin, Date& d){        _cin >> d._year >> d._month >> d._day;        return _cin;}

效果图:

到此,相信大家对"C++ Date类的实现方法有哪些"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0