运算符重载的格式为:

  1. 返回值类型 operator 运算符名称 (形参表列){
  2. //TODO:
  3. }

一.在类里面实例运行符重载

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class complex{
  5. public:
  6. complex();
  7. complex(double real, double imag);
  8. public:
  9. //声明运算符重载
  10. complex operator+(const complex &A) const;
  11. void display() const;
  12. private:
  13. double m_real; //实部
  14. double m_imag; //虚部
  15. };
  16.  
  17. complex::complex(): m_real(0.0), m_imag(0.0){ }
  18. complex::complex(double real, double imag): m_real(real), m_imag(imag){ }
  19.  
  20. //实现运算符重载
  21. complex complex::operator+(const complex &A) const{
  22. complex B;
  23. B.m_real = this->m_real + A.m_real;
  24. B.m_imag = this->m_imag + A.m_imag;
  25. return B;
  26. }
  27.  
  28. void complex::display() const{
  29. cout<<m_real<<" + "<<m_imag<<"i"<<endl;
  30. }
  31.  
  32. int main(){
  33. complex c1(4.3, 5.8);
  34. complex c2(2.4, 3.7);
  35. complex c3;
  36. c3 = c1 + c2;
  37. c3.display();
  38.  
  39. return ;
  40. }

运行结果:

6.7 + 9.5i

1.运算符重载其实就是定义一个函数,在函数体内实现想要的功能,当用到该运算符时,编译器会自动调用这个函数。也就是说,运算符重载是通过函数实现的,它本质上是函数重载。

2.operator是关键字,专门用于定义重载运算符的函数。我们可以将operator 运算符名称这一部分看做函数名,对于上面的代码,函数名就是operator+

3.上面的例子中,我们在 complex 类中重载了运算符+,该重载只对 complex 对象有效。

二.在全局范围内重载运算符

运算符重载函数不仅可以作为类的成员函数,还可以作为全局函数。更改上面的代码,在全局范围内重载+,实现复数的加法运算:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class complex{
  5. public:
  6. complex();
  7. complex(double real, double imag);
  8. public:
  9. void display() const;
  10. //声明为友元函数
  11. friend complex operator+(const complex &A, const complex &B);
  12. private:
  13. double m_real;
  14. double m_imag;
  15. };
  16.  
  17. complex operator+(const complex &A, const complex &B);
  18.  
  19. complex::complex(): m_real(0.0), m_imag(0.0){ }
  20. complex::complex(double real, double imag): m_real(real), m_imag(imag){ }
  21. void complex::display() const{
  22. cout<<m_real<<" + "<<m_imag<<"i"<<endl;
  23. }
  24.  
  25. //在全局范围内重载+
  26. complex operator+(const complex &A, const complex &B){
  27. complex C;
  28. C.m_real = A.m_real + B.m_real;
  29. C.m_imag = A.m_imag + B.m_imag;
  30. return C;
  31. }
  32.  
  33. int main(){
  34. complex c1(4.3, 5.8);
  35. complex c2(2.4, 3.7);
  36. complex c3;
  37. c3 = c1 + c2;
  38. c3.display();
  39.  
  40. return ;
  41. }

运算符重载函数不是 complex 类的成员函数,但是却用到了 complex 类的 private 成员变量,所以必须在 complex 类中将该函数声明为友元函数。

当执行c3 = c1 + c2;语句时,编译器检测到+号两边都是 complex 对象,就会转换为类似下面的函数调用:

  1. c3 = operator+(c1, c2);

三.重载数学运算符(+-*/)

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. //复数类
  6. class Complex{
  7. public: //构造函数
  8. Complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ }
  9. public: //运算符重载
  10. //以全局函数的形式重载
  11. friend Complex operator+(const Complex &c1, const Complex &c2);
  12. friend Complex operator-(const Complex &c1, const Complex &c2);
  13. friend Complex operator*(const Complex &c1, const Complex &c2);
  14. friend Complex operator/(const Complex &c1, const Complex &c2);
  15. friend bool operator==(const Complex &c1, const Complex &c2);
  16. friend bool operator!=(const Complex &c1, const Complex &c2);
  17. //以成员函数的形式重载
  18. Complex & operator+=(const Complex &c);
  19. Complex & operator-=(const Complex &c);
  20. Complex & operator*=(const Complex &c);
  21. Complex & operator/=(const Complex &c);
  22. public: //成员函数
  23. double real() const{ return m_real; }
  24. double imag() const{ return m_imag; }
  25. private:
  26. double m_real; //实部
  27. double m_imag; //虚部
  28. };
  29.  
  30. //重载+运算符
  31. Complex operator+(const Complex &c1, const Complex &c2){
  32. Complex c;
  33. c.m_real = c1.m_real + c2.m_real;
  34. c.m_imag = c1.m_imag + c2.m_imag;
  35. return c;
  36. }
  37. //重载-运算符
  38. Complex operator-(const Complex &c1, const Complex &c2){
  39. Complex c;
  40. c.m_real = c1.m_real - c2.m_real;
  41. c.m_imag = c1.m_imag - c2.m_imag;
  42. return c;
  43. }
  44. //重载*运算符 (a+bi) * (c+di) = (ac-bd) + (bc+ad)i
  45. Complex operator*(const Complex &c1, const Complex &c2){
  46. Complex c;
  47. c.m_real = c1.m_real * c2.m_real - c1.m_imag * c2.m_imag;
  48. c.m_imag = c1.m_imag * c2.m_real + c1.m_real * c2.m_imag;
  49. return c;
  50. }
  51. //重载/运算符 (a+bi) / (c+di) = [(ac+bd) / (c²+d²)] + [(bc-ad) / (c²+d²)]i
  52. Complex operator/(const Complex &c1, const Complex &c2){
  53. Complex c;
  54. c.m_real = (c1.m_real*c2.m_real + c1.m_imag*c2.m_imag) / (pow(c2.m_real, ) + pow(c2.m_imag, ));
  55. c.m_imag = (c1.m_imag*c2.m_real - c1.m_real*c2.m_imag) / (pow(c2.m_real, ) + pow(c2.m_imag, ));
  56. return c;
  57. }
  58. //重载==运算符
  59. bool operator==(const Complex &c1, const Complex &c2){
  60. if( c1.m_real == c2.m_real && c1.m_imag == c2.m_imag ){
  61. return true;
  62. }else{
  63. return false;
  64. }
  65. }
  66. //重载!=运算符
  67. bool operator!=(const Complex &c1, const Complex &c2){
  68. if( c1.m_real != c2.m_real || c1.m_imag != c2.m_imag ){
  69. return true;
  70. }else{
  71. return false;
  72. }
  73. }
  74.  
  75. //重载+=运算符
  76. Complex & Complex::operator+=(const Complex &c){
  77. this->m_real += c.m_real;
  78. this->m_imag += c.m_imag;
  79. return *this;
  80. }
  81. //重载-=运算符
  82. Complex & Complex::operator-=(const Complex &c){
  83. this->m_real -= c.m_real;
  84. this->m_imag -= c.m_imag;
  85. return *this;
  86. }
  87. //重载*=运算符
  88. Complex & Complex::operator*=(const Complex &c){
  89. this->m_real = this->m_real * c.m_real - this->m_imag * c.m_imag;
  90. this->m_imag = this->m_imag * c.m_real + this->m_real * c.m_imag;
  91. return *this;
  92. }
  93. //重载/=运算符
  94. Complex & Complex::operator/=(const Complex &c){
  95. this->m_real = (this->m_real*c.m_real + this->m_imag*c.m_imag) / (pow(c.m_real, ) + pow(c.m_imag, ));
  96. this->m_imag = (this->m_imag*c.m_real - this->m_real*c.m_imag) / (pow(c.m_real, ) + pow(c.m_imag, ));
  97. return *this;
  98. }
  99.  
  100. int main(){
  101. Complex c1(, );
  102. Complex c2(, );
  103. Complex c3(, );
  104. Complex c4(, );
  105. Complex c5(, );
  106. Complex c6(, );
  107.  
  108. Complex c7 = c1 + c2;
  109. Complex c8 = c1 - c2;
  110. Complex c9 = c1 * c2;
  111. Complex c10 = c1 / c2;
  112. cout<<"c7 = "<<c7.real()<<" + "<<c7.imag()<<"i"<<endl;
  113. cout<<"c8 = "<<c8.real()<<" + "<<c8.imag()<<"i"<<endl;
  114. cout<<"c9 = "<<c9.real()<<" + "<<c9.imag()<<"i"<<endl;
  115. cout<<"c10 = "<<c10.real()<<" + "<<c10.imag()<<"i"<<endl;
  116.  
  117. c3 += c1;
  118. c4 -= c2;
  119. c5 *= c2;
  120. c6 /= c2;
  121. cout<<"c3 = "<<c3.real()<<" + "<<c3.imag()<<"i"<<endl;
  122. cout<<"c4 = "<<c4.real()<<" + "<<c4.imag()<<"i"<<endl;
  123. cout<<"c5 = "<<c5.real()<<" + "<<c5.imag()<<"i"<<endl;
  124. cout<<"c6 = "<<c6.real()<<" + "<<c6.imag()<<"i"<<endl;
  125.  
  126. if(c1 == c2){
  127. cout<<"c1 == c2"<<endl;
  128. }
  129. if(c1 != c2){
  130. cout<<"c1 != c2"<<endl;
  131. }
  132.  
  133. return ;
  134. }

运行结果:

c7 = 35 + 55i
c8 = 15 + 15i
c9 = -450 + 850i
c10 = 1.9 + -0.3i
c3 = 26 + 37i
c4 = -6 + -11i
c5 = 220 + 4460i
c6 = 5.2 + 1.592i
c1 != c2

四.重载下标运算符[]

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Array{
  5. public:
  6. Array(int length = );
  7. ~Array();
  8. public:
  9. int & operator[](int i);
  10. const int & operator[](int i) const;
  11. public:
  12. int length() const { return m_length; }
  13. void display() const;
  14. private:
  15. int m_length; //数组长度
  16. int *m_p; //指向数组内存的指针
  17. };
  18.  
  19. Array::Array(int length): m_length(length){
  20. if(length == ){
  21. m_p = NULL;
  22. }else{
  23. m_p = new int[length];
  24. }
  25. }
  26.  
  27. Array::~Array(){
  28. delete[] m_p;
  29. }
  30.  
  31. int& Array::operator[](int i){
  32. return m_p[i];
  33. }
  34.  
  35. const int & Array::operator[](int i) const{
  36. return m_p[i];
  37. }
  38.  
  39. void Array::display() const{
  40. for(int i = ; i < m_length; i++){
  41. if(i == m_length - ){
  42. cout<<m_p[i]<<endl;
  43. }else{
  44. cout<<m_p[i]<<", ";
  45. }
  46. }
  47. }
  48.  
  49. int main(){
  50. int n;
  51. cin>>n;
  52.  
  53. Array A(n);
  54. for(int i = , len = A.length(); i < len; i++){
  55. A[i] = i * ;
  56. }
  57. A.display();
  58.  
  59. const Array B(n);
  60. cout<<B[n-]<<endl; //访问最后一个元素
  61.  
  62. return ;
  63. }

运行结果:

0, 5, 10, 15, 20

C++语言基础(17)-运算符重载的更多相关文章

  1. C语言基础知识-运算符与表达式

    C语言基础知识-运算符与表达式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常用运算符分类 1>.算术运算符 用于处理四则运算. 2>.赋值运算符 用于将表达式的 ...

  2. GO学习-(6) Go语言基础之运算符

    Go语言基础之运算符 运算符用于在程序运行时执行数学或逻辑运算. 运算符 Go 语言内置的运算符有: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 算数运算符 运算符 描述 + 相加 - ...

  3. C++_基础_运算符重载2

    内容: (1)只能用成员形式重载的运算符 (2)new/delete操作符的重载 (3)封装和继承的初识 (4)继承的特性 (5)子类及其函数的特性 (6)多重继承和虚继承 1.只能用成员形式重载的运 ...

  4. C++学习6-面向对象编程基础(运算符重载、类的派生与继承、命名空间)

    运算符重载 重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.重载的运算符是遵循函数重载的选择原则,根据不同类型或不同参数来选择不同的重载运算符. 运 ...

  5. C#-语言基础+数据类型+运算符

    一.C#语言基础 新建项目:文件→新建→项目→Visual C#(默认.NET Framework 4.5)→控制台应用程序 1.项目结构 (1)项目后缀 .config ——配置文件(存放配置参数文 ...

  6. C语言基础学习运算符-赋值运算符

    简单赋值 在C语言里,运算符=并不表示相等,而是一个赋值运算符.这意味着,符号=的左边该是一个可修改的变量名,而右边是赋给该变量的值. 如下程序语句: i = i+; 在数学上,该等式无法成立.而作为 ...

  7. C++_基础_运算符重载

    内容: (1)输入输出运算符重载 (2)友元类和友元函数 (3)双目运算符重载 (4)单目运算符重载 (5)不能被重载的运算符 (6)只能定义为成员形式的运算符 1.输入输出运算符重载如: int n ...

  8. [Python学习笔记1]Python语言基础 数学运算符 字符串 列表

    这个系列是我在学习Python语言的过程中记录的笔记,主要是一些知识点汇总,而非学习教程,可供有一定编程基础者参考.文中偏见和不足难以避免,仅供参考,欢迎批评指正. 本系列笔记主要参考文献是官网文档: ...

  9. C语言基础学习运算符-基本算术运算符

    C语言中用于基本算术运算的运算符有:+,-,*,%,/.这些运算符的用法和你想像到的基本无异: 加法运算符 “+”使得它两侧的值被加到一起. 减法运算符“-”用它前面的数减去后面的数. 乘法由“*”表 ...

随机推荐

  1. jmeter 线程组之间的参数传递

    http://www.cnblogs.com/wnfindbug/p/5817277.html 场景测试中,一次登录后做多个接口的操作,然后登录后的uid需要关联传递给其他接口发送请求的时候使用. 1 ...

  2. [BZOJ 1228] E&D

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1228 Solution: 感觉自己对博弈论的理论一直了解得不够透彻 一篇讲原理的文章:S ...

  3. AtCoder - 2568 Lotus Leaves

    Problem Statement There is a pond with a rectangular shape. The pond is divided into a grid with H r ...

  4. STL之search

    描述 使用STL中的search函数,判断一个序列是否是另一个序列的子序列. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { vector<int> ...

  5. TZOJ 数据结构实验:创建单链表

    描述 实现一个函数CreateLinkList,能够完成不带头节点链表的创建. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. void PrintLinkList(Node *head ...

  6. 使用Maven下载jar包

    有些开源项目不直接提供jar包的下载,而是建议使用Maven下载,以开源库hipster(https://github.com/citiususc/hipster,http://www.hipster ...

  7. iOS开发——使用Autolayout生成动态高度的TableViewCell单元格

    步骤一.TableViewCell中使用Autolayout 要点:Cell的高度必须在Constraints中指明,但不能定死,需要让内部由内容决定高度的View决定动态高度. 如UILabel设置 ...

  8. js splice()方法

    splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目. 注释:该方法会改变原始数组. 实例 例子 1 在本例中,我们将创建一个新数组,并向其添加一个元素: <script ty ...

  9. cs-SelectTree-DropTreeNode, SelectTreeList

    ylbtech-Unitity: cs-SelectTree-DropTreeNode, SelectTreeList DropTreeNode.cs SelectTreeList.cs 1.A,效果 ...

  10. android BSP与硬件相关子系统读书笔记(1)android BSP移植综述

    从linux驱动转行至Android驱动开发大半年了,一开始就产生了一个很纠结目标和问题,就是不停的google如何porting android!这个问题得到的结果对于初出茅庐的我,感到迷惘.随着工 ...