-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.cpp
More file actions
52 lines (47 loc) · 1.12 KB
/
complex.cpp
File metadata and controls
52 lines (47 loc) · 1.12 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
#include <iostream>
using namespace std;
class complex {
float real;
float imag;
public:
complex() : real(0), imag(0) {}
complex operator+(complex);
complex operator*(complex);
friend istream &operator >> (istream &input, complex &t) {
cout<<"\nEnter the real part: ";
input>>t.real;
cout<<"Enter the imaginary part: ";
input>>t.imag;
return input;
}
friend ostream &operator << (ostream &output, complex &t) {
output<<t.real<<" + "<<t.imag<<"i"<<endl;
return output;
}
};
complex complex::operator+(complex c) {
complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
complex complex::operator*(complex c) {
complex temp;
temp.real = (real * c.real) - (imag * c.imag);
temp.imag = (imag * c.real) + (real * c.imag);
return temp;
}
int main() {
complex c1, c2, c3, c4;
cout<<"Default value in constructor: ";
cout<<c1;
cout<<"Enter the value of first complex number";
cin>>c1;
cout<<"Enter the value of second complex number";
cin>>c2;
c3 = c1 + c2;
c4 = c1 * c3;
cout<<"Addition of the complexes is : "<<c3;
cout<<"Multiplication of the complexes is : "<<c4;
return 0;
}