-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_overloading.cpp
83 lines (75 loc) · 1.91 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
#include <iostream>
using namespace std;
class Complex
{
int real, imag;
public:
Complex() {}
Complex(int r, int i) : real(r), imag(i) {}
void set(int i){
cout<<"Enter the real part of "<<i<<" complex number : ";
cin>>real;
cout<<"Enter the imaginary part of"<<i<<" complex number : ";
cin>>imag;
}
void show() { cout << real << " + " << imag << "i"
<< "\n"; }
Complex operator+(const Complex &op2) const { return {real + op2.real, imag + op2.imag}; }
Complex operator-(const Complex &op2) const { return {real - op2.real, imag - op2.imag}; }
Complex &operator++()
{
++real;
++imag;
return *this;
}
Complex operator++(int)
{
Complex temp = *this;
++real;
++imag;
return temp;
}
Complex &operator--()
{
--real;
--imag;
return *this;
}
Complex operator--(int)
{
Complex temp = *this;
--real;
--imag;
return temp;
}
bool operator==(const Complex &op2) const { return (op2.real == real) && (op2.imag == imag); }
};
int main()
{
Complex comp1, comp2, comp3;
comp1.set(1);
comp2.set(2);
cout << "Complex Number 1: ";
comp1.show();
cout << "Complex Number 2: ";
comp2.show();
cout << "Addition of complex numbers: ";
(comp1 + comp2).show();
cout << "Subtraction of complex numbers: ";
(comp1 - comp2).show();
cout << "Checking if the 2 complex numbers are equal\n";
comp1.show();
comp2.show();
cout << (comp1 == comp2 ? "Numbers are same" : "Numbers are different") << endl;
cout << "Incrementing complex number: ";
comp1.show();
++comp1;
cout << "After increment: ";
comp1.show();
cout << "Decrementing complex number: ";
comp2.show();
--comp2;
cout << "After decrement: ";
comp2.show();
return 0;
}