C++:日期类的实现 const修饰 取地址及const取地址操作符重载(类的6个默认成员函数完结篇)

本文主要是介绍C++:日期类的实现 const修饰 取地址及const取地址操作符重载(类的6个默认成员函数完结篇),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、日期类的实现

根据之前赋值运算符重载逻辑,我们现在来实现完整的日期类。

1.1 判断小于

上篇博客已经实现:

bool 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;
}

1.2 判断等于

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

1.3 判断小于等于

  • 根据1.1和1.2,我们可以直接复用。
bool operator<=(const Date& d)
{return *this <= d || *this == d;
}
  • 假如要判断d1是否小于等于d2,那就是d1相当于*this,d2相当于d

1.4 判断大于

  • 在这里我们改变一下思路,不需要像判断小于那样if嵌套,正因为已经实现了小于,我们直接取反就可以实现大于了
bool operator>(const Date& d)
{return !(*this <=d);
}

1.5 判断大于等于

这里小于取反

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

1.6 判断不等于

这里等于取反

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

1.7 获取月份天数

因为有闰年的存在,所以我们必须先实现获取月份天数才能确保加减天数

  • 因为这里的获取月份天数需要被频繁调用,我们这里声明和定义就不分离了(本质就是内联函数inline)
  • 放到静态区,好处是避免重复调用而导致数组重复生成
inline int GetMonthDay(int year,int month)
{assert(month < 13 && month > 0);//这里记得加上头文件#include<>static int MonthDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31 };// 先判断月份if (month == 2 && (((year % 100 == 0) && (year % 4 == 0)) || (year % 400 != 0)))return 29;return MonthDays[month];
}

1.8 日期加等天数

获取到月份天数后,我们就可以往下实现了。

  • 首先加上天数,判断当前月的天数和加上的天数
  • 然后进行减掉天数,月份+1,如果月份等于了13,年就+1,月份赋值为1
Date& operator+=(int day)
{// 这里就直接修改了_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}return *this;
}

1.9 日期加天数(本身不能改变)

  • 与日期加等天数不同,这里需要另外开一块空间,修改别的空间才不会影响这里的值
  • 这里不可以用引用返回(出了作用域还在才能使用引用返回),tmp是一个临时对象,必须用传值返回
Date operator+(int day)
{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 operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}
  • 也可以用+=复用+
Date& operator+=(int day)
{*this = *this + day;return *this;
}

总体而言用+复用+=会更好,因为+里面会创建临时对象


1.10 日期减等天数

思路与上面加等一致

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

1.11 日期减天数

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

二、前置++和后置++重载

  • 前置++
Date& operator++()
{*this += 1;return *this;
}
  • 为了与前置++区分,增加一个int形参,能够构成重载区分
  • 后置++是要返回++以后的值
Date operator++(int)
{Date tmp = *this;*this += 1;return tmp;
}

三、日期-日期

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

四、const修饰

4.1 const成员函数

  • 定义:将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。

在这里插入图片描述

class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}void Print(){cout << "Print()" << endl;cout << "year:" << _year << endl;cout << "month:" << _month << endl;cout << "day:" << _day << endl << endl;}void Print(){cout << "Print()const" << endl;cout << "year:" << _year << endl;cout << "month:" << _month << endl;cout << "day:" << _day << endl << endl;}
private:int _year; // 年int _month; // 月int _day; // 日
};
void Test()
{Date d1(2022,1,13);d1.Print();const Date d2(2022,1,13);d2.Print();
}

我们一起来运行一下:
在这里插入图片描述

  • 这里是const对象去调用非const成员函数

  • 这里会出现一个权限放大的问题

  • 因此参数要改为 const Date*

所以要解决这个问题,我们要在第二个Print成员函数处加上一个const,如下图:
(这里的const修饰的是this指针指向的内容)
在这里插入图片描述


  • 下面图片为非const对象和const对象同时调用const成员函数

在这里插入图片描述
根据运行结果可以看到:非const对象是可以调用const成员函数的(因为这是权限的缩小)


既然非const对象和const对象都可以调用const成员函数,那我们是否可以将所有函数都加上const呢?
答案是不能的~
因为如果函数内部要被修改,那肯定是不能加的。

4.2 const修饰总结

  • 成员函数如果是一个对成员变量只进行读访问的函数,一般加上const,这样const对象和非const对象都可以访问

  • 成员函数如果是一个对成员变量进行读写访问的函数,不可以加上const,因为不能修改成员变量

下面集中总结4个问题:

  1. const对象可以调用非const成员函数吗? > 不可以(权限放大)
  2. 非const对象可以调用const成员函数吗? > 可以(权限缩小)
  3. const成员函数内可以调用其它的非const成员函数吗?> 不可以(权限放大)
  4. 非const成员函数内可以调用其它的const成员函数吗?> 可以(权限缩小)

五、取地址及const取地址操作符重载

前面几篇博客我们已经聊过前面4个默认成员函数,最后再来看看这最后两个吧~(了解即可)
在这里插入图片描述
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

class Date
{ 
public :Date* operator&(){return this ;}const Date* operator&()const{return this ;}
private :int _year ; int _month ; int _day ; 
};

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容

六、日期类的实现【源码】

#include <iostream>
#include <assert.h>
using namespace std;class Date
{
public:// 构造函数Date(int year = 1900, int month = 1, int day = 1){_year = year;_month = month;_day = day;if (!CheckInvalid()){cout << "构造日期非法" << endl;}}// 判断等于bool operator==(const Date& d){return _year == d._year&& _month == d._month&& _day == d._day;}// 判断小于bool 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 operator<=(const Date& d){return *this <= d || *this == d;}// 判断大于bool operator>(const Date& d){return !(*this <= d);}// 判断大于等于bool operator>=(const Date& d){return !(*this < d);}// 判断不等于bool operator!=(const Date& d){return !(*this == d);}// 日期加等天数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 operator+(int day){Date tmp(*this);//Date tmp = *this;tmp += day;return tmp;}// 日期加天数Date operator+(const Date& d){Date tmp(*this);tmp._day += d._day;while (d._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& operator-=(int day){_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;}// 日期减天数Date operator-(int day){Date tmp = *this;tmp -= day;return tmp;}// 前置++Date& operator++(){*this += 1;return *this;}// 后置++Date operator++(int){Date tmp = *this;*this += 1;return tmp;}// 日期-日期int operator-(const Date& d){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;}inline int GetMonthDay(int year, int month){assert(month < 13 && month > 0);// 放到静态区static int MonthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };// 先判断月份if (month == 2 && (((year % 100 == 0) && (year % 4 == 0)) || (year % 400 != 0)))return 29;return MonthDays[month];}// 拷贝构造Date& operator=(const Date& d){if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;}friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);bool CheckInvalid(){if (_year <= 0|| _month < 1|| _month > 12|| _day < 1|| _day > GetMonthDay(_year,_month)){return false;}else{return true;}}void Print(){cout << _year << "/" << _month << "/" << _day << endl;}
private:int _year;int _month;int _day;
};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.CheckInvalid()){cout << "输入非法日期,请重新输入" << endl;}else{break;}}return in;
}

这篇关于C++:日期类的实现 const修饰 取地址及const取地址操作符重载(类的6个默认成员函数完结篇)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/880323

相关文章

将java程序打包成可执行文件的实现方式

《将java程序打包成可执行文件的实现方式》本文介绍了将Java程序打包成可执行文件的三种方法:手动打包(将编译后的代码及JRE运行环境一起打包),使用第三方打包工具(如Launch4j)和JDK自带... 目录1.问题提出2.如何将Java程序打包成可执行文件2.1将编译后的代码及jre运行环境一起打包2

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

MySQL8.0设置redo缓存大小的实现

《MySQL8.0设置redo缓存大小的实现》本文主要在MySQL8.0.30及之后版本中使用innodb_redo_log_capacity参数在线更改redo缓存文件大小,下面就来介绍一下,具有一... mysql 8.0.30及之后版本可以使用innodb_redo_log_capacity参数来更改

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本