6 操作符重载实现复数类

image.png

(1)、(2)、(3)、(4)代码文件结构:
image.png

//Complex.h

  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

//Complex.cpp

  1. #include "Complex.h"
  2. #include "math.h"
  3. Complex::Complex(double a, double b)
  4. {
  5. this->a = a;
  6. this->b = b;
  7. }
  8. double Complex::getA()
  9. {
  10. return a;
  11. }
  12. double Complex::getB()
  13. {
  14. return b;
  15. }
  16. double Complex::getModulus()
  17. {
  18. return sqrt(a * a + b * b);
  19. }
  20. Complex Complex::operator + (const Complex& c)
  21. {
  22. double na = a + c.a;
  23. double nb = b + c.b;
  24. Complex ret(na, nb);
  25. return ret;
  26. }
  27. Complex Complex::operator - (const Complex& c)
  28. {
  29. double na = a - c.a;
  30. double nb = b - c.b;
  31. Complex ret(na, nb);
  32. return ret;
  33. }
  34. Complex Complex::operator * (const Complex& c)
  35. {
  36. double na = a * c.a - b * c.b;
  37. double nb = a * c.b + b * c.a;
  38. Complex ret(na, nb);
  39. return ret;
  40. }
  41. Complex Complex::operator / (const Complex& c)
  42. {
  43. double cm = c.a * c.a + c.b * c.b;
  44. double na = (a * c.a + b * c.b) / cm;
  45. double nb = (b * c.a - a * c.b) / cm;
  46. Complex ret(na, nb);
  47. return ret;
  48. }
  49. bool Complex::operator == (const Complex& c)
  50. {
  51. return (a == c.a) && (b == c.b);
  52. }
  53. bool Complex::operator != (const Complex& c)
  54. {
  55. return !(*this == c);
  56. }
  57. Complex& Complex::operator = (const Complex& c)
  58. {
  59. if( this != &c )
  60. {
  61. a = c.a;
  62. b = c.b;
  63. }
  64. return *this;
  65. }

//main.cpp

#include <stdio.h>

#include "Complex.h"

int main()

{

   Complex c1(1, 2);

   Complex c2(3, 6);

   Complex c3 = c2 - c1;

   Complex c4 = c1 * c3;

   Complex c5 = c2 / c1;



   printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());

   printf("c4.a = %f, c4.b = %f\n", c4.getA(), c4.getB());

   printf("c5.a = %f, c5.b = %f\n", c5.getA(), c5.getB());



   Complex c6(2, 4);



   printf("c3 == c6 : %d\n", c3 == c6);

   printf("c3 != c4 : %d\n", c3 != c4);



   (c3 = c2) = c1;



   printf("c1.a = %f, c1.b = %f\n", c1.getA(), c1.getB());

   printf("c2.a = %f, c2.b = %f\n", c2.getA(), c2.getB());

   printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());



   return 0;

}

(5)前置,后置操作符
image.png
//Complex.h

#ifndef _COMPLEX_H_

#define _COMPLEX_H_

class Complex

{

   double a;

   double b;

public:

   Complex(double a = 0, double b = 0);

   double getA();

   double getB();

   double getModulus();



   Complex operator + (const Complex& c);

   Complex operator - (const Complex& c);

   Complex operator * (const Complex& c);

   Complex operator / (const Complex& c);



   bool operator == (const Complex& c);

   bool operator != (const Complex& c);



   Complex& operator = (const Complex& c);



   Complex& operator ++ ();

   Complex operator ++ (int);

};

#endif

Complex.cpp

#include "Complex.h"

#include "math.h"

Complex::Complex(double a, double b)

{

   this->a = a;

   this->b = b;

}

double Complex::getA()

{

   return a;

}

double Complex::getB()

{

   return b;

}

double Complex::getModulus()

{

   return sqrt(a * a + b * b);

}

Complex Complex::operator + (const Complex& c)

{

   double na = a + c.a;

   double nb = b + c.b;

   Complex ret(na, nb);



   return ret;

}

Complex Complex::operator - (const Complex& c)

{

   double na = a - c.a;

   double nb = b - c.b;

   Complex ret(na, nb);



   return ret;

}

Complex Complex::operator * (const Complex& c)

{

   double na = a * c.a - b * c.b;

   double nb = a * c.b + b * c.a;

   Complex ret(na, nb);



   return ret;

}

Complex Complex::operator / (const Complex& c)

{

   double cm = c.a * c.a + c.b * c.b;

   double na = (a * c.a + b * c.b) / cm;

   double nb = (b * c.a - a * c.b) / cm;

   Complex ret(na, nb);



   return ret;

}



bool Complex::operator == (const Complex& c)

{

   return (a == c.a) && (b == c.b);

}

bool Complex::operator != (const Complex& c)

{

   return !(*this == c);

}



Complex& Complex::operator = (const Complex& c)

{

   if( this != &c )

   {

       a = c.a;

       b = c.b;

   }



   return *this;

}

Complex& Complex::operator ++ ()

{

   a = a + 1;

   b = b + 1;



   return *this;

}



Complex Complex::operator ++ (int)

{

   Complex ret(a, b);



   a = a + 1;

   b = b + 1;



   return ret;

}

1.cpp

#include <iostream>

#include <string>

using namespace std;

int main()

{

   int i = 0;

   i++;

   ++i;

   return 0;

}

2.cpp

#include <iostream>

#include <string>

using namespace std;

class Test

{

   int mValue;

public:

   Test(int i)

   {

       mValue = i;

   }



   int value()

   {

       return mValue;

   }



   Test& operator ++ ()

   {

       ++mValue;



       return *this;

   }



   Test operator ++ (int)

   {

       Test ret(mValue);



       mValue++;



       return ret;

   }

};

int main()

{

   Test t(0);



   t++;



   ++t;



   return 0;

}