【C++】类和对象(6)--运算符重载

目录

一 概念

二 运算符重载的实现

三 关于时间的所有运算符重载

四 默认赋值运算符

五 const取地址操作符重载


一 概念

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

函数名字为:关键字operator后面接需要重载的运算符符号。

函数原型:返回值类型 operator操作符(参数列表)

注意:

不能通过连接其他符号来创建新的操作符:比如operator@

重载操作符必须有一个类类型参数

用于内置类型的运算符,其含义不能改变,例如:内置的整型 + ,不能改变其含义

作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this

总之如何去比较自定义类型, 并且要有可读性, 那就需要运算符重载

二 运算符重载的实现

// 全局的operator==
class Date
{
public:Date(int year = 1900, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//private:int _year;int _month;int _day;
};
// 这里会发现运算符重载成全局的就需要成员变量是公有的,那么问题来了,封装性如何保证?
// 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。
bool operator==(const Date& d1, const Date& d2)
{return d1._year == d2._year//如果成员变量不是共有的那就访问不到了&& d1._month == d2._month&& d1._day == d2._day;
}
void Test1()
{Date d1(2018, 9, 26);Date d2(2018, 9, 27);/*cout << operator>(d1, d2) << endl;cout << operator==(d1, d2) << endl;*/cout << (d1 == d2) << endl;
}int main()
{Test1();return 0;
}

现在我们把==运算符重载到成员函数中

// 全局的operator==
class Date
{
public:Date(int year = 1900, int month = 1, int day = 1){_year = year;_month = month;_day = day;}bool operator==(const Date& dd2){return _year == dd2._year&& _month == dd2._month&& _day == dd2._day;      }private:int _year;int _month;int _day;
};void Test2()
{Date d1(2018, 9, 26);Date d2(2018, 9, 27);/*cout << operator>(&d1, d2) << endl;cout << operator==(&d1, d2) << endl;*/cout << (d1 == d2) << endl;
}int main()
{Test2();return 0;
}

总结:

运算符重载 函数重载 他们之间没有关联
运算符重载:自定义类型可以直接使用运算符
函数重载:可以允许参数不同的同名函数,

内置类型对象可以直接用各种运算符,内置类型都是简单类型
语言自己定义,编译直接转换成指令
自定义类型呢?不支持  所以运算符重载诞生
不能被重载的运算符只有5个, 点号.三目运算 ? : 作用域访问符::运算符sizeof  以及.*(*是可以重载的 只是点星是不能的)
 

三 关于时间的所有运算符重载

1 Date.h

#pragma once#include<iostream>
#include<assert.h>
using namespace std;class Date
{
public://全缺省参数只需要在声明中Date(int year = 1, int month = 1, int day = 1);void Print();int GetMonthDay(int year, int month);Date& operator=(const Date& d);bool operator==(const Date& y);bool operator!=(const Date& y);bool operator>(const Date& y);bool operator<(const Date& y);bool operator>=(const Date& y);bool operator<=(const Date& y);int operator-(const Date& d);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);//友元函数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);

2 Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"int Date:: GetMonthDay(int year, int month)
{int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int day = days[month];if (month == 2 && (year % 4 == 0 && year % 100 != 0) && (year % 100 == 0)){day += 1;}return day;
}//构造函数
Date:: Date(int year, int month, int day)
{_year = year;_month = month;_day = day;
}void Date::Print()
{cout << _year << "/" << _month << "/" << _day << endl;
}//赋值运算符
Date& Date::operator=(const Date& d)
{if (*this != d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}
//赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,
//就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数bool Date::operator==(const Date& d)
{return (_day == d._day && _month == d._month && _year == d._year);
}bool Date::operator!=(const Date& d)
{return !(*this == d);
}bool Date::operator>(const Date& d)
{if (_year > d._year){return true;}else if (_year == d._year && _month > d._month){return true;}else if (_year == d._year && _month == d._month && _day > d._day){return true;}else{return false;}}bool Date:: operator>=(const Date& d)
{return (*this == d) || (*this > d);
}bool Date::operator<(const Date& d)
{return !(*this >= d);
}bool Date:: operator<=(const Date& d)
{return !(*this > d);
}//在类里面是不用区分函数顺序的
Date& Date::operator+=(int day)
{if (day < 0){return *this -= (-day);}_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){*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;
}//++d1
Date& Date::operator++()
{*this += 1;return *this;
}//d1++
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}//--d1
Date& Date::operator--()
{*this -= 1;return *this;
}//d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}//d1 -100
int Date::operator-(const Date& d)
{//假设左大右小int flag = 1;Date max = *this;Date min = d;if (*this < d){flag = -1;min = *this;max = d;}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)
{in >> d._year >> d._month >> d._day;return in;
}

 3 Test.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"void TestDate1()
{Date d1(2023, 10, 24);d1.Print();Date ret1 = d1 - 100;ret1.Print();Date ret2 = d1 - 10000;ret2.Print();Date ret3 = d1 + 100;ret3.Print();Date ret4 = d1 + 10000;ret4.Print();
}void TestDate2()
{Date d1(2023, 10, 24);d1.Print();// 语法设计,无法逻辑闭环,那么这时就只能特殊处理// 特殊处理++d1;d1.operator++();d1.Print();d1++;d1.operator++(10);d1.operator++(1);d1.Print();
}void TestDate3()
{Date d1(2023, 10, 24);d1.Print();Date d2(2024, 5, 5);d2.Print();Date d3(2024, 8, 1);d3.Print();cout << d2 - d1 << endl;cout << d1 - d3 << endl;}void TestDate4()
{Date d1(2023, 10, 24);d1 += -100;d1.Print();
}void Test5()
{Date d1(2023, 10, 21);Date d2(2023, 12, 31);d1.Print();cout << d1;cin >> d2;cout << d2 << d1 << endl;
}int main()
{TestDate1();TestDate2();TestDate3();TestDate4();Test5();return 0;
}

解释一下<< >>运算符重载

 

对友元函数不太了解的 可以【C++】类和对象(7)--友元, static成员-CSDN博客

四 默认赋值运算符

用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注 意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符 重载完成赋值。

Date MyStack这些就不用自己构造赋值运算符重载, 但是栈这些就必须要自己构造, 因为涉及到了资源的拷贝

注意:如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必 须要实现。

// 这里会发现下面的程序会崩溃 这里就需要我们以后讲的深拷贝去解决。
typedef int DataType;
class Stack
{
public:Stack(size_t capacity = 10){_array = (DataType*)malloc(capacity * sizeof(DataType));if (nullptr == _array){perror("malloc申请空间失败");return;}_size = 0;_capacity = capacity;}void Push(const DataType& data){// CheckCapacity();_array[_size] = data;_size++;}~Stack(){if (_array){free(_array);_array = nullptr;_capacity = 0;_size = 0;}}
private:DataType* _array;size_t _size;size_t _capacity;
};
int main()
{Stack s1;s1.Push(1);s1.Push(2);s1.Push(3);s1.Push(4);Stack s2;s2 = s1;return 0;
}

改正如下

typedef int DataType;
class Stack
{
public:Stack(size_t capacity = 10){_array = (DataType*)malloc(capacity * sizeof(DataType));if (nullptr == _array){perror("malloc申请空间失败");return;}_size = 0;_capacity = capacity;}void Push(const DataType& data){// CheckCapacity();_array[_size] = data;_size++;}void Pop(){_size--;}DataType Top(){return _array[_size - 1];}bool Empty(){return _size == 0;}Stack& operator=(Stack& st){_array = (int*)malloc(sizeof(int) * st._capacity);if (_array == nullptr){perror("malloc fail");exit(-1);}memcpy(_array, st._array, sizeof(int) * st._size);_size = st._size;_capacity = st._capacity;}~Stack(){if (_array){free(_array);_array = nullptr;_capacity = 0;_size = 0;}}
private:DataType* _array;size_t _size;size_t _capacity;
};
int main()
{Stack s1;s1.Push(1);s1.Push(2);s1.Push(3);s1.Push(4);while (!s1.Empty()){printf("%d ", s1.Top());s1.Pop();}printf("\n");Stack s2;s2 = s1;s2.Push(5);s2.Push(6);s2.Push(7);s2.Push(8);while (!s2.Empty()){printf("%d ", s2.Top());s2.Pop();}return 0;
}

讲一下为什么

五 const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

       Date* operator&() //返回类型为 Date*{cout << "Date* operator&()" << endl;return this;}const Date* operator&()const//返回类型为 const Date*{cout << "const Date* operator&()const" << endl;return this;}

当然不是const的地址也可以调用const类型, 只不过两个都存在的时候, 会优先调用最匹配的一个

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需 要重载,比如想让别人获取到指定的内容!

本节感觉理解起来还是比较抽象, 有时候记住咋用就行了, 祖师爷就是这样规定的, 但是底层的东西我们还是得好好琢磨一下.对于类和对象基础要求挺高. 大家可以看看我之前的博客.

继续加油!

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

信息安全相关标准

GB/T 25070-2019 信息安全技术 网络安全等级保护安全设计技术要求 在线预览|GB/T 25070-2019 (gb688.cn) 每一级安全保护环境分为安全计算环境&#xff0c;安全区域边界&#xff0c;安全通信网络。 一级通用安全计算环境 二级通用安全 计算环境 三级 通用安全计算环境 第四级…

LM(大模型)应用开发利器之LangChain,带你走进AI世界

原文&#xff1a;LLM&#xff08;大模型&#xff09;应用开发利器之LangChain&#xff0c;带你走进AI世界 - 简书 LangChain组件图 LangChain 是什么 首先 LangChain 是一个框架&#xff0c;这个框架是用来让开发者进行 LLMs &#xff08;大语言模型&#xff09;应用开发的。…

利用ETLCloud自动化流程实现业务系统数据快速同步至数仓

现代企业有不少都完成了数字化的转型&#xff0c;而还未转型的企业或商铺也有进行数字化转型的趋势&#xff0c;由此可见&#xff0c;数据已经成为企业决策的重要依据。企业需要先获取数据&#xff0c;将业务系统数据同步至数仓进行整合&#xff0c;然后再进行数据分析。为了更…

Vue3 插槽 v-slot

插槽 视频链接&#xff1a;尚硅谷vue-插槽章节 不使用插槽的情况下 结果&#xff1a; 1 默认插槽 在子组件中只能有一个 结果&#xff1a; 2 具名插槽 #b是v-slot:b 的缩写 顾名思义就是指着名字去插入 结果&#xff1a; 3 作用域插槽 可以传递数据的插槽&#…

“轻松实现文件夹批量重命名:使用顺序编号批量改名“

你是否曾经遇到过需要批量重命名文件夹&#xff0c;却因为繁琐的手动操作而感到困扰&#xff1f;现在&#xff0c;我们为你带来了一款全新的工具——轻松实现文件夹批量重命名&#xff0c;使用顺序编号批量改名。这款工具将帮助你轻松解决文件夹重命名的问题&#xff0c;提高工…

Vue3 shallowRef 和 shallowReactive

一、shallowRef 使用shallowRef之前需要进行引入&#xff1a; import { shallowRef } from vue; 使用方法和ref 的使用方法一致&#xff0c;以下是二者的区别&#xff1a; 1. 如果ref 和 shallowRef 都传入的是普通数据类型的数据&#xff0c;那么他们的效果是一样的&#x…

python基础练习题库实验1

题目1 使用以下变量 product_code“377B” product_name“牛肉汤” product_size“250mL” product_price2.15 使用字符串加法编写一个print语句&#xff0c;以便生成以下精确输出&#xff1a; 377B&#xff1a;牛肉汤&#xff0c;250mL 代码 product_code "377B"…

图像分类(二) 全面解读复现ZFNet

网络详解 前言&#xff1a;ZF网络是2013年提出的&#xff0c;网上有很多关于它的介绍和讲解&#xff0c;但是很多内容讲的不太好&#xff08;个人感觉&#xff09;&#xff0c;于是花时间收集了一些资料&#xff0c;整理了一些比较好的文章&#xff0c;从头到尾把ZFNet说了一遍…

【Python自动化】定时自动采集,并发送微信告警通知,全流程案例讲解!

文章目录 一、概要二、效果演示三、代码讲解3.1 爬虫采集行政处罚数据3.2 存MySQL数据库3.3 发送告警邮件&微信通知3.4 定时机制 四、总结 一、概要 您好&#xff01;我是马哥python说&#xff0c;一名10年程序猿。 我原创开发了一套定时自动化爬取方案&#xff0c;完整开…

Echarts柱状图配置代码详解,含常用图例代码

一、初识柱状图 从echarts官网引入基础的柱状图后&#xff0c;可以看到他有如下的配置项。我们可以改变各个配置项的属性&#xff0c;将图例调整为我们期望的效果。 二、常用配置项 因为引入echarts图例后&#xff0c;改变图例的东西都在option配置项中&#xff0c;所以其他部…

【电路笔记】-最大功率传输

最大功率传输 文章目录 最大功率传输1、概述2、最大功率传输定理 (MPTT)3、示例4、阻抗匹配5、总结 当工程师设计电子电路时&#xff0c;他们会跟踪许多不同的参数&#xff0c;但最重要的参数之一是功率。 在现代电路中&#xff0c;功率在多个阶段中不断变化&#xff0c;有时由…

CF1899 G. Unusual Entertainment [二维数点/二维偏序]

传送门:CF [前题提要]:没什么好说的,区域赛爆炸之后发愤加训思维题.秒了div3 A~F的脑筋急转弯,然后被G卡了,树剖dfs序的想法已经想到了,题目也已经化简为两个线段是否存在一个合法位置了.但是MD不会二维数点,用一个树剖扫描线搞来搞去最后还是Tle.果然如下图所说:科技还是十分…