-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operator_overloading.cpp
111 lines (111 loc) · 3.7 KB
/
Operator_overloading.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
using namespace std;
class ComplexNumber{
private:
double re;
double im;
public:
//constructor
ComplexNumber(double re, double im){
this->re = re;
this->im = im;
}
friend ostream& operator<<(ostream& output, const ComplexNumber& cn){
output << cn.re << " +" << " ( " <<cn.im <<" i" << " )";
return output;
}
friend istream& operator>>(istream &input, ComplexNumber& cn){
input >> cn.re >> cn.im;
return input;
}
friend ComplexNumber operator + (const ComplexNumber& x, const ComplexNumber& y){
return(ComplexNumber(x.re+y.re, x.im + y.im));
}
friend ComplexNumber operator - (const ComplexNumber& x, const ComplexNumber& y){
return(ComplexNumber(x.re-y.re, x.im - y.im));
}
friend ComplexNumber operator * (const ComplexNumber& x, const ComplexNumber& y) {
return (ComplexNumber(x.re * y.re, x.im * y.im));
}
friend ComplexNumber operator / (const ComplexNumber& x, const ComplexNumber& y) {
return (ComplexNumber(((x.re*y.re)+(x.im*y.im))/((y.re*y.re)+(y.im*y.im)), ( (x.im *y.re - x.re*y.im) / ( y.re * y.re + y.im*y.im) )));
}
friend bool operator== (ComplexNumber& x, ComplexNumber& y){
return ((x.re == y.re) && (x.im == y.im));
}
friend bool operator!= (ComplexNumber& x, ComplexNumber& y){
return ((x.re != y.re) && (x.im != y.im));
}
ComplexNumber& operator =(const ComplexNumber& cn){
this->re = cn.re;
this->im = cn.im;
return* this;
}
ComplexNumber& operator +=(const ComplexNumber& cn){
this->re += cn.re;
this->im += cn.im;
return* this;
}
ComplexNumber& operator -=(const ComplexNumber& cn){
this->re -= cn.re;
this->im -= cn.im;
return* this;
}
ComplexNumber& operator *=(const ComplexNumber& cn){
this->re = (this->re * cn.re) - (this->im * cn.im);
this->im = (this->re * cn.im) + (this->im * cn.re);
return* this;
}
ComplexNumber& operator /=(const ComplexNumber& cn){
this->re = ((this->re * cn.re) + (this->im * cn.im))/(cn.re * cn.re + cn.im * cn.im);
this->im = (this->re * cn.im) + (this->im * cn.re)/(cn.re * cn.re + cn.im * cn.im);
return* this;
}
ComplexNumber& operator ~ (){
this->re = this->re * (-1);
this->im = this->im * (-1);
return *this;
}
ComplexNumber& operator ! (){
this->im = this->im * (-1);
return *this;
}
ComplexNumber& operator ++ (int){
this->im = this->im +1;
return *this;
}
ComplexNumber& operator -- (int){
this->im = this->im -1;
return *this;
}
ComplexNumber& operator -- (){
this->re = this->re -1;
return *this;
}
ComplexNumber& operator ++ (){
this->re = this->re +1;
return *this;
}
};
int main() {
ComplexNumber a = ComplexNumber(8,3);
ComplexNumber b = ComplexNumber(4,5);
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "a / b = " << a / b << endl;
cout << " a == b ? " << (a == b) << endl;
cout << " a != b ? " << (a != b) << endl;
// cout << " a = b " << (a = b) << endl;
// cout << " a += b " << (a+=b) << endl;
// cout << " a -= b " << (a -= b) << endl;
// cout << " a *= b " << (a *= b) << endl;
// cout << " a /= b " << (a /= b) << endl;
// cout << " ~a = " << ~a << endl;
// cout << " !a = " << !a << endl;
cout << " a ++ " << a++ << endl;
cout << " a -- " << a-- << endl;
cout << " ++ a " << ++a << endl;
cout << " -- a " << --a << endl;
return 0;
}