page contents

C++重载数学运算符(实例演示)

需要注意的是,我们以全局函数的形式重载了 +、-、*、/、==、!=,以成员函数的形式重载了 +=、-=、*=、/=,而且应该坚持这样做,不能一股脑都写作成员函数或者全局函数。

四则运算符(+、-、*、/、+=、-=、*=、/=)和关系运算符(>、<、<=、>=、==、!=)都是数学运算符,它们在实际开发中非常常见,被重载的几率也很高,并且有着相似的重载格式。本节以复数类 Complex 为例对它们进行重载,重在演示运算符重载的语法以及规范。

复数能够进行完整的四则运算,但不能进行完整的关系运算:我们只能判断两个复数是否相等,但不能比较它们的大小,所以不能对 >、<、<=、>= 进行重载。下面是具体的代码:



  1. #include <iostream>

  2. #include <cmath>

  3. using namespace std;

  4. //复数类

  5. class Complex{

  6. public:  //构造函数

  7. Complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ }

  8. public:  //运算符重载

  9. //以全局函数的形式重载

  10. friend Complex operator+(const Complex &c1, const Complex &c2);

  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 bool operator==(const Complex &c1, const Complex &c2);

  15. friend bool operator!=(const Complex &c1, const Complex &c2);

  16. //以成员函数的形式重载

  17. Complex & operator+=(const Complex &c);

  18. Complex & operator-=(const Complex &c);

  19. Complex & operator*=(const Complex &c);

  20. Complex & operator/=(const Complex &c);

  21. public:  //成员函数

  22. double real() const{ return m_real; }

  23. double imag() const{ return m_imag; }

  24. private:

  25. double m_real;  //实部

  26. double m_imag;  //虚部

  27. };

  28. //重载+运算符

  29. Complex operator+(const Complex &c1, const Complex &c2){

  30. Complex c;

  31. c.m_real = c1.m_real + c2.m_real;

  32. c.m_imag = c1.m_imag + c2.m_imag;

  33. return c;

  34. }

  35. //重载-运算符

  36. Complex operator-(const Complex &c1, const Complex &c2){

  37. Complex c;

  38. c.m_real = c1.m_real - c2.m_real;

  39. c.m_imag = c1.m_imag - c2.m_imag;

  40. return c;

  41. }

  42. //重载*运算符  (a+bi) * (c+di) = (ac-bd) + (bc+ad)i

  43. Complex operator*(const Complex &c1, const Complex &c2){

  44. Complex c;

  45. c.m_real = c1.m_real * c2.m_real - c1.m_imag * c2.m_imag;

  46. c.m_imag = c1.m_imag * c2.m_real + c1.m_real * c2.m_imag;

  47. return c;

  48. }

  49. //重载/运算符  (a+bi) / (c+di) = [(ac+bd) / (c²+d²)] + [(bc-ad) / (c²+d²)]i

  50. Complex operator/(const Complex &c1, const Complex &c2){

  51. Complex c;

  52. c.m_real = (c1.m_real*c2.m_real + c1.m_imag*c2.m_imag) / (pow(c2.m_real, 2) + pow(c2.m_imag, 2));

  53. c.m_imag = (c1.m_imag*c2.m_real - c1.m_real*c2.m_imag) / (pow(c2.m_real, 2) + pow(c2.m_imag, 2));

  54. return c;

  55. }

  56. //重载==运算符

  57. bool operator==(const Complex &c1, const Complex &c2){

  58. if( c1.m_real == c2.m_real && c1.m_imag == c2.m_imag ){

  59. return true;

  60. }else{

  61. return false;

  62. }

  63. }

  64. //重载!=运算符

  65. bool operator!=(const Complex &c1, const Complex &c2){

  66. if( c1.m_real != c2.m_real || c1.m_imag != c2.m_imag ){

  67. return true;

  68. }else{

  69. return false;

  70. }

  71. }

  72. //重载+=运算符

  73. Complex & Complex::operator+=(const Complex &c){

  74. this->m_real += c.m_real;

  75. this->m_imag += c.m_imag;

  76. return *this;

  77. }

  78. //重载-=运算符

  79. Complex & Complex::operator-=(const Complex &c){

  80. this->m_real -= c.m_real;

  81. this->m_imag -= c.m_imag;

  82. return *this;

  83. }

  84. //重载*=运算符

  85. Complex & Complex::operator*=(const Complex &c){

  86. this->m_real = this->m_real * c.m_real - this->m_imag * c.m_imag;

  87. this->m_imag = this->m_imag * c.m_real + this->m_real * c.m_imag;

  88. return *this;

  89. }

  90. //重载/=运算符

  91. Complex & Complex::operator/=(const Complex &c){

  92. this->m_real = (this->m_real*c.m_real + this->m_imag*c.m_imag) / (pow(c.m_real, 2) + pow(c.m_imag, 2));

  93. this->m_imag = (this->m_imag*c.m_real - this->m_real*c.m_imag) / (pow(c.m_real, 2) + pow(c.m_imag, 2));

  94. return *this;

  95. }

  96. int main(){

  97. Complex c1(25, 35);

  98. Complex c2(10, 20);

  99. Complex c3(1, 2);

  100. Complex c4(4, 9);

  101. Complex c5(34, 6);

  102. Complex c6(80, 90);

  103. Complex c7 = c1 + c2;

  104. Complex c8 = c1 - c2;

  105. Complex c9 = c1 * c2;

  106. Complex c10 = c1 / c2;

  107. cout<<"c7 = "<<c7.real()<<" + "<<c7.imag()<<"i"<<endl;

  108. cout<<"c8 = "<<c8.real()<<" + "<<c8.imag()<<"i"<<endl;

  109. cout<<"c9 = "<<c9.real()<<" + "<<c9.imag()<<"i"<<endl;

  110. cout<<"c10 = "<<c10.real()<<" + "<<c10.imag()<<"i"<<endl;

  111. c3 += c1;

  112. c4 -= c2;

  113. c5 *= c2;

  114. c6 /= c2;

  115. cout<<"c3 = "<<c3.real()<<" + "<<c3.imag()<<"i"<<endl;

  116. cout<<"c4 = "<<c4.real()<<" + "<<c4.imag()<<"i"<<endl;

  117. cout<<"c5 = "<<c5.real()<<" + "<<c5.imag()<<"i"<<endl;

  118. cout<<"c6 = "<<c6.real()<<" + "<<c6.imag()<<"i"<<endl;

  119. if(c1 == c2){

  120. cout<<"c1 == c2"<<endl;

  121. }

  122. if(c1 != c2){

  123. cout<<"c1 != c2"<<endl;

  124. }

  125. return 0;

  126. }

运行结果:
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

需要注意的是,我们以全局函数的形式重载了 +、-、*、/、==、!=,以成员函数的形式重载了 +=、-=、*=、/=,而且应该坚持这样做,不能一股脑都写作成员函数或者全局函数。

attachments-2021-05-Os32WQJe60aca9971ac39.jpg

  • 发表于 2021-05-25 15:39
  • 阅读 ( 683 )
  • 分类:C/C++开发

0 条评论

请先 登录 后评论
小柒
小柒

1470 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1470 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章