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

【C++】类和对象——const修饰成员函数和取地址操作符重载

在上篇博客中,我们已经对于日期类有了较为全面的实现,但是,还有一个问题,比如说,我给一个const修饰的日期类的对象
在这里插入图片描述
在这里插入图片描述

这个对象是不能调用我们上篇博客写的函数的,因为&d1是const Date*类型的,而this指针是Date*类型,&d1传给this是一种权限的放大,这是不行的,所以,我们要改造一下相关函数,就是声明和定义都要加上const,那么具体形式如下
在这里插入图片描述
在这里插入图片描述
不只是这一个函数,像比较大小,加减天数等,凡是不改变this指针指向的内容的值的,都要加const,那么<<这个符号加const吗?不用,因为这不是成员函数,没有this指针
关于日期类的所有代码我会放在这篇文章的最后,下面我们来说最后两个类的默认成员函数,就是取地址操作符重载和const修饰的取地址操作符重载
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这个默认成员函数确实没什么实际的作用,就算我不写这个函数,直接取地址也不会有任何问题,唯一的作用就是你可以选择不返回this,而返回空或一个假地址
Date.h文件

#include<iostream>
#include<assert.h>
using namespace std;
class Date {
public:Date(int year = 1, int month = 1, int day = 1);Date* operator&();const Date* operator&()const;void Print()const;int GetMonthDay(int year, int month);bool operator==(const Date& n);bool operator!=(const Date& n);bool operator<(const Date& n);bool operator<=(const Date& n);bool operator>(const Date& n);bool operator>=(const Date& n);Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);Date& operator++();//前置++Date operator++(int);//后置++Date& operator--();Date operator--(int);int operator-(const Date& d);friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in,  Date& d);private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in,  Date& d);

Date.cpp文件

#include"Date.h"
Date::Date(int year, int month , int day ) {_year = year;_month = month;_day = day;if (_year < 1 || _month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month)) {cout << _year << "年" << _month << "月" << _day << "日";cout << "日期非法" << endl;}
}
Date* Date:: operator&() {return this;
}const Date* Date::operator&()const {return this;
}void Date::Print() const{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}int Date :: GetMonthDay(int year, int month) {assert(year >= 1 && month >= 1 && month <= 12);int monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if ((month == 2) && (((year % 400) == 0) || ((year % 4) == 0 && (year % 100) != 0))) {return 29;}return monthArray[month];
}bool Date:: operator==(const Date& n) {return _year == n._year && _month == n._month && _day == n._day;
}bool Date::operator!=(const Date& n) {return !(*this == n);
}bool Date:: operator<(const Date& n) {if (_year < n._year) {return true;}if (_year == n._year && _month < n._month) {return true;}if (_year == n._year && _month == n._month && _day < n._day) {return true;}return false;
}bool Date::operator<=(const Date& n) {return ((*this < n) || (*this == n));
}bool Date::operator>(const Date& n) {return !(*this <= n);
}bool Date:: operator>=(const Date& n) {//return *this > n || *this == n;return !(*this < n);
}Date& Date:: operator+=(int day) {if (day < 0) {return *this -= (-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);tmp += day;return tmp;
}Date& Date:: operator-=(int day) {if (day < 0) {return *this -= (-day);}if (day < 0) {return *this += (-day);}_day -= day;while (_day <= 0) {--_month;if (_month == 0) {--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date:: operator-(int day) {Date tmp(*this);tmp -= day;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;
}Date Date:: operator--(int) {Date tmp(*this);*this -= 1;return tmp;
}//int Date::operator-(const Date& d) {
//	int flag = -1;
//	Date min = *this;
//	Date max = d;
//	if (*this > d) {
//		min = d;
//		max = *this;
//		flag = 1;
//	}
//	int n = 0;
//	while (min < max) {
//		++min;
//		++n;
//	}
//	return n*flag;
//}
int Date::operator-(const Date& d) {int flag = 1;Date max = *this;Date min = d;if (*this < d) {max = d;min = *this;flag = -1;}int n = 0;int y = min._year;while (y != max._year) {if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {n += 366;}else {n += 365;}y++;}int m1 = 1;int m2 = 1;while (m1 < max._month) {n += GetMonthDay(max._year, m1);m1++;}while (m2 < min._month) {n -= GetMonthDay(min._year, m2);m2++;}n = n + max._day - min._day;return n;
}
ostream& operator<<(ostream& out, const Date& d) {out << d._year << "-" << d._month << "-" << d._day << endl;return out;
}istream& operator>>(istream& in,  Date& d) {in >> d._year>>d._month >> d._day;return in;
}

相关文章:

【C++】类和对象——const修饰成员函数和取地址操作符重载

在上篇博客中&#xff0c;我们已经对于日期类有了较为全面的实现&#xff0c;但是&#xff0c;还有一个问题&#xff0c;比如说&#xff0c;我给一个const修饰的日期类的对象 这个对象是不能调用我们上篇博客写的函数的&#xff0c;因为&d1是const Date*类型的&#xff…...

express+mySql实现用户注册、登录和身份认证

expressmySql实现用户注册、登录和身份认证 注册 注册时需要对用户密码进行加密入库&#xff0c;提高账户的安全性。用户登录时再将密码以相同的方式进行加密&#xff0c;再与数据库中存储的密码进行比对&#xff0c;相同则表示登录成功。 安装加密依赖包bcryptjs cnpm insta…...

【PyTorch】(二)加载数据集

文章目录 1. 创建数据集1.1. 直接继承Dataset类1.2. 使用TensorDataset类 2. 加载数据集3. 将数据转移到GPU 1. 创建数据集 主要是将数据集读入内存&#xff0c;并用Dataset类封装。 1.1. 直接继承Dataset类 必须要重写__getitem__方法&#xff0c;用于根据索引获得相应样本…...

如何提高3D建模技能?

无论是制作影视动画还是视频游戏&#xff0c;提高3D建模技能对于你的工作都至关重要的。那么如何能创建出精美的3D模型呢&#xff1f;本文给大家一些3D建模技能方面的建议。 3D建模通过专门的软件完成&#xff0c;涉及制作三维对象。这项技能在视频游戏开发、建筑、动画和产品…...

【前端开发】Next.js与Nest.js之间的差异2023

在快节奏的网络开发领域&#xff0c;JavaScript已成为构建可靠且引人入胜的在线应用程序的标准语言。然而&#xff0c;随着对适应性强、高效的在线服务的需求不断增加&#xff0c;开发人员通常不得不从广泛的库和框架中进行选择&#xff0c;以满足其项目的要求。Next.js和Nest.…...

【CAN通信】CanIf模块详细介绍

目录 1.内容简介 2.CanIf详细设计 2.1 CanIf功能简介 2.2 一些关键概念 2.3依赖的上下层模块 2.4 功能详细设计 2.4.1 Hardware object handles 2.4.2 Static L-PDUs 2.4.3 Dynamic L-PDUs 2.4.4 Dynamic Transmit L-PDUs 2.4.5 Dynamic receive L-PDUs 2.4.6Physi…...

PS最新磨皮软件Portraiture4.1.2

Portraiture是一款好用的PS磨皮滤镜插件&#xff0c;拥有磨皮美白的功能&#xff0c;操作也很简单&#xff0c;一键点击即可实现美白效果&#xff0c;软件还保留了人物的皮肤质感让照片看起来更加真实。portraiture体积小巧&#xff0c;不会占用过多的电脑内存哦。 内置了多种…...

旋转框(obb)目标检测计算iou的方法

首先先定义一组多边形&#xff0c;这里的数据来自前后帧的检测结果 pre [[[860.0, 374.0], [823.38, 435.23], [716.38, 371.23], [753.0, 310.0]],[[829.0, 465.0], [826.22, 544.01], [684.0, 539.0], [686.78, 459.99]],[[885.72, 574.95], [891.0, 648.0], [725.0, 660.0]…...

render函数举例

在这段代码中&#xff0c;renderButton是一个对象吗 还有render为什么不能写成render() {} 代码原文链接 <template><div><renderButton /></div> </template><script setup> import { h, ref } from "vue"; const renderButt…...

微信小程序文件预览和下载-文件系统

文件预览和下载 在下载之前&#xff0c;我们得先调用接口获取文件下载的url 然后通过wx.downloadFile将下载文件资源到本地 wx.downloadFile({url: res.data.url,success: function (res) {console.log(数据,res);} })tempFilePath就是临时临时文件路径。 通过wx.openDocume…...

图解Redis适用场景

Redis以其速度而闻名。 1 业务数据缓存 1.1 通用数据缓存 string&#xff0c;int&#xff0c;list&#xff0c;map。Redis 最常见的用例是缓存对象以加速 Web 应用程序。 此用例中&#xff0c;Redis 将频繁请求的数据存储在内存。允许 Web 服务器快速返回频繁访问的数据。这…...

掌握Python BentoML:构建、部署和管理机器学习模型

更多资料获取 &#x1f4da; 个人网站&#xff1a;ipengtao.com BentoML是一个开源的Python框架&#xff0c;旨在简化机器学习模型的打包、部署和管理。本文将深入介绍BentoML的功能和用法&#xff0c;提供详细的示例代码和解释&#xff0c;帮助你更好地理解和应用这个强大的工…...

西南科技大学模拟电子技术实验二(二极管特性测试及其应用电路)预习报告

目录 一、计算/设计过程 二、画出并填写实验指导书上的预表 三、画出并填写实验指导书上的虚表 四、粘贴原理仿真、工程仿真截图 一、计算/设计过程 说明:本实验是验证性实验,计算预测验证结果。是设计性实验一定要从系统指标计算出元件参数过程,越详细越好。用公式输入…...

熟悉SVN基本操作-(SVN相关介绍使用以及冲突解决)

一、SVN相关介绍 1、SVN是什么? 代码版本管理工具它能记住你每次的修改查看所有的修改记录恢复到任何历史版本恢复已经删除的文件 2、SVN跟Git比&#xff0c;有什么优势 使用简单&#xff0c;上手快目录级权限控制&#xff0c;企业安全必备子目录checkout&#xff0c;减少…...

代码随想录二刷 |字符串 |反转字符串II

代码随想录二刷 &#xff5c;字符串 &#xff5c;反转字符串II 题目描述解题思路 & 代码实现 题目描述 541.反转字符串II 给定一个字符串 s 和一个整数 k&#xff0c;从字符串开头算起&#xff0c;每计数至 2k 个字符&#xff0c;就反转这 2k 字符中的前 k 个字符。 如果…...

哪吒汽车拔头筹,造车新势力首家泰国工厂投产

中国造车新势力首家泰国工厂投产&#xff01;11月30日&#xff0c;哪吒汽车位于泰国的首家海外工厂——泰国生态智慧工厂正式投产下线新车&#xff0c;哪吒汽车联合创始人兼CEO张勇、哪吒汽车泰国合作伙伴BGAC公司首席执行官万查曾颂翁蓬素等出席仪式。首辆“泰国制造”的哪吒汽…...

Redis String类型

String 类型是 Redis 最基本的数据类型&#xff0c;String 类型在 Redis 内部使用动态长度数组实现&#xff0c;Redis 在存储数据时会根据数据的大小动态地调整数组的长度。Redis 中字符串类型的值最大可以达到 512 MB。 关于字符串需要特别注意∶ 首先&#xff0c;Redis 中所…...

lxd提权

lxd/lxc提权 漏洞介绍 lxd是一个root进程&#xff0c;它可以负责执行任意用户的lxd&#xff0c;unix套接字写入访问操作。而且在一些情况下&#xff0c;lxd不会调用它的用户权限进行检查和匹配 原理可以理解为用用户创建一个容器&#xff0c;再用容器挂载宿主机磁盘&#xf…...

Ubuntu+Tesla V100环境配置

系统基本信息 nvidia-smi’ nvidia-smi 470.182.03 driver version:470.182.03 cuda version: 11.4 查看系统体系结构 uname -aUTC 2023 x86_64 x86_64 x86_64 GNU/Linux 下载miniconda https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/?CM&OA https://mi…...

leetcode:用栈实现队列(先进先出)

题目描述 题目链接&#xff1a;232. 用栈实现队列 - 力扣&#xff08;LeetCode&#xff09; 题目分析 我们先把之前写的数组栈的实现代码搬过来 用栈实现队列最主要的是实现队列先进先出的特点&#xff0c;而栈的特点是后进先出&#xff0c;那么我们可以用两个栈来实现&…...

Linux应用开发之网络套接字编程(实例篇)

服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …...

大数据学习栈记——Neo4j的安装与使用

本文介绍图数据库Neofj的安装与使用&#xff0c;操作系统&#xff1a;Ubuntu24.04&#xff0c;Neofj版本&#xff1a;2025.04.0。 Apt安装 Neofj可以进行官网安装&#xff1a;Neo4j Deployment Center - Graph Database & Analytics 我这里安装是添加软件源的方法 最新版…...

中南大学无人机智能体的全面评估!BEDI:用于评估无人机上具身智能体的综合性基准测试

作者&#xff1a;Mingning Guo, Mengwei Wu, Jiarun He, Shaoxian Li, Haifeng Li, Chao Tao单位&#xff1a;中南大学地球科学与信息物理学院论文标题&#xff1a;BEDI: A Comprehensive Benchmark for Evaluating Embodied Agents on UAVs论文链接&#xff1a;https://arxiv.…...

[ICLR 2022]How Much Can CLIP Benefit Vision-and-Language Tasks?

论文网址&#xff1a;pdf 英文是纯手打的&#xff01;论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误&#xff0c;若有发现欢迎评论指正&#xff01;文章偏向于笔记&#xff0c;谨慎食用 目录 1. 心得 2. 论文逐段精读 2.1. Abstract 2…...

【算法训练营Day07】字符串part1

文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接&#xff1a;344. 反转字符串 双指针法&#xff0c;两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...

python爬虫:Newspaper3k 的详细使用(好用的新闻网站文章抓取和解析的Python库)

更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 一、Newspaper3k 概述1.1 Newspaper3k 介绍1.2 主要功能1.3 典型应用场景1.4 安装二、基本用法2.2 提取单篇文章的内容2.2 处理多篇文档三、高级选项3.1 自定义配置3.2 分析文章情感四、实战案例4.1 构建新闻摘要聚合器…...

什么是EULA和DPA

文章目录 EULA&#xff08;End User License Agreement&#xff09;DPA&#xff08;Data Protection Agreement&#xff09;一、定义与背景二、核心内容三、法律效力与责任四、实际应用与意义 EULA&#xff08;End User License Agreement&#xff09; 定义&#xff1a; EULA即…...

Android Bitmap治理全解析:从加载优化到泄漏防控的全生命周期管理

引言 Bitmap&#xff08;位图&#xff09;是Android应用内存占用的“头号杀手”。一张1080P&#xff08;1920x1080&#xff09;的图片以ARGB_8888格式加载时&#xff0c;内存占用高达8MB&#xff08;192010804字节&#xff09;。据统计&#xff0c;超过60%的应用OOM崩溃与Bitm…...

【开发技术】.Net使用FFmpeg视频特定帧上绘制内容

目录 一、目的 二、解决方案 2.1 什么是FFmpeg 2.2 FFmpeg主要功能 2.3 使用Xabe.FFmpeg调用FFmpeg功能 2.4 使用 FFmpeg 的 drawbox 滤镜来绘制 ROI 三、总结 一、目的 当前市场上有很多目标检测智能识别的相关算法&#xff0c;当前调用一个医疗行业的AI识别算法后返回…...

ip子接口配置及删除

配置永久生效的子接口&#xff0c;2个IP 都可以登录你这一台服务器。重启不失效。 永久的 [应用] vi /etc/sysconfig/network-scripts/ifcfg-eth0修改文件内内容 TYPE"Ethernet" BOOTPROTO"none" NAME"eth0" DEVICE"eth0" ONBOOT&q…...