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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/217494.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

12.2旋转,SPLAY树的各种操作(SPLAY与AVL是两种BST)

Splay树和AVL树是两种不同的自平衡二叉搜索树实现。 1. 平衡条件&#xff1a;AVL树通过维护每个节点的平衡因子&#xff08;左子树高度减去右子树高度&#xff09;来保持平衡&#xff0c;要求每个节点的平衡因子的绝对值不超过1。Splay树则通过经过每次操作后将最近访问的节点…

【隐私计算】VOLE (Vector Oblivious Linear Evaluation)学习笔记

近年来&#xff0c;VOLE&#xff08;向量不经意线性评估&#xff09;被用于构造各种高效安全多方计算协议&#xff0c;具有较低的通信复杂度。最近的CipherGPT则是基于VOLE对线性层进行计算。 1 VOLE总体设计 VOLE的功能如下&#xff0c;VOLE发送 Δ \Delta Δ和 b b b给send…

MySQL笔记-第03章_基本的SELECT语句

视频链接&#xff1a;【MySQL数据库入门到大牛&#xff0c;mysql安装到优化&#xff0c;百科全书级&#xff0c;全网天花板】 文章目录 第03章_基本的SELECT语句1. SQL概述1.1 SQL背景知识1.2 SQL语言排行榜1.3 SQL 分类 2. SQL语言的规则与规范2.1 基本规则2.2 SQL大小写规范 …

Linux系统安装Python3环境

1、默认情况下&#xff0c;Linux会自带安装Python&#xff0c;可以运行python --version命令查看&#xff0c;如图&#xff1a; 我们看到Linux中已经自带了Python2.7.5。再次运行python命令后就可以使用python命令窗口了&#xff08;CtrlD退出python命令窗口&#xff09;。 2…

MySQL笔记-第06章_多表查询

视频链接&#xff1a;【MySQL数据库入门到大牛&#xff0c;mysql安装到优化&#xff0c;百科全书级&#xff0c;全网天花板】 文章目录 第06章_多表查询1. 一个案例引发的多表连接1.1 案例说明1.2 笛卡尔积&#xff08;或交叉连接&#xff09;的理解1.3 案例分析与问题解决 2. …

详解原生Spring当中的事务

&#x1f609;&#x1f609; 学习交流群&#xff1a; ✅✅1&#xff1a;这是孙哥suns给大家的福利&#xff01; ✨✨2&#xff1a;我们免费分享Netty、Dubbo、k8s、Mybatis、Spring...应用和源码级别的视频资料 &#x1f96d;&#x1f96d;3&#xff1a;QQ群&#xff1a;583783…

如何从T-N曲线判断电机选对了没有

我的知乎原文&#xff1a;https://zhuanlan.zhihu.com/p/670156320? 如果你是一个刚入行的电机工程师&#xff0c;刚刚参加了一个新产品的开发&#xff0c;在众多电机供应商中让你去挑选一款合适的电机&#xff0c;该从哪个角度去入手呢&#xff1f; 今天这篇文章就从T-N曲线…

llama2.c推理

模型图 代码及分析 不需要考虑任何mask问题&#xff0c;直接通过矩阵计算求出下三角矩阵每个元素的值即可&#xff0c;不需要额外添加mask之类的。 temperature0&#xff08;确定性&#xff09;的时候&#xff0c;模型推理每次都取概率最大的&#xff08;从而导致同样的输入…

4.grid_sample理解与使用

pytorch中的grid_sample 文章目录 pytorch中的grid_samplegrid_samplegrid_sample函数原型实例 欢迎访问个人网络日志&#x1f339;&#x1f339;知行空间&#x1f339;&#x1f339; grid_sample 直译为网格采样&#xff0c;给定一个mask patch&#xff0c;根据在目标图像上的…

css实现正六边形嵌套圆心

要实现一个正六边形嵌套圆心&#xff0c;可以使用CSS的::before和::after伪元素以及border-radius属性。以下是具体的解析和代码&#xff1a; 使用::before和::after伪元素创建正六边形。设置正六边形的背景色。使用border-radius属性使正六边形的内角为60度。在正六边形内部创…

基于Springboot的在线问卷调查系统(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的在线问卷调查系统(有报告)。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0c;通过Spring…

初步认识结构体

hello&#xff0c;hello&#xff0c;各位小伙伴&#xff0c;本篇文章跟大家一起学习结构体&#xff0c;并跟大家一边做题一边进行学习和理解。感谢大家对我上一篇的支持&#xff0c;如有什么问题&#xff0c;还请多多指教&#xff01; 如果本篇文章对你有帮助&#xff0c;还请…

【arduino库之TroykaDHT(针对DHT系列温湿度传感器)】

该库允许您从 DHT 系列传感器读取温度和湿度。 该库允许获取以摄氏度、开尔文和华氏度为单位的相对湿度和温度数据。支持的传感器&#xff1a;DH11、DHT21、DHT22。 TroykaDHT库的的使用非常简单&#xff0c;它包含7个函数&#xff1a; begin //初始化接口&#xff0c;做好…

Course2-Week2-神经网络的训练方法

Course2-Week2-神经网络的训练方法 文章目录 Course2-Week2-神经网络的训练方法1. 神经网络的编译和训练1.1 TensorFlow实现1.2 损失函数和代价函数的数学公式 2. 其他的激活函数2.1 Sigmoid激活函数的替代方案2.2 如何选择激活函数2.3 为什么需要激活函数 3. 多分类问题和Soft…

Redis 分布式锁测试

一、前提依赖&#xff08;除去SpringBoot项目基本依赖外&#xff09;&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId> </dependency><!-- 配置使用redis启动…

移动端APP自动化测试框架-UiAutomator2基础

很早以前&#xff0c;我用uiautomatorjava实践过Android APP自动化测试&#xff0c;不过今天要提的不是uiautomator&#xff0c;而是uiautomator2。听起来uiautomator2像是uiautomator的升级版&#xff0c;但是这两款框架仅仅是名字上比较相似&#xff0c;实际上没有任何关联。…

BiseNet实现遥感影像地物分类

遥感地物分类通过对遥感图像中的地物进行准确识别和分类&#xff0c;为资源管理、环境保护、城市规划、灾害监测等领域提供重要信息&#xff0c;有助于实现精细化管理和科学决策&#xff0c;提升社会治理和经济发展水平。深度学习遥感地物分类在提高分类精度、自动化程度、处理…

最长连续序列代码中的细节解读

最长连续序列 一、题目概述 给定一个未排序的整数数组 nums &#xff0c;找出数字连续的最长序列&#xff08;不要求序列元素在原数组中连续&#xff09;的长度。 请你设计并实现时间复杂度为 O(n) 的算法解决此问题。 原题地址&#xff1a;https://leetcode.cn/problems/l…

前端开发学习 (四) 自定义按键修饰符

一、 v-on的按键修饰符 按键修饰符&#xff0c;通俗的来说就是监听键盘输入的事件&#xff0c; 在Vue 中允许为 v-on 在监听键盘事件时添加按键修饰符 修饰符用途.enter当在输入框按下回车时触发.tab当按下tab时触发.delete当按下删除键&#xff08;通常是键盘上的Delete键&am…

软件工程 课后题 选择 查缺补漏

在一张状态图中只能有一个初态&#xff0c;而终态则可以没有&#xff0c;也可以有多个 所有的对象可以成为各种对象类&#xff0c;每个对象类都定义了一组 方法 通过执行对象的操作可以改变对象的属性&#xff0c;但它必须经过 消息 的传递 UML应用于 基于对象的面向对象的方…
最新文章