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

相关文章

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL 默认隔离级别的设置

《PostgreSQL默认隔离级别的设置》PostgreSQL的默认事务隔离级别是读已提交,这是其事务处理系统的基础行为模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一 默认隔离级别概述1.1 默认设置1.2 各版本一致性二 读已提交的特性2.1 行为特征2.2

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg