当前位置: 首页 > ops >正文

c++第6天--运算符重载

  • 运算符重载概述
  • 运算符重载规则
  • 友元、成员函数重载运算符
  • 运算符重载的限制

运算符重载概述

自定义的类的对象无法像基本类型的数据那样运算与输出。

运算符重载本质上就是定义一个特殊的函数,这个函数的名字就是operater加上要重载的字符,其一般的样式就是下面的形式:

返回类型 operator运算符(参数列表) {

    // 函数体

}

重载运算符并没有改变其原来的功能,只是增加了针对自定义数据类型的运算功能。

一下为一个例子,实现复数类重载+运算符

#include<iostream>
using namespace std;class Complex{
private:double real;double imag;
public:Complex(double real = 0,double imag = 0) : real(real),imag(imag) {}Complex operator+(const Complex& other){return Complex(real + other.real,imag + other.imag);}friend Complex operator+(const Complex& first,const Complex& second);
void display()
{cout << real;if(imag > 0)cout << "+" << imag << "i";else if(imag < 0)cout << imag << "i";cout << endl; 
}
};Complex operator+(const Complex& first,const Complex& second)
{return Complex(first.real + second.real,first.imag + second.imag);
}
int main()
{Complex c1(1,2),c2(3,-4);Complex c3 = c1 + c2;Complex c4 = c1 + c2;cout << "c1 = ";c1.display();cout << "c2 = ";c2.display();cout << "c3 = ";c3.display();cout << "c4 = ";c4.display();return 0;
}

运算符重载的方式:

有两种,分别是成员函数重载与友元函数重载

  那么什么是成员函数重载? 运算符重载函数可以作为类的成员函数,在成员的函数重载中,this指针会隐式地调用该运算符的对象,因此参数列表中的参数个数会比运算符时机操作数少一个(一元运算符无参数,二元运算符有一个参数)

//运算符重载有两种方式,一种是成员运算符重载,一种是友元函数重载
#include<iostream>
using namespace std;class Complex{
private:double real;double imag;
public:Complex(double real = 0,double imag = 0) : real(real),imag(imag) {}//成员函数重载改+运算符Complex operator+(const Complex& other) const{return Complex(real + other.real,imag + other.imag);}void display() const{cout << real;if(imag > 0) cout << " + " << imag << "i";else if(imag < 0)cout << "-" << -imag << "i";cout << endl;}
};int main()
{Complex c1(2,3),c2(4,-5);Complex c3 = c1 + c2;cout << "c1 = ";  c1.display();cout << "c2 = ";  c2.display();cout << "c3 = ";  c3.display();return 0;
}

友元函数重载:友元函数不属于类的成员函数,但是可以访问类的私有成员,使用友元函数重载时,参数列表中的参数个数和运算符的实际操作数相同。

//现在写一个友元函数的实现函数运算符重载+号
#include<iostream>
using namespace std;class Complex{
private:double real;double imag;
public:Complex(double real = 0,double imag = 0) : real(real),imag(imag) {}//友元函数重载+运算符friend Complex operator+(const Complex& c1,const Complex& c2);void display() const{cout << real;if(imag > 0)cout << "+" << imag << "i";else if(imag < 0)cout << imag << "i";cout << endl;}
};Complex operator+(const Complex& c1,const Complex& c2){return Complex(c1.real + c2.real, c1.imag + c2.imag);}int main(){Complex c1(2,3),c2(4,-5);Complex c3 = c1 + c2;cout << "c1 = "; c1.display();cout << "c2 = "; c2.display();cout << "c3 = "; c3.display();return 0;}

运算符重载的限制:

* 不能改变运算符的优先级和结合性,运算符重载只是重新定义了运算符对于自定义类型的操作行为,但不能改变运算符原有的优先级和结合性。

* 不能创造新的运算符:只能对已有的运算符进行重载,不能创建新的运算符。

* 至少有一个操作数是自定义类型:运算符重载主要是为了让自定义类型能使用运算符,因此运算符的操作数中至少有一个是自定义的类对象。 

//一个大练习实现重载+重载-重载==
#include<iostream>
using namespace std;class Point{
private:int x;int y;
public:Point(int x = 0,int y = 0) : x(x),y(y) {}//成员函数重载 + 运算符Point operator+(const Point& other) const{return Point(x + other.x,y + other.y);}//友元函数重载 - 运算符friend  Point operator-(const Point& p1,const Point& p2); //全局函数重载 == 运算符friend bool operator==(const Point& p1,const Point& p2);//输出坐标void display() const{cout << "(" << x << ", " << y << ")" << endl;}
};//友元函数实现 - 运算符
Point operator-(const Point& p1,const Point& p2){return Point(p1.x - p2.x, p1.y - p2.y);
}//全局函数实现 == 运算符
bool operator==(const Point& p1,const Point& p2){return p1.x == p2.x && p1.y == p2.y;
}
int main() {Point p1(3, 4), p2(1, 2);Point p3 = p1 + p2;Point p4 = p1 - p2;cout << "p1 = "; p1.display();cout << "p2 = "; p2.display();cout << "p3 = "; p3.display();cout << "p4 = "; p4.display();cout << "p1 " << (p1 == p2 ? "等于" : "不等于") << " p2" << endl;return 0;
}

http://www.xdnf.cn/news/11868.html

相关文章:

  • Linux基础开发工具——yum工具
  • Flutter快速上手,入门教程
  • stm32——UART和USART
  • URL 结构说明+路由(接口)的认识
  • VSCode 工作区配置文件通用模板(CMake + Ninja + MinGW/GCC 编译器 的 C++ 或 Qt 项目)
  • 软件工程:如何在项目中把软件做好
  • Python爬虫:trafilatura 的详细使用(高效的网页正文提取工具)
  • AI自动化任务执行工具OpenManus一键启动整合包
  • k8s热更新-subPath 不支持热更新
  • 网络安全中网络诈骗的攻防博弈
  • label-studio 标注实体识别
  • 第三篇:MCP协议深度实践——从理论到生产级AI智能体系统
  • go的工具库:github.com/expr-lang/expr
  • 杰发科技AC7840——Timer修改重装载值
  • 深入解析Java17核心新特性(增强NullPointerException、强封装 JDK 内部 API、伪随机数生成器增强)
  • LVDS与GMSL和FPD-Link之间的关系
  • CppCon 2015 学习:All Your Tests are Terrible
  • Unity 中实现可翻页的 PageView
  • 【游戏科学】游戏开发中数学算法的核心与应用
  • Opencl
  • superior哥AI系列第9期:高效训练与部署:从实验室到生产环境
  • 【Linux】centos软件安装
  • ios版本的Tiktok二次安装不上,提示:Unable to Install “TikTok”
  • Android apk装机编译类型: verify、speed-profile, speed与启动耗时
  • 【学习记录】如何使用 Python 提取 PDF 文件中的内容
  • 聚沙成塔,三步成书:GitBook极简入门教程
  • 近期调试有感
  • 快速掌握MCP——Spring AI MCP包教包会
  • 01串(二进制串)与集合之间存在天然的对应关系 ← bitset
  • django ssh登录 并执行命令