详细介绍运算符重载函数,清晰明了

2024-06-01 20:44

本文主要是介绍详细介绍运算符重载函数,清晰明了,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

祝各位六一快乐~

前言

1.为什么要进行运算符重载?

C++中预定义的运算符的操作对象只能是基本数据类型。但实际上,对于许多用户自定义类型(例如类),也需要类似的运算操作。这时就必须在C++中重新定义这些运算符赋予已有运算符新的功能,使它能够用于特定类型执行特定的操作

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

2.什么是运算符重载 ?

运算符重载是通过创建运算符函数实现的,运算符函数定义了重载的运算符将要进行的操作。

1.基本知识

操作符重载,本质上就是函数重载(详细了解可点击阅读函数重载),它大大丰富了已有操作符的含义,方便使用

运算符重载格式如下:

1.函数名:operator+需要重载的运算符符号

2.函数原型:返回值类型 operator+符号(形参参数列表)

3.必须有一个类类型的参数

4.     ::    ?:    .      .*       sizeof  这五个运算符不能重载

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

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

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

8.运算符重载时必须遵循的原则

  • 重载运算符含义必须清楚;
  • 重载运算符不能有二义性。

9.算符函数重载的两种形式

  • 重载为类的成员函数
  • 重载为类的非成员函数 (非成员函数通常是友元函数)。

注:可以把一个运算符作为一个非成员、非友元函数重载。但是,这样的运算符函数访问类的私有和保护成员时,必须使用类的公有接口中提供的设置数据和读取数据的函数,调用这些函数时会降低性能。可以内联这些函数以提高性能。

补充知识:友元函数

一、友元函数的作用

  • 提供数据共享接口:为不同类之间的成员函数,以及类的成员函数与一般函数之间提供了数据共享的接口。
  • 支持类间紧密协作:当两个或多个类之间需要进行紧密的协作和交互时,友元函数允许直接访问私有成员,减少系统开销,提高效率。
  • 支持运算符重载:在某些情况下,可能需要重载运算符并操作两个不同对象之间的私有数据。此时可以将相应操作符重载函数声明为两个类的友元。

二、友元函数的特点(重点)

  • 与类的成员函数具有一样的权限:友元函数可以访问类的所有成员,包括私有成员。
  • 不属于任何类:友元函数是定义在类外的普通函数,不属于任何类。
  • 没有this指针:由于友元函数不是类的成员函数,因此它没有this指针。

三、友元函数的用法

  • 声明方式:友元函数需要在类中进行声明,前面需要加上friend关键字,可以放在公有部分也可以放在私有部分。
  • 多类友元:一个函数可以是多个类的友元函数,只需要在个各类中分别进行声明。
  • 调用方式:友元函数的调用与一般函数的调用方式和原理一致。

四、注意事项

  • 破坏封装性:友元函数破坏了类的封装性和类数据的隐藏性,因此在使用时需要谨慎考虑。
  • 避免过度使用:原则上应尽量少使用或不使用友元,除非确实能显著提高开发效率。

    2.经典运算符重载的代码示例(主要以日期类为例)

2.1operator+,operator-,operator+=,operator-=

以复数类为例

代码

#include<iostream>
using namespace std;//负数类
class complex
{
public:complex(double r = 0, double i = 0) :_real(r), _imag(i) {}complex operator +(const complex& c); //+运算符complex operator -(const complex& c);//-运算符complex& operator +=(const complex& c); //+=运算符complex& operator -=(const complex& c);//-=运算符complex& operator - ();//求负运算符void Print()const{cout << "(" << _real << "," << _imag << ")" << endl;}
private:double _real;//实部double _imag;//虚部
};
complex complex::operator +(const complex& c) //+运算符
{complex tmp;tmp._real = _real + c._real;tmp._imag = _imag + c._imag;return tmp;
}
complex complex::operator -(const complex& c) //-运算符
{complex tmp;tmp._real = _real - c._real;tmp._imag = _imag - c._imag;return tmp;
}
complex& complex::operator +=(const complex& c) //+=运算符
{_real += c._real;_imag += c._imag;return *this;
}
complex& complex::operator -=(const complex& c) //-=运算符
{_real -= c._real;_imag -= c._imag;return *this;
}
complex& complex::operator - ()//求负运算符
{_real = -_real;_imag = -_imag;return *this;
}
int main()
{complex c1(3.5, 5), c2(6, 8), c3, c4, c5;c3 = c1 + c2;c3.Print();c4 = c2 - c1;c4.Print();c1 += c2;c1.Print();c2.Print();c2 -= c1;c1.Print();c2.Print();c5 = -c2;c5.Print();return 0;
}

2.2前置operator++(--),后置operator++(--)

前置++和后置++的函数名都是operator++(没错,又是函数重载),他们的区别在于前置++没有形参,后置++有一个形参int,但是我们在实际上使用时并不需要给后置++的形参int传实参,int只是为了区分前置++和后置++的标识。

前置--和后置--也是同样用形参int来区分。

以日期类为例

#include<iostream>
using namespace std;
class Date
{
public:Date(int year = 0, int month = 0, int day = 0);// 拷贝构造函数// d2(d1)Date(const Date& d);// 获取某年某月的天数int GetMonthDay(int year, int month);// 日期+=天数Date& operator+=(int day);// 日期+天数Date operator+(int day)const;// 日期-=天数Date& operator-=(int day);// 日期-天数Date operator-(int day)const;// 前置++Date& operator++();// 后置++Date operator++(int);// 后置--Date operator--(int);// 前置--Date& operator--();void print()const;// 析构函数(日期类无需清理资源,析构函数不必显示写)//void print();//~Date()//{//cout << "~Date()" << endl;//}
private:int _year, _month, _day;
};Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;
}
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}
int Date::GetMonthDay(int year, int month)
{static int MonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)){return 29;}else{return MonthDay[month];}
}
void Date::print()const
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
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){_month = 1;_year++;}}return *this;
}
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){_year--;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}
Date Date::operator-(int day)const
{Date tmp = *this;tmp -= day;return tmp;
}
void Test1()
{Date d1(2024, 4, 30);Date d2 = d1 + 3;d2.print();Date d3(2024, 12, 31);Date d5 = d3;d3 += 1;d3.print();d5 = d5 - 1;d5.print();d1 -= 30;d1.print();Date d4 = d1 - 3;d4.print();
}
// 前置++
Date& Date::operator++()
{//这里直接用刚刚实现的Date& Date::operator+=(int day)//只是++相当于day=1而已//减少了代码负担*this += 1;return *this;
}
// 后置++
Date Date::operator++(int)
{//这里直接用刚刚实现的Date& Date::operator+(int day)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;
}
void Test2()
{Date d1(2024, 6, 1);Date d2(2023, 12, 31);Date d3 = d1--;Date d4 = d2++;d3.print();d4.print();Date d5 = --d3;Date d6 = ++d4;d5.print();d6.print();
}
int main()
{//Test1();//可以自行测试Test2();
}

2.3operator<,operator<=,operator==,operator!=,operator>=,operator>

只需要实现operator<,operator==,其他的运算符重载就能轻松实现了,下面我们一起看一下吧

#include<iostream>
using namespace std;
class Date
{
public:Date(int year = 0, int month = 0, int day = 0);Date(const Date& d);// 获取某年某月的天数int GetMonthDay(int year, int month);// >运算符重载bool operator>(const Date& d);// ==运算符重载bool operator==(const Date& d);// >=运算符重载bool operator >= (const Date& d);// <运算符重载bool operator < (const Date& d);// <=运算符重载bool operator <= (const Date& d);// !=运算符重载bool operator != (const Date& d);void print()const;private:int _year, _month, _day;
};Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;
}
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}
int Date::GetMonthDay(int year, int month)
{static int MonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)){return 29;}else{return MonthDay[month];}
}
void Date::print()const
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}bool Date::operator < (const Date& d)
{if (_year < d._year){return true;}else if (_year == d._year){if (_month < d._month){return true;}else if (_month == d._month){if (_day < d._day){return true;}}}return false;
}
bool Date::operator == (const Date& d)
{return _year == d._year&&_month == d._month&&_day == d._day;
}
bool Date::operator != (const Date& d)
{return !(*this == d);
}
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);
}
int main()
{Date d1(2024, 3, 2), d2(2023, 5, 6);cout << (d1 == d2) << endl;cout << (d1 != d2) << endl;cout << (d1 <= d2) << endl;cout << (d1 >= d2) << endl;cout << (d1 < d2) << endl;cout << (d1 > d2) << endl;
}

这篇关于详细介绍运算符重载函数,清晰明了的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1022073

相关文章

揭秘未来艺术:AI绘画工具全面介绍

📑前言 随着科技的飞速发展,人工智能(AI)已经逐渐渗透到我们生活的方方面面。在艺术创作领域,AI技术同样展现出了其独特的魅力。今天,我们就来一起探索这个神秘而引人入胜的领域,深入了解AI绘画工具的奥秘及其为艺术创作带来的革命性变革。 一、AI绘画工具的崛起 1.1 颠覆传统绘画模式 在过去,绘画是艺术家们通过手中的画笔,蘸取颜料,在画布上自由挥洒的创造性过程。然而,随着AI绘画工

VMware9.0详细安装

双击VMware-workstation-full-9.0.0-812388.exe文件: 直接点Next; 这里,我选择了Typical(标准安装)。 因为服务器上只要C盘,所以我选择安装在C盘下的vmware文件夹下面,然后点击Next; 这里我把√取消了,每次启动不检查更新。然后Next; 点击Next; 创建快捷方式等,点击Next; 继续Cont

笔记本电脑屏幕模糊?6招恢复屏幕清晰!

在数字化时代的浪潮中,笔记本电脑已成为我们生活、学习和工作中不可或缺的一部分。然而,当那曾经清晰明亮的屏幕逐渐变得模糊不清时,无疑给我们的使用体验蒙上了一层阴影。屏幕模糊不仅影响视觉舒适度,更可能对我们的工作效率和眼睛健康构成威胁。 遇到笔记本电脑屏幕模糊的情况时我们应该如何解决?本文将与大家分享6个简单易懂的解决方法。 方法一:调整Windows分辨率 电脑屏幕模糊显示不清晰怎

20.Spring5注解介绍

1.配置组件 Configure Components 注解名称说明@Configuration把一个类作为一个loC容 器 ,它的某个方法头上如果注册7@Bean , 就会作为这个Spring容器中的Bean@ComponentScan在配置类上添加@ComponentScan注解。该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 <context:component-scan>@Sc

【操作系统】信号Signal超详解|捕捉函数

🔥博客主页: 我要成为C++领域大神🎥系列专栏:【C++核心编程】 【计算机网络】 【Linux编程】 【操作系统】 ❤️感谢大家点赞👍收藏⭐评论✍️ 本博客致力于知识分享,与更多的人进行学习交流 ​ 如何触发信号 信号是Linux下的经典技术,一般操作系统利用信号杀死违规进程,典型进程干预手段,信号除了杀死进程外也可以挂起进程 kill -l 查看系统支持的信号

(超详细)YOLOV7改进-Soft-NMS(支持多种IoU变种选择)

1.在until/general.py文件最后加上下面代码 2.在general.py里面找到这代码,修改这两个地方 3.之后直接运行即可

java中查看函数运行时间和cpu运行时间

android开发调查性能问题中有一个现象,函数的运行时间远低于cpu执行时间,因为函数运行期间线程可能包含等待操作。native层可以查看实际的cpu执行时间和函数执行时间。在java中如何实现? 借助AI得到了答案 import java.lang.management.ManagementFactory;import java.lang.management.Threa

Java注解详细总结

什么是注解?         Java注解是代码中的特殊标记,比如@Override、@Test等,作用是:让其他程序根据注解信息决定怎么执行该程序。         注解不光可以用在方法上,还可以用在类上、变量上、构造器上等位置。 自定义注解  现在我们自定义一个MyTest注解 public @interface MyTest{String aaa();boolean bbb()

SQL Server中,isnull()函数以及null的用法

SQL Serve中的isnull()函数:          isnull(value1,value2)         1、value1与value2的数据类型必须一致。         2、如果value1的值不为null,结果返回value1。         3、如果value1为null,结果返回vaule2的值。vaule2是你设定的值。        如

tf.split()函数解析

API原型(TensorFlow 1.8.0): tf.split(     value,     num_or_size_splits,     axis=0,     num=None,     name='split' ) 这个函数是用来切割张量的。输入切割的张量和参数,返回切割的结果。  value传入的就是需要切割的张量。  这个函数有两种切割的方式: 以三个维度的张量为例,比如说一