当前位置: 首页 > java >正文

【C++】日期类

日期类

文章目录

  • 日期类
    • 一、日期类的成员变量和默认成员函数:
    • 二、比较运算符重载
    • 三、+和-运算符重载
      • +和+=运算符
      • -和-=运算符重载
    • 四、++和--运算符重载
    • 五、流插入和流提取函数重载
      • 5.1 流插入和流提取重载函数声明写在哪?
      • 5.2 具体代码实现
    • 六、完整代码实现

一、日期类的成员变量和默认成员函数:

在设计一个类时,我们首先要想这个类有什么特性,需要什么成员变量。日期类显然的有年月日三个基础的数据,所以简单的日期类的成员变量就是他们三

class Date
{
public://...
private:int _year;int _month;int _day;
};

确定了三个成员变量,而后我们该思考默认成员函数,编译器的默认函数是什么行为,我们自己该不该写。

现在我们来回顾一下编译器生成的六个默认成员函数的行为。

  1. 构造函数

    编译器生成的默认构造函数:

    • 对于自定义类型变量,会调用它的默认构造函数
    • 对于内置类型变量没有要求

    构造函数走初始化列表规则:

    在这里插入图片描述

    编译器自己生成的默认构造函数的参数列表是空的,换句话说它仅支持不传参调用

    class Date
    {
    public:Date(int year = 2025, int month = 4, int day = 21):_year(year),_month(month),_day(day){}//......
    private:int _year;int _month;int _day;
    };
    

    在这里插入图片描述

  2. 拷贝构造

    编译器默认生成的拷贝构造函数:

    • 对于内置类型成员:对内置类型成员变量会完成值拷贝/浅拷贝(⼀个字节⼀个字节的拷贝)
    • 对于自定义类型成员:对自定义类型成员变量会调用他的拷贝构造。

    我们的三个成员变量都是内置类型,而且没有额外的空间申请,拷贝构造也没有必要自己写

  3. 赋值运算符重载

    编译器默认生成的赋值运算符重载函数:

    • 对于内置类型成员:对内置类型成员变量会完成值拷贝/浅拷贝(⼀个字节⼀个字节的拷贝)
    • 对于自定义类型成员:对自定义类型成员变量会调用他的赋值运算符重载函数。

    我们的三个成员变量都是内置类型,而且没有额外的空间申请,赋值运算符重载也没有必要自己写

  4. 析构函数

    编译器默认生成的析构函数:

    • 自定类型成员会调用他的析构函数。
    • 对内置类型成员不做处理,由编译器决定

    我们的三个成员变量都是内置类型,而且没有额外的空间申请,析构函数没有必要自己写

  5. 取地址运算符重载和const取地址重载

    编译器生成默认函数就是返回地址

    编译器生成的够用了,没有必要写

到这里我们发现日期类6个默认成员函数,只有构造函数需要我们显示的写

在这里插入图片描述

但有时候为了方便调试,或者出于学习需求,我们也可以写写

class Date
{
public://默认构造Date(int year = 2025, int month = 4, int day = 21):_year(year),_month(month),_day(day){cout << "Date(int year = 2025, int month = 4, int day = 21)" << endl;if (!CheckDate())//成员函数,在后面实现{cout << "非法日期" << endl;Print();//成员函数,在后面实现}}//拷贝构造Date(const Date& d){cout << "拷贝构造" << endl;_year = d._year;_month = d._month;_day = d._day;}//赋值运算符重载Date& operator=(const Date& d){cout << "Date& operator=(const Date& d)赋值运算符重载" << endl;_year = d._year;_month = d._month;_day = d._day;return *this;}//取地址重载函数Date* operator&()// Date* const this{return this;}//const取地址重载函数const Date* operator&() const// const Date* const this {return this;}~Date(){cout << "~Date()" << endl;//没有而外空间申请,没有写的必要,内置类型在函数栈帧销毁时销毁}//以上为6个默认成员函数//############################################################################//...........
private:int _year;int _month;int _day;
};

二、比较运算符重载

函数声明与函数实现分离

//Date.h
class Date
{
public: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;//......
private:int _year = 2025;int _month = 4;int _day = 21;
}

比较运算符重载我们无需一一实现,只需实现==<、>其中一个,这三个中的两个即可,其他通过函数的复用实现

//Date.cpp
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)return _day < d._day;}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);
}

在这里插入图片描述


三、+和-运算符重载

+和+=运算符

  1. 确定需求

    首先我们要知道不是说所有的±都是有意义的,比如说日期加日期,两个日期相加平时是很少使用的,因为意义很狭隘。我们平时使用较多的是日期+天数

  2. 具体实现

    +、+=是双目运算符,它的函数重载需要两个参数,但在类里面的成员函数会有隐式的this指针,所以只需要一个参数就够了

    //Date.h
    class Date
    {
    public://类成员函数默认内联,频繁调用的短小函数的实现可以直接写在类里面int GetMonthDay( int year, int month) const{assert(month > 0 && month < 13);static int monthDayArray[13] = { -1, 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 monthDayArray[month];}Date& operator+=(int day);Date operator+(int day);//......
    private:int _year = 2025;int _month = 4;int _day = 21;
    }
    

    +、+=的实现有两种写法

    1. +复用+=(写法更优)
    2. +=复用+
    //Date.cpp
    //1.+复用+=
    Date& Date::operator+=(int day)
    {if (day < 0){return *this -= -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)
    {Date tmp = *this;//拷贝构造1return tmp += day;//拷贝构造2,传值返回会返回临时对象,临时对象会对tmp进行拷贝构造//总共两次拷贝构造
    }
    
    //Date.cpp
    //2.+=复用+
    Date& Date::operator+=(int day)
    {this + day;//函数复用+运算符重载,会有两次拷贝构造return *this;//总共两次拷贝构造    
    }Date Date::operator+(int day)
    {Date tmp = *this;//拷贝构造1tmp._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;//拷贝构造2,传值返回会返回临时对象,临时对象会对tmp进行拷贝构造//总共两次拷贝构造
    }
    

-和-=运算符重载

  1. 确定需求

    日期-日期是有意义的,意义就是两天相差多少天。日期-天数也是有意义的。

  2. 具体实现

    -、-=的实现有两种写法

    1. -复用-=(写法更优)
    2. -=复用-
    //Date.h
    class Date
    {
    public://类成员函数默认内联,频繁调用的短小函数的实现可以直接写在类里面int GetMonthDay( int year, int month) const{assert(month > 0 && month < 13);static int monthDayArray[13] = { -1, 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 monthDayArray[month];}Date& operator-=(int day);Date operator-(int day);int operator-(const Date& d) const//......
    private:int _year = 2025;int _month = 4;int _day = 21;
    }
    

    1.-复用-=

    //Date.cpp
    //-复用-=,-=没有拷贝构造,-里有两次拷贝构造
    Date& Date::operator-=(int day)
    {if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (0 == _month){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;//没有拷贝构造
    }Date Date::operator-(int day)
    {Date tmp = *this;//拷贝构造return tmp -= day;//传值返回,临时变量调用拷贝构造//总共两次拷贝构造
    }
    

    2.-=复用-

    //Date.cpp
    //-=复用-,-实现会有两次拷贝构造,-=复用-也会有两次拷贝构造
    Date& Date::operator-=(int day)
    {*this = *this - day;//赋值运算符重载,运算符重载里会调用两次拷贝构造return *this;//总共两次拷贝构造
    }Date Date::operator-(int day)
    {Date tmp = *this;//拷贝构造1tmp._day -= day;while (tmp._day <= 0){--tmp._month;if (0 == tmp._month){tmp._month = 12;--tmp._year;}tmp._day += GetMonthDay(tmp._year, tmp._month);}return tmp;//传值返回,临时变量调用拷贝构造//总共两次拷贝构造
    }
    

    日期-日期

    int Date::operator-(const Date& d) const
    {int n = 0;int flag = 1;Date Max = *this;Date Min = d;if (*this < d){Max = d;Min = *this;flag = -1;}while (Min != Max){++Min;++n;}return flag * n;
    }
    

四、++和–运算符重载

++和–都是单目运算符,但++和–是分前置和后置,单拿++来说:++作为日期类的成员函数,因为有隐式的this指针,所以理应它是不需要参数的,为了实现前置和后置++,就需要函数重载,但是返回值不同无法作为函数重载条件。怎么办?

C++规定在后置++、–的参数列表里给一个int当作区分

//Date.h
class Date
{
public//d1++、++d1//++是一元运算符理应是没有参数的//Date& operator++()//Date& operator++()//但是返回值不同,无法作为函数重载的条件//重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,无法很好的区分。//后置++Date operator++(int);//前置++Date& operator++();//后置--Date operator--(int);//前置--Date& operator--();//........
private:int _year;int _month;int _day;
};

具体实现

复用+=、-=

//Date.cpp
//后置++
Date Date::operator++(int)
{Date tmp = *this;//拷贝构造*this += 1;return tmp;//临时变量调用拷贝构造//两次拷贝构造
}
//前置++
Date& Date::operator++()
{*this += 1;return *this;//无拷贝构造
}//后置--
Date Date::operator--(int)
{Date tmp = *this;*this -= 1;return tmp;
}
//前置--
Date& Date::operator--()
{*this -= 1;return *this;
}

五、流插入和流提取函数重载

在这里插入图片描述

我们之前用的cin其实是istream类的实例化对象,cout其实是ostream类的实例化对象

运算符<<和>>仅支持内置类型的标准输入输出,对于自定义类型的对象我们需要重载运算符<<和>>

5.1 流插入和流提取重载函数声明写在哪?

  1. 在类中作类成员对象?

    因为成员函数有隐式的this指针,所以它的第一个操作数一定是Date类对象本身
    但这其实不符合我们平时<<、>>的用法

    在这里插入图片描述

  2. 放在全局,作普通函数?

    在全局中我们可以随意更改参数列表的参数位置,因为没有隐式的this指针

    在这里插入图片描述

    但这样做当我们在函数实现时无法访问类的成员变量,在取值上还需要另作处理

  3. 类里作友元函数

    而更好的办法是对于需要访问私有且不能作为成员函数的函数,我们给他一个类的友元声明,这样它就可以访问类的私有和保护了

    在这里插入图片描述

另外的为了支持连续的流插入和流提取,我们的运算符重载需要返回值返回ostream对象的引用、istream对象的引用

ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);//这里的d不能加const,因为流插入需要权限修改d

5.2 具体代码实现

class Date
{//友元函数声明friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);//这里的d不能加const,因为流插入需要权限修改d
public://因为成员函数有隐式的this指针,所以它的第一个操作数一定是Date类对象本身//但这其实不符合我们平时<<、>>的用法//所以这里的解决办法是:<<、>>的函数重载声明放到全局,但这样做当我们在函数实现时无法访问类的成员变量,需要另作处理//而更好的办法是对于需要访问私有且不能作为成员函数的函数,我们给他一个类的友元声明,这样它就可以访问类的私有和保护了//错误写法,左操作数为Date对象//ostream& operator<<(ostream& out);//istream& operator>>(istream& in);private:int _year = 2025;int _month = 4;int _day = 21;
};//全局,无法访问d的私有
//ostream& operator<<(ostream& out, const Date& d);
//istream& operator>>(istream& in, Date& d);

六、完整代码实现

声明和定义分离

//Date.h
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;class Date
{//友元函数声明friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public://默认构造Date(int year = 2025, int month = 4, int day = 21):_year(year),_month(month),_day(day){if (!CheckDate()){cout << "非法日期" << endl;Print();}}void Print() const;//类成员函数默认内联,频繁调用的短小函数int GetMonthDay( int year, int month) const{assert(month > 0 && month < 13);static int monthDayArray[13] = { -1, 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 monthDayArray[month];}//const成员函数,防止修改,权限只读//普通成员函数的this指针:Date* const this;//const成员函数的this指针:const Date* const this;bool CheckDate() const{if (_month < 1 || _month > 13|| _day < 1 || _day   > GetMonthDay(_year, _month))return false;return true;}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);Date& operator-=(int day);Date operator-(int day);//后置++Date operator++(int);//前置++Date& operator++();////后置--Date operator--(int);//前置--Date& operator--();//d1 - d2int operator-(const Date& d) const;private:int _year;int _month;int _day;
};
//Date.cpp
#include "Date.h"void Date::Print() const
{cout << _year << '/' << _month << '/' << _day << endl;//_year++;//无法修改
}
//+复用+=
Date& Date::operator+=(int day)
{if (day < 0){return *this -= -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)
{Date tmp = *this;return tmp += day;
}//- 复用-=,-=没有拷贝构造,-里有两次拷贝构造
Date& Date::operator-=(int day)
{if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (0 == _month){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;//没有拷贝构造
}Date Date::operator-(int day)
{Date tmp = *this;//拷贝构造return tmp -= day;//传值返回,临时变量调用拷贝构造//总共两次拷贝构造
}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)return _day < d._day;}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)//int作区分
{Date tmp = *this;//拷贝构造*this += 1;return tmp;//临时变量调用拷贝构造//两次拷贝构造
}
//前置++
Date& Date::operator++()
{*this += 1;return *this;//无拷贝构造
}//后置--
Date Date::operator--(int)
{Date tmp = *this;*this -= 1;return tmp;
}
//前置--
Date& Date::operator--()
{*this -= 1;return *this;
}int Date::operator-(const Date& d) const
{int n = 0;int flag = 1;Date Max = *this;Date Min = d;if (*this < d){Max = d;Min = *this;flag = -1;}while (Min != Max){++Min;++n;}return flag * n;
}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 << "请依次输入年月日:>";in >> d._year >> d._month >> d._day;if (!d.CheckDate()){cout << "输入日期非法:";d.Print();cout << "请重新输入:" << endl;}elsebreak;}return in;
}

感谢你的阅读

http://www.xdnf.cn/news/7441.html

相关文章:

  • sherpa-ncnn:音频处理跟不上采集速度 -- 语音转文本大模型
  • Logrotate:配置日志轮转、高效管理Linux日志文件
  • 开发体育比分网站,有哪些坑需要注意的
  • 手搓一个Transformer
  • 以用户为中心的产品才是好产品
  • Kali安装配置JAVA环境和切换JDK版本的最详细的过程
  • BGP综合实验(2)
  • ai agent(智能体)开发 python高级应用7: crawl4ai 0.6.3 加re正则表达式 获取百度中含有 韩立的图片要求横屏图片
  • ts导入vue文件时提示找不到模块或其相应的类型声明问题解决
  • ADVANTEST Q8326光学波长计操作手Operation Manual
  • 升级mysql (rpm安装)
  • MIMO 检测(6)--最大似然检测(1)
  • js逆向反调试的基本 bypass
  • 【C语言】大程序结构
  • Linux详解基本指令(一)
  • 对盒模型的理解
  • 澳大利亚TikTok网络专线+本地化策略:澳洲电商品牌的破局之道
  • 最大子树和--树形dp
  • day30python打卡
  • Rust 学习笔记:关于错误处理的练习题
  • 1-3V升3.2V升压驱动WT7013
  • 反射操作注解的详细说明
  • HTTPS核心机制拆解
  • Windows 如何安装CUDA
  • 【免杀】C2免杀技术(六)进程镂空(傀儡进程)
  • 往现有虚拟环境中增加python3.9.6
  • 万用表如何区分零线、火线、地线
  • 2022年下半年信息系统项目管理师——综合知识真题及答案(3)
  • Pytorch---view()函数
  • 机器人编程基础---C语言中的文件操作