-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComplex_DefiningOperators.cpp
More file actions
73 lines (61 loc) · 1.08 KB
/
Copy pathComplex_DefiningOperators.cpp
File metadata and controls
73 lines (61 loc) · 1.08 KB
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
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imaginary;
int Magnitude2()
{
return real*real + imaginary*imaginary;
}
Complex()
{
real=0;
imaginary=0;
}
public:
Complex(int real, int imaginary)
{
this->real = real;
this->imaginary = imaginary;
}
Complex operator+(Complex& rhs)
{
Complex temp = *new Complex;
temp.real = this->real + rhs.real;
temp.imaginary = this->imaginary + rhs.imaginary;
return temp;
}
Complex operator-(Complex& rhs)
{
Complex temp = *new Complex;
temp.real = this->real - rhs.real;
temp.imaginary = this->imaginary - rhs.imaginary;
return temp;
}
Complex operator*(Complex& rhs)
{
Complex temp = *new Complex;
temp.real = this->real*rhs.real - this->imaginary*rhs.imaginary;
temp.imaginary = this->real*rhs.imaginary + this->imaginary*rhs.real;
return temp;
}
void Print()
{
cout << this->real <<"+";
cout << this->imaginary << "i" << endl;
}
};
int main()
{
Complex a(4,5);
Complex b(8,1);
Complex c=a+b;
c.Print();
c=a-b;
c.Print();
c=a*b;
c.Print();
return 0;
}