Guest User

Untitled

a guest
Jan 6th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. class KomplexeZahl
  5. {
  6. public:
  7.  
  8. KomplexeZahl(double r, double i) //Konstruktor
  9. {
  10. m_i = r;
  11. m_r = i;
  12. }
  13.  
  14. KomplexeZahl() //Konstruktor leer
  15. {
  16. m_i = 0;
  17. m_r = 0;
  18. }
  19.  
  20. KomplexeZahl operator+(const KomplexeZahl& rhs) const // Operation +
  21. {
  22. KomplexeZahl comp;
  23. comp.m_r = this->m_r + rhs.m_r;
  24. comp.m_i = this->m_i + rhs.m_i;
  25. return comp;
  26. }
  27.  
  28. KomplexeZahl operator*(const KomplexeZahl& rhs) const // Operation *
  29. {
  30. KomplexeZahl comp;
  31. comp.m_r = (this->m_r * rhs.m_r) - (this->m_i * rhs.m_i);
  32. comp.m_i = (rhs.m_r * this->m_i) + (this->m_r * rhs.m_i);
  33. return comp;
  34. }
  35.  
  36. KomplexeZahl operator-(const KomplexeZahl& rhs) const //Operation -
  37. {
  38. KomplexeZahl comp;
  39. comp.m_r = this->m_r - rhs.m_r;
  40. comp.m_i = this->m_i - rhs.m_i;
  41. return comp;
  42. }
  43.  
  44. //Ein- und Ausgabe
  45. friend std::ostream &operator<<(std::ostream &ostr, const KomplexeZahl &rhs);
  46. friend std::istream &operator>>(std::istream &istr, KomplexeZahl &rhs);
  47.  
  48. private:
  49.  
  50. double m_r;
  51. double m_i;
  52. };
  53.  
  54. std::ostream& operator<<(std::ostream &ostr, const KomplexeZahl &rhs)
  55. {
  56. ostr << rhs.m_r << " + i " << rhs.m_i;
  57. return ostr;
  58. }
  59. std::istream &operator>>(std::istream &istr, KomplexeZahl &rhs)
  60. {
  61. istr >> rhs.m_r;
  62. return istr;
  63. }
  64.  
  65.  
  66.  
  67. int main()
  68. {
  69. KomplexeZahl zahl1(3,4);
  70. KomplexeZahl zahl2(5,6);
  71. KomplexeZahl zahl_leer();
  72.  
  73. std::cout << zahl1+zahl2;
  74.  
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment