Complex.hpp 1.95 KB
Newer Older
muozturk's avatar
muozturk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <vector>

using namespace std;

class Complex {

    private:
        float real;
        float imag;


    public:

        Complex(){
            this->real = 0;
            this->imag = 0;
        }

        Complex(float real, float imag) {
            this->real = real;
            this->imag = imag;
        }


        void setReal(float real) {
            this->real = real;
        }

        void setImag(float imag) {
            this->imag = imag;
        }

        float getReal() const {
            return this->real;
        }

        float getImag() const {
            return this->imag;
        }


        Complex operator+(Complex const &obj) {
            Complex res;
            res.real = this->real + obj.real;
            res.imag = this->imag + obj.imag;
            return res;
        }

        Complex operator-(Complex const &obj) {
            Complex res;
            res.real = this->real - obj.real;
            res.imag = this->imag - obj.imag;
            return res;
        }

        Complex operator*(Complex const &obj) {
            Complex res;
            res.real = this->real * obj.real - this->imag * obj.imag;
            res.imag = this->real * obj.imag + this->imag * obj.real;
            return res;
        }

        friend bool operator!=(Complex const &obj1, Complex const &obj2) {
            return (obj1.real != obj2.real || obj1.imag != obj2.imag);
        }

        friend bool operator==(Complex const &obj1, Complex const &obj2) {
            return (obj1.real == obj2.real && obj1.imag == obj2.imag);
        }

        friend ostream& operator<<(ostream& os, const Complex& obj) {
            os << obj.real << " + " << obj.imag << "i";
            return os;
        }

        Complex complex_mult(Complex const &obj) const{
            return Complex(this->real * obj.real - this->imag * obj.imag, 
                            this->real * obj.imag + this->imag * obj.real);
        }


};