Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- class KomplexeZahl
- {
- public:
- KomplexeZahl(double r, double i) //Konstruktor
- {
- m_i = r;
- m_r = i;
- }
- KomplexeZahl() //Konstruktor leer
- {
- m_i = 0;
- m_r = 0;
- }
- KomplexeZahl operator+(const KomplexeZahl& rhs) const // Operation +
- {
- KomplexeZahl comp;
- comp.m_r = this->m_r + rhs.m_r;
- comp.m_i = this->m_i + rhs.m_i;
- return comp;
- }
- KomplexeZahl operator*(const KomplexeZahl& rhs) const // Operation *
- {
- KomplexeZahl comp;
- comp.m_r = (this->m_r * rhs.m_r) - (this->m_i * rhs.m_i);
- comp.m_i = (rhs.m_r * this->m_i) + (this->m_r * rhs.m_i);
- return comp;
- }
- KomplexeZahl operator-(const KomplexeZahl& rhs) const //Operation -
- {
- KomplexeZahl comp;
- comp.m_r = this->m_r - rhs.m_r;
- comp.m_i = this->m_i - rhs.m_i;
- return comp;
- }
- //Ein- und Ausgabe
- friend std::ostream &operator<<(std::ostream &ostr, const KomplexeZahl &rhs);
- friend std::istream &operator>>(std::istream &istr, KomplexeZahl &rhs);
- private:
- double m_r;
- double m_i;
- };
- std::ostream& operator<<(std::ostream &ostr, const KomplexeZahl &rhs)
- {
- ostr << rhs.m_r << " + i " << rhs.m_i;
- return ostr;
- }
- std::istream &operator>>(std::istream &istr, KomplexeZahl &rhs)
- {
- istr >> rhs.m_r;
- return istr;
- }
- int main()
- {
- KomplexeZahl zahl1(3,4);
- KomplexeZahl zahl2(5,6);
- KomplexeZahl zahl_leer();
- std::cout << zahl1+zahl2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment