运算符重载和const成员 (日期类的实现C++)

news2024/10/7 10:25:46

运算符重载和const成员

  • const成员
    • const修饰类成员函数
    • const对象调用权限
    • 小结
  • 运算符重载

const成员

const成员函数:const修饰的成员函数。const修饰类成员函数,实际限制的是*this,表明该成员函数不能对类的任何成员进行修改。

const修饰类成员函数

const成员

const对象调用权限

const对象调用权限问题

小结

  1. const对象不能调用非const成员函数。
  2. 非const对象可以调用const成员函数。
  3. const成员函数内可以调用其他的非const成员函数。
  4. 非const成员函数内可以调用其他的const成员函数。

结论:

  1. 成员函数后面+const以后,普通和const对象都可以调用。
  2. 不是所有成员函数都可以+const,要修改对象成员变量的函数不能+
  3. 只要成员函数内部不修改成员变量,都应该+const,这样const对象和普通对象都可以调用

运算符重载

借用日期类,实现运算符的重载。为了便于查看,把成员函数采用声明和定义分离的形式,写在两个文件中。

Date.h:

#include <iostream>
#include <assert.h>
using namespace std;

class Date
{
	//友元函数声明
	friend ostream& operator<<(ostream& out, Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	//全缺省构造函数
	Date(int year = 1970, int month = 1, int day = 1);

	//打印日期
	void Print() const
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

	//拷贝构造函数
	//d2(d1)
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	//运算符重载
	//d2 = d3; <==>d2.operator(d3)
	Date& operator=(const Date& d);

	//析构函数
	~Date();

	// < 运算符重载
	bool operator<(const Date& d) const;

	// == 运算符重载
	bool operator==(const Date& d) const;

	// <= 运算符重载
	bool operator<=(const Date& d) const;

	// > 运算符重载
	bool operator>(const Date& d) const;
	
	// >= 运算符重载
	bool operator>=(const Date& d) const;

	// != 运算符重载
	bool operator!=(const Date& d) const;

	//获取某年某月的天数
	int GetMonthDay(int year, int month);

	//日期+=天数
	Date& operator+=(int day);
	
	//日期+天数
	Date operator+(int day) const;

	//前置++
	Date& operator++();

	//后置++
	Date operator++(int);

	//日期-=天数
	Date& operator-=(int day);
	
	//日期-天数	
	Date operator-(int day) const;

	//前置--
	Date& operator--();

	//后置--
	Date operator--(int);

	//日期-日期  返回天数
	int operator-(const Date& d) const;

private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& out, Date& d);
istream& operator>>(istream& in, Date& d);

Date.cpp:

#include "Date.h"

//构造函数
Date::Date(int year, int month, int day)
{
	if (month > 0 && month < 13
		&& day > 0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "非法日期" << endl;
		assert(false);
	}
}

//运算符重载
Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	return *this;
}

//析构函数
Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}

// < 运算符重载
bool Date::operator<(const Date& x) const
{
	if (_year < x._year)
	{
		return true;
	}
	else if (_year == x._year && _month < x._month)
	{
		return true;
	}
	else if (_year == x._year && _month == x._month && _day < x._day)
	{
		return true;
	}

	return false;
}

// == 运算符重载
bool Date::operator==(const Date& x) const
{
	return _year == x._year
		&& _month == x._month
		&& _day == x._day;
}

// 复用
// <= 运算符重载
bool Date::operator<=(const Date& x) const
{
	return *this < x || *this == x;
}

// > 运算符重载
bool Date::operator>(const Date& x) const
{
	return !(*this <= x);
}

// >= 运算符重载
bool Date::operator>=(const Date & x) const
{
	return !(*this < x);
}

// != 运算符重载
bool Date::operator!=(const Date& x) const
{
	return !(*this == x);
}

//获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	static int daysArr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	//if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && month == 2)
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		return 29;
	}
	else
	{
		return daysArr[month];
	}
}

//日期+=天数
Date& Date::operator+=(int 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;
}

// d1 + 100
//日期+天数
Date Date::operator+(int day)  const
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}

//日期-=天数
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}

	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_month = 12;
			--_year;
		}

		_day += GetMonthDay(_year, _month);
	}

	return *this;
}

//日期-天数
Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

// 前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

// 后置++
// 增加这个int参数不是为了接收具体的值,仅仅是占位,跟前置++构成重载
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;
}

//日期-日期  返回天数
// d1 - d2;
int Date::operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}

	return n * flag;
}

//流插入运算符重载,只能写成全局函数才能满足我们正常输入的需要,所以这里用到了友元函数
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;

	return out;
}

//流提取运算符重载
istream& operator>>(istream& in, Date& d)
{
	int year, month, day;
	in >> year >> month >> day;

	if (month > 0 && month < 13
		&& day > 0 && day <= d.GetMonthDay(year, month))
	{
		d._year = year;
		d._month = month;
		d._day = day;
	}
	else
	{
		cout << "非法日期" << endl;
		assert(false);
	}

	return in;
}

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

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

相关文章

设计模式-建造者模式在Java中使用示例

场景 建造者模式 复杂对象的组装与创建 没有人买车会只买一个轮胎或者方向盘&#xff0c;大家买的都是一辆包含轮胎、方向盘和发动机等多个部件的完整汽车。 如何将这些部件组装成一辆完整的汽车并返回给用户&#xff0c;这是建造者模式需要解决的问题。 建造者模式又称为…

复选框,购物车功能,单选,全选

<template><view class"index"><u-navbar title"购物车" :is-back"false" :border-bottom"false" title-color"#333":background"{background:#fff}"><view class"page_navbar_warp&qu…

探究ThreadLocal和ThreadPoolExecutor中的内存泄露风险与防范策略

探究ThreadLocal和ThreadPoolExecutor中的内存泄露风险与防范策略 本文将探讨ThreadLocal和ThreadPoolExecutor中可能存在的内存泄露问题&#xff0c;并提出相应的防范策略。 ThreadPoolExecutor的内存泄露问题 ThreadPoolExecutor是一个线程池类&#xff0c;它可以管理和复…

【PHP面试题39】linux下面chmod和chown使用详解

文章目录 一、前言二、什么是 chmod 命令&#xff1f;2.1 使用方法&#xff1a;2.2 数值表示法&#xff1a;2.3 符号表示法&#xff1a; 三、什么是 chown 命令&#xff1f;3.1 使用方法&#xff1a;3.2 更改所有者和用户组&#xff1a; 四、使用示例4.1 使用 chmod 命令修改权…

媒体邀约:企业新品发布会如何邀约记者到现场采访报道?

媒介易是国内领先的全媒体广告营销平台&#xff0c;专注全媒体营销平台创新服务。我们有超过近11年的实战经验&#xff0c;我们拥有丰富的媒体记者资源&#xff0c;关于邀约记者到现场采访&#xff0c;我们会采取以下步骤&#xff1a; 1、提前策划&#xff1a;在发布会前至少…

MATLAB 基于NDT的点云配准实验(不同参数效果) (25)

MATLAB 基于NDT的点云配准实验(不同参数效果) (25) 一、算法简介二、具体使用1.代码(注释详细)2.结果(不同参数 与ICP比较)一、算法简介 NDT点云配准与ICP一样,都是经典的点云配准算法,这里使用MATLAB进行ndt点云配准,对配准结果进行显示,并尝试不同参数,得到较好…

单元测试用例到底该如何设计?

目录 前言 使用参数方法创建测试用例 使用执行路径方法创建测试用例 总结 前言 最近一些大公司在进行去测试化的操作&#xff0c;这一切的根源大概可以从几年前微软一刀切砍掉所有内部正式的测试人员开始说起&#xff0c;当时微软内部的测试工程师有一部分转职成了开发工程…

STM32 Proteus仿真红外检测PWM调速温控风扇-0073

STM32 Proteus仿真红外检测PWM调速温控风扇-0073 Proteus仿真小实验&#xff1a; STM32 Proteus仿真红外检测PWM调速温控风扇-0073 功能&#xff1a; 硬件组成&#xff1a;STM32F103C6单片机 LCD1602显示器DS18B20温度传感器人检测 按下说明有人L298驱动电机模拟风扇 1.按键…

PWM呼吸灯设计

呼吸灯&#xff1a; 呼吸灯是一种特殊的灯光效果&#xff0c;它可以模拟呼吸的效果&#xff0c;即灯光逐渐由暗变亮再由亮变暗&#xff0c;循环往复。这种效果给人一种柔和、舒缓的感觉&#xff0c;常被应用在装饰、照明和显示等领域。 PWM呼吸灯设计&#xff1a; 在数字电路设…

Windows搭建Nginx实现RTMP转为HLS流

所需软件 nginx-1.7.11.3-Gryphon&#xff08;这个包含必须的RTMP模块&#xff0c;普通的Ngxin没有这个&#xff09;ffmpegVLC 配置Nginx 1为Nginx配置RTMP和HLS 这里定义了一个叫live的RTMP路径。同时设置其开启HLS功能&#xff0c;那么所有推送到这个地址的RTMP流都会自动生…

AWS MSK集群认证和加密传输的属性与配置

通常&#xff0c;身份认证和加密传输是两项不相关的安全配置&#xff0c;在Kafka/MSK上&#xff0c;身份认证和加密传输是有一些耦合关系的&#xff0c;重点是&#xff1a;对于MSK来说&#xff0c;当启用IAM, SASL/SCRAM以及TLS三种认证方式时&#xff0c;TLS加密传输是必须的&…

C++STL库中的string

文章目录 STL库对于string类的介绍 string常用接口 string类的模拟实现 string对象大小的计算 写时拷贝 前言 C语言中&#xff0c;字符串是以\0结尾的一些字符的集合&#xff0c;为了操作方便&#xff0c;C标准库中提供了一些str系列的库函数&#xff0c;但是这些库函数与字…

青龙面板集合仓库(不断更新)青龙面板,京东定时任务库,脚本库大全

文章目录 文章目录前言简易一键安装脚本库最新京东比价小插件 文章目录 前言 Faker维护仓库&#xff0c;本地sign保证CK安全防泄漏&#xff0c;收集全网目前能正常使用的脚本。 全网能用的&#xff0c;本仓库都有。有问题进群反馈。 简易一键安装 最新版青龙有可能造成脚本…

软件测试技能提升自学,如何学?

又到了年底&#xff0c;对于我们测试同学来说&#xff0c;多多少少会立一些flag。我已经被连续打脸了好几年&#xff0c;生活为什么总是这么不易&#xff1f;好了&#xff0c;不扯远了&#xff0c;我们今天的主题是自学的那些事。 学习新的技能我相信是每一个测试同学都要面对…

使用MFC CAD 的一些使用方式记录【追加ing】

1. 项目调试&#xff1a;由于项目很大&#xff0c;因此&#xff0c;我们调试的时候&#xff0c;不应该编译整个软件而是应该只编译对应的 类去做处理 2. debug 设置断点方面&#xff1a; 以往我们的操作都是在.exe直接执行文件上进行操作&#xff0c;但是&#xff0c;现在&am…

解决虚拟机安装debian系统报错The failing step is: Select and install software

一波三折&#xff0c;一直卡在这一步&#xff0c;总是到不了流行度调查以及选择软件的界面。 下面是最终正确的步骤&#xff1a; 1&#xff0c;镜像源选项选择yes&#xff1b; 2&#xff0c;镜像源地区选择china&#xff1b; 3&#xff0c;镜像源地址可选mirrors.163.com&am…

Linux离线安装Jenkins、Maven、Gitlab、Git,部署Java项目

安装Java 《Linux安装java》 安装Maven 把Maven上传到Linux服务器/data/目录下进行解压 cd /data/ && tar -zxvf apache-maven-3.9.3-bin.tar.gz配置环境变量 vim /etc/profile找到export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL在下面追加 # mave…

【C语言】约分最简式

题目描述&#xff1a; 分数可以表示为分子/分母的形式。编写一个程序&#xff0c;要求用户输入一个分数&#xff0c;然后将其约分为最简分式。最简分式是指分子和分母不具有可以约分的成分了。如6/12可以被约分为1/2。当分子大于分母时&#xff0c;不需要表达为整数又分数的形…

基于 JIT 技术的开源全场景高性能 JSON 库

大家好&#xff0c;我是Mandy&#xff0c;上一节我们对Go中的切片数据类型进行了深度的剖析&#xff0c;今天给大家分享一个字节跳动自研开源的JSON数据解析包。一个速度奇快的 JSON 序列化/反序列化库&#xff0c;由 JIT &#xff08;即时编译&#xff09;和 SIMD &#xff08…

基于亚博K210开发板——LED(RGB)点灯

文章目录 开发板实验目的实验准备查看原理图软件对应SDKGPIO配置函数什么是 FPIOA 呢 实验代码LED/RGB驱动主程序控制 实验结果 开发板 实验目的 实现开发板上LED0、LED1以及RGB灯的点亮 实验准备 查看原理图 K210 开发板出厂默认已经焊接好 LED0 和 LED1。LED0 连接的是 IO…