-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex
50 lines (45 loc) · 1010 Bytes
/
complex
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
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
Complex(float r=0, float i=0) {
real = r;
imag = i;
}
~Complex() {};
Complex add(const Complex &c) {
Complex result;
result.real = real + c.real;
result.imag = imag + c.imag;
return result;
}
Complex operator+(const Complex &c)
{
Complex result;
result.real = real + c.real;
result.imag = imag + c.imag;
return result;
}
friend ostream &operator<<(ostream&os, Complex &C) ;
friend Complex operator+(const Complex &c1, const Complex &c2) ;
}
Complex operator+(const Complex &c1, const Complex &c2)
{
Complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}
ostream &operator<<(ostream&os, Complex &C)
os << "( " << C.real << " + " << C.imag << "i ) " << endl;
return os;
}
int main() {
Complex C1(3,2);
Complex C2(2,3);
Complex C3;
C3 = C1 + C2;
cout << C3 << endl;