-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextraction_insertion.cpp
58 lines (50 loc) · 1.24 KB
/
extraction_insertion.cpp
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
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imaginary;
public:
Complex(double real = 0.0, double imaginary = 0.0) {
this->real = real;
this->imaginary = imaginary;
}
friend ostream& operator<<( ostream& os, const Complex& c) {
os << c.real << " + " << c.imaginary << "i";
return os;
}
friend istream& operator>>( istream& is, Complex& c) {
cout << "Enter real part: ";
cout << "Enter real part: ";
is >> c.real;
cout << "Enter imaginary part: ";
is >> c.imaginary;
return is;
}
};
int main() {
Complex c1;
cout << "Enter a complex number: ";
cin >> c1;
cout << "The complex number you entered is: " << c1 << endl;
return 0;
}
Complex operator>>(Complex& c, istream& is) {
cout << "Enter real part: ";
is >> c.real;
cout << "Enter imaginary part: ";
is >> c.imaginary;
return c;
}
void operator<<(const Complex& c, ostream& os) {
os << c.real << " + " << c.imaginary << "i";
}
int main() {
Complex c1;
cout << "Enter a complex number: ";
c1 >> cin;
cout << "The complex number you entered is: ";
c1 << cout;
cout << endl;
return 0;
}