-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment1.cpp
76 lines (64 loc) · 1.64 KB
/
Assignment1.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Assignment 1
Name : Swaraj Sachin Gosavi
Roll no : 21327
Batch : F3 [SE 3]
Implement a class Complex which represents the Complex Number data type.
Implement the following
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overload operator+ to add two complex numbers.
3. Overload operator* to multiply two complex numbers.
4. Overload operators << and >> to print and read Complex Numbers.
*/
#include<iostream>
using namespace std;
class Complex{
private:
float x, y;
public:
Complex(){
x = 0;
y = 0;
}
Complex operator+(Complex);
Complex operator*(Complex);
friend ostream & operator<<(ostream &, Complex &);
friend istream & operator>>(istream &, Complex &);
};
Complex Complex :: operator+(Complex obj){
Complex temp;
temp.x = x + obj.x;
temp.y = y + obj.y;
return temp;
}
Complex Complex :: operator*(Complex obj){
Complex temp;
temp.x = (x * obj.x) - (y * obj.y);
temp.y = (x * obj.y) + (y * obj.x);
return temp;
}
istream & operator>>(istream &din, Complex &obj){
cout << "Enter the real part : ";
din >> obj.x;
cout << "Enter the imaginary part : ";
din >> obj.y;
return din;
}
ostream & operator<<(ostream &dout, Complex &obj){
dout << obj.x << " + " << obj.y << "i" << endl;
return dout;
}
int main(){
Complex a, b, c, d;
cin >> a;
cout << a;
cin >> b;
cout << b;
c = a + b;
cout << "Addition : ";
cout << c;
d = a * b;
cout << "Multiplication : ";
cout << d;
return 0;
}