-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01_overload_arithmetic.cpp
163 lines (130 loc) · 3.6 KB
/
01_overload_arithmetic.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Define a class Complex with appropriate instance variables and member functions.
Define following operators in the class :-
- a. +
- b. -
- c. *
- d. ==
*/
// // Header files
#include <iostream>
// // use namespace
using namespace std;
// // define class Complex
class Complex
{
private:
// // instance member variables
double real;
double imag;
public:
// // constructors
Complex()
{
real = imag = 0;
}
Complex(double r)
{
real = imag = r;
}
Complex(double r, double i)
{
real = r;
imag = i;
}
// // instance member function to set compelx number
void setData(double r, double i)
{
real = r;
imag = i;
}
// // instance member function to display compelx number
void showData()
{
cout << "\n"
<< real << " + " << imag << "i" << endl;
}
// // overload binary (addition +) operator
Complex operator+(Complex c)
{
c.real = real + c.real;
c.imag = imag + c.imag;
return c;
}
// // overload binary (subtraction -) operator
Complex operator-(Complex c)
{
c.real = real - c.real;
c.imag = imag - c.imag;
return c;
}
// // overload binary (multiplication *) operator
Complex operator*(Complex c)
{
c.real = real * c.real;
c.imag = imag * c.imag;
return c;
}
// // overload relational equality (==) operator
bool operator==(Complex c)
{
if (real == c.real && imag == c.imag)
return true;
return false;
}
};
// // Main Function Start
int main()
{
Complex c3; // create object of Complex
double real, imag;
// // Get complex numbers to add
cout << "\n>>>>>>>> Enter Two Complex Numbers to Perform Various Operations <<<<<<<<<\n";
// // Get first complex number
cout << "\n>>>>>>>> Enter First Complex Number <<<<<<<<<\n";
cout << "\nEnter Real Part => ";
cin >> real;
cout << "\nEnter Imaginary Part => ";
cin >> imag;
Complex c1(real, imag); // create object of Complex
// // Get second complex number
cout << "\n\n>>>>>>>> Enter Second Complex Number <<<<<<<<<\n";
cout << "\nEnter Real Part => ";
cin >> real;
cout << "\nEnter Imaginary Part => ";
cin >> imag;
Complex c2(real, imag); // create object of Complex
// // display first complex number
cout << "\n>>>>>>>> First Complex Number <<<<<<<<<\n";
c1.showData();
// // display second complex number
cout << "\n>>>>>>>> Second Complex Number <<<<<<<<<\n";
c2.showData();
// // add complex numbers
c3 = c1 + c2;
// // display addition of complex numbers
cout << "\n>>>>>>>> Addition of Entered Complex Numbers <<<<<<<<<\n";
c3.showData();
// // subtraction complex numbers
c3 = c1 - c2;
// // display subtraction of complex numbers
cout << "\n>>>>>>>> Subtraction of Entered Complex Numbers <<<<<<<<<\n";
c3.showData();
// // multiply complex numbers
c3 = c1 * c2;
// // display multiplication of complex numbers
cout << "\n>>>>>>>> Multiplication of Entered Complex Numbers <<<<<<<<<\n";
c3.showData();
// // check equality of complex numbers
bool isEqual = c1 == c2;
// // display equality of complex numbers
cout << "\n>>>>>>>> Entered Complex Numbers Are Equal or Not <<<<<<<<<\n";
if (isEqual)
cout << "\nEntered Complex Numbers Are Equal";
else
cout << "\nEntered Complex Numbers Are Not Equal";
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End