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

news2024/9/22 19:30:38

在上篇博客中,我们已经对于日期类有了较为全面的实现,但是,还有一个问题,比如说,我给一个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.coloradmin.cn/o/1272907.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【合集】MQ消息队列——Message Queue消息队列的合集文章 RabbitMQ入门到使用

前言 RabbitMQ作为一款常用的消息中间件&#xff0c;在微服务项目中得到大量应用&#xff0c;其本身是微服务中的重点和难点。本篇博客是Message Queue相关的学习博客文章的合集篇&#xff0c;目前主要是RabbitMQ入门到使用文章&#xff0c;后续会扩展其他MQ。 目录 前言一、R…

C++ ini配置文件的简单读取使用

ini文件就是简单的section 下面有对应的键值对 std::map<std::string, std::map<std::string, std::string>>MyIni::readIniFile() {std::ifstream file(filename);if (!file.is_open()) {std::cerr << "Error: Unable to open file " << …

Java多线程核心技术一-基础篇synchronzied同步方法

1 概述 关键字synchronzied保障了原子性、可见性和有序性。 非线程安全问题会在多个线程对同一个对象中的同一个实例变量进行并发访问时发生&#xff0c;产生的后果就是“脏读”&#xff0c;也就是读取到的数据其实是被更改过的。而线程安全是指获取的实例变量的值是经过同步处…

年终好价节买什么好?这些数码好物闭眼入

大家是不是都没听说过好价节&#xff1f;直截了当地说&#xff0c;这其实就是原先的双十二购物狂欢节&#xff0c;只不过给它起了个新名字。不过&#xff0c;今年毕竟是首次改名&#xff0c;因此淘宝年终好价节的各种优惠&#xff0c;仍然是我们值得期待的&#xff01;作为年前…

2023.11.29 深度学习框架理解

2023.11.29 深度学习框架理解 对深度学习框架进行复习&#xff0c;选最简单的“三好学生”问题的四个变化&#xff0c;简要总结其具体思路。 深度学习一开始就是为分类问题研究的&#xff0c;因此其框架的设计都是基于分类的问题&#xff0c;虽然现在也已经演变为可以执行多种…

Java中的JMX的使用

文章目录 1. 定义和存在的意义2. 架构2.1 Instrumentation2.2 JMX Agent2.3 Remote Management 3. 启动和连接3.1 注册MBean3.2 有两个方式启动JMX Agent3.3 Remote Management(客户端) 4. MBeanServerConnection使用4.1 列出所有的MBean4.2 列出所有的Domain4.3 MBean计数4.4 …

Python (十六) 错误和异常

程序员的公众号&#xff1a;源1024&#xff0c;获取更多资料&#xff0c;无加密无套路&#xff01; 最近整理了一波电子书籍资料&#xff0c;包含《Effective Java中文版 第2版》《深入JAVA虚拟机》&#xff0c;《重构改善既有代码设计》&#xff0c;《MySQL高性能-第3版》&…

【Linux】基础IO--文件基础知识/文件操作/文件描述符

文章目录 一、文件相关基础知识二、文件操作1.C语言文件操作2.操作系统文件操作2.1 比特位传递选项2.2 文件相关系统调用2.3 文件操作接口的使用 三、文件描述符fd1.什么是文件描述符2.文件描述符的分配规则 一、文件相关基础知识 我们对文件有如下的认识&#xff1a; 1.文件 …

C 语言-数组

1. 数组 1.1 引入 需求&#xff1a;记录班级10个学员的成绩 需要定义10个变量存在的问题:变量名起名困难变量管理困难需求&#xff1a;记录班级1000个学员的成绩 1.2 概念 作用&#xff1a;容纳 数据类型相同 的多个数据的容器 。 特点&#xff1a; 长度不可变容纳 数据类型…

python实现two way ANOVA

文章目录 目的&#xff1a;用python实现two way ANOVA 双因素方差分析1. python代码实现1 加载python库2 加载数据3 统计样本重复次数&#xff0c;均值和方差&#xff0c;绘制箱线图4 查看people和group是否存在交互效应5 模型拟合与Two Way ANOVA&#xff1a;双因素方差分析6 …

ELFK集群部署(Filebeat+ELK) 本地收集nginx日志 远程收集多个日志

filebeat是一款轻量级的日志收集工具&#xff0c;可以在非JAVA环境下运行。 因此&#xff0c;filebeat常被用在非JAVAf的服务器上用于替代Logstash&#xff0c;收集日志信息。 实际上&#xff0c;Filebeat几乎可以起到与Logstash相同的作用&#xff0c; 可以将数据转发到Logst…

融资经理简历模板

这份简历内容&#xff0c;以综合柜员招聘需求为背景&#xff0c;我们制作了1份全面、专业且具有参考价值的简历案例&#xff0c;大家可以灵活借鉴。 融资经理简历在线编辑下载&#xff1a;百度幻主简历 求职意向 求职类型&#xff1a;全职 意向岗位&#xff1a;融资经理 …

Python Pyvis库:可视化复杂网络结构的利器

更多Python学习内容&#xff1a;ipengtao.com 大家好&#xff0c;我是涛哥&#xff0c;今天为大家分享 Python Pyvis库&#xff1a;可视化复杂网络结构的利器&#xff0c;全文4000字&#xff0c;阅读大约12钟。 在数据科学和网络分析领域&#xff0c;理解和可视化复杂网络结构是…

JeecgBoot低代码开发—Vue3版前端入门教程

JeecgBoot低代码开发—Vue3版前端入门教程 后端接口配置VUE3 必备知识1.vue3新特性a. https://v3.cn.vuejs.org/b.setup的用法c.ref 和 reactive 的用法d.新版 v-model 的用法e.script setup的用法 2.TypeScript基础 后端接口配置 如何修改后台项目路径 http://127.168.3.52:8…

Ubuntu systemd-analyze命令(系统启动性能分析工具:分析系统启动时间,找出可能导致启动缓慢的原因)

文章目录 Ubuntu systemd-analyze命令剖析目录简介systemd与systemd-analyze工作原理 安装和使用命令参数详解用例与示例显示启动时间&#xff08;systemd-analyze time&#xff09;列出启动过程中各个服务的启动时间&#xff08;systemd-analyze blame&#xff09;显示系统启动…

R语言单因素方差分析+差异显著字母法标注+逐行详细解释

R语言单因素方差分析 代码如下 df <- read.csv("data.csv",header TRUE,row.names 1) library(reshape2) df <- melt(df,idc()) names(df) <- c(trt, val) df aov1 <- aov(val~trt,datadf) summary(aov1)library(agricolae) data <- LSD.test(aov…

windows远程桌面登录,提示:“出现身份验证错误,要求的函数不受支持”

问题&#xff1a; windows登录远程桌面&#xff0c;提示&#xff1a;“出现身份验证错误&#xff0c;要求的函数不受支持”&#xff0c;如下图&#xff1a; 问题原因&#xff1a; windows系统更新&#xff0c;微软系统补丁的更新将 CredSSP 身份验证协议的默认设置进行了调…

【腾讯云云上实验室-向量数据库】个人对腾讯云向量数据库的体验心得

目录 前言Tencent Cloud VectorDB概念使用初体验腾讯云向量数据库的优势应用场景有哪些&#xff1f;未来展望番外篇&#xff1a;腾讯云向量数据库的设计核心结语 前言 还是那句话&#xff0c;不用多说想必大家都能猜到&#xff0c;现在技术圈最火的是什么&#xff1f;非人工智…

ZZULIOJ 2466: 楼上瞎说,楼下才是,Java

2466: 楼上瞎说&#xff0c;楼下才是 题目描述 《九章算术》的内容十分丰富&#xff0c;全书采用问题集的形式&#xff0c;收有246个与生产、生活实践有联系的应用问题&#xff0c;其中每道题有问&#xff08;题目&#xff09;、答&#xff08;答案&#xff09;、术&#xff…

【Vulnhub 靶场】【DriftingBlues: 9 (final)】【简单】【20210509】

1、环境介绍 靶场介绍&#xff1a;https://www.vulnhub.com/entry/driftingblues-9-final,695/ 靶场下载&#xff1a;https://download.vulnhub.com/driftingblues/driftingblues9.ova 靶场难度&#xff1a;简单 发布日期&#xff1a;2021年05月09日 文件大小&#xff1a;738 …