复数类应该具有的操作
运算:+ - * /
比较:==,!=
赋值:=
求模:modulus

技术:操作符重载,使用重载技术对已经存在的操作符进行重载操作

1.利用操作符重载
—-统一复数与实数的运算方式
—-统一复数与实数的比较方式

Complex operator +(const Complex& c);
Complex operator -(const Complex& c);
Complex operator *(const Complex& c);
Complex operator /(const Complex& c);

…….

  1. #ifndef _COMPLEX_H_
  2. #define _COMPLEX_H_
  3. class Complex
  4. {
  5. double a;
  6. double b;
  7. public:
  8. Complex(double a = 0,double b = 0);
  9. double getA();
  10. double getB();
  11. double getModulus();
  12. Complex operator +(const Complex& c);
  13. Complex operator -(const Complex& c);
  14. Complex operator *(const Complex& c);
  15. Complex operator /(const Complex& c);
  16. bool operator ==(const Complex& c);
  17. bool operator !=(const Complex& c);
  18. Complex operator =(const Complex& c);
  19. };
  20. #endif
  21. #include"complex.h"
  22. #include"math.h"
  23. Complex::Complex(double a,double b)//default only exist at deleration
  24. {
  25. this->a = a;
  26. this->b = b;
  27. }
  28. double Complex::getA()
  29. {
  30. return a;
  31. }
  32. double Complex::getB()
  33. {
  34. return b;
  35. }
  36. double Complex::getModulus()
  37. {
  38. return sqrt(a*a + b*b);
  39. }
  40. Complex Complex::operator +(const Complex& c)
  41. {
  42. double na = this->a + c.a;
  43. double nb = this->b + c.b;
  44. Complex ret(na,nb);
  45. return ret;
  46. }
  47. Complex Complex::operator -(const Complex& c)
  48. {
  49. double na = this->a - c.a;
  50. double nb = this->b - c.b;
  51. Complex ret(na,nb);
  52. return ret;
  53. }
  54. Complex Complex::operator *(const Complex& c)
  55. {
  56. double na = (this->a * c.a) - (this->b * c.b);
  57. double nb = (this->a * c.b) + (this->b * c.a);
  58. Complex ret(na,nb);
  59. return ret;
  60. }
  61. Complex Complex::operator /(const Complex& c)
  62. {
  63. double cm = c.a * c.a + c.b* c.b;
  64. double na = (this->a * c.a) + (this->b * c.b) / cm;
  65. double nb = (this->b * c.a) - (this->a * c.b) / cm;
  66. Complex ret(na,nb);
  67. return ret;
  68. }
  69. bool Complex::operator ==(const Complex& c)
  70. {
  71. return (this->a == c.a)&& (this->b == c.b);
  72. }
  73. bool Complex::operator !=(const Complex& c)
  74. {
  75. return !(*this == c); //巧妙的利用了“==”
  76. }
  77. Complex& Complex::operator =(const Complex& c)
  78. {
  79. if(this !=&c)
  80. {
  81. this->a = c.a;
  82. this->b = c.b;
  83. }
  84. return *this; //return current
  85. }
  86. #include<stdio.h>
  87. #include"complex.h"
  88. int main()
  89. {
  90. Complex c1(1,2);
  91. Complex c2(3,6);
  92. Complex c3 = c2 - c1;
  93. Complex c4 = c1 * c3;
  94. Complex c5 = c2 / c1;
  95. printf("c3.a = %f,c3.b = %f\n",c3.getA(),c3.getB());
  96. printf("c4.a = %f,c4.b = %f\n",c4.getA(),c4.getB());
  97. printf("c5.a = %f,c5.b = %f\n",c5.getA(),c5.getB());
  98. Complex c6(2,4);
  99. printf("c6 == c3: %d\n",(c6 == c3));
  100. printf("c6 != c3: %d\n",(c6 != c3));
  101. Complex c7 = c6;
  102. printf("c7.a = %f,c7.b = %f\n",c7.getA(),c7.getB());
  103. (c3 = c2)= c1; //c3 = c1;
  104. printf("c1.a = %f,c1.b = %f\n",c1.getA(),c1.getB());
  105. printf("c2.a = %f,c2.b = %f\n",c2.getA(),c2.getB());
  106. printf("c3.a = %f,c3.b = %f\n",c3.getA(),c3.getB());
  107. return 0;
  108. }

注意事项:

1.c++规定赋值操作符(=)只能重载为成员函数
2.操作符重载不能改变原操作符的优先级
3.操作符重载不能改变操作数的个数
4.操作符重载不应该改变操作符的原有语义

小结:

1.复数的概念可以通过自定义类实现
2.复数中的运算操作可以通过操作符重载实现
3.赋值操作符只能通过成员函数实现
4.操作符重载的本质为函数定义