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

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter