Advertisement
Guest User

Complex.cpp

a guest
Oct 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1.  
  2.  
  3. #include "stdafx.h"
  4.  
  5. #include "Complex.h"
  6.  
  7. Complex::Complex(): _re(0.0), _im(0.0)
  8. {
  9. }
  10.  
  11. Complex::Complex(double re, double im): _re(re), _im(im)
  12. {
  13. }
  14.  
  15. double Complex::getIm() const
  16. {
  17.     return _im;
  18. }
  19.  
  20. double Complex::getRe() const
  21. {
  22.     return _re;
  23. }
  24.  
  25. void Complex::setIm(double im)
  26. {
  27.     _im = im;
  28. }
  29.  
  30. void Complex::setRe(double re)
  31. {
  32.     _re = re;
  33. }
  34.  
  35. Complex & Complex::operator+=(const Complex & other)
  36. {
  37.     setRe(getRe() + other.getRe());
  38.     setIm(getIm() + other.getIm());
  39.     return *this;
  40. }
  41.  
  42. Complex  Complex::operator+(const Complex & other) const
  43. {
  44.     Complex result(this->getRe() + other.getRe(), this->getIm() + other.getIm());
  45.     return result;
  46. }
  47.  
  48. double Complex::operator[](int number) const
  49. {
  50.     switch (number)
  51.     {
  52.         case 0: return getRe();
  53.         case 1: return getIm();
  54.         default: return 0.0;
  55.     }
  56. }
  57.  
  58. bool Complex::operator!=(const Complex & other) const
  59. {
  60.     if (this->getRe() == other.getRe() && this->getIm() == other.getIm())
  61.         return false;
  62.     return true;
  63. }
  64.  
  65. bool Complex::operator==(const Complex & other) const
  66. {
  67.     return !(other!=(*this));
  68. }
  69.  
  70. Complex & Complex::operator++()
  71. {
  72.     this->setRe(this->getRe() + 1.0);
  73.     this->setIm(this->getIm() + 1.0);
  74.     return *this;
  75. }
  76.  
  77. Complex Complex::operator++(int)
  78. {
  79.     Complex temp(this->getIm(),this-> getRe());
  80.     ++(*this);
  81.     return temp;
  82. }
  83.  
  84. void Complex::operator()(double re, double im)
  85. {
  86.     this->setRe(re);
  87.     this->setIm(im);
  88. }
  89.  
  90. Complex & Complex::operator=(const Complex & other)
  91. {
  92.     this->setRe(other.getRe());
  93.     this->setIm(other.getIm());
  94.     return *this;
  95. }
  96.  
  97. Complex & operator+(Complex & complex, const double & d)
  98. {
  99.     complex.setIm(complex.getIm() + d);
  100.     complex.setRe(complex.getRe() + d);
  101.     return complex;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement