-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoperator_overloading.cpp
677 lines (574 loc) · 21.6 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#include <iostream>
#include <cmath>
class ComplexNumber
{
private:
double real;
double imaginary;
public:
// Constructor - Destructor
ComplexNumber(double r = 0.0, double i = 0.0) : real{r}, imaginary{i} {}
ComplexNumber(const ComplexNumber &c) : real{c.real}, imaginary{c.imaginary} {}
ComplexNumber(const ComplexNumber *c) : real{c->real}, imaginary{c->imaginary} {}
~ComplexNumber() {}
// Unary Operators
ComplexNumber operator+() const; // Unary Plus
ComplexNumber operator-() const; // Unary Minus
ComplexNumber &operator++(); // Increment (prefix)
ComplexNumber operator++(int); // Increment (postfix)
ComplexNumber &operator--(); // Decrement (prefix)
ComplexNumber operator--(int); // Decrement (postfix)
// double operator*() const; // Dereference or Indirection
ComplexNumber *operator&(); // Address-of
// Binary Arithmetic Operators
ComplexNumber operator+(const ComplexNumber &other) const;
ComplexNumber operator-(const ComplexNumber &other) const;
ComplexNumber operator*(const ComplexNumber &other) const;
ComplexNumber operator/(const ComplexNumber &other) const;
ComplexNumber operator%(const ComplexNumber &other) const;
// Binary Comparison Operators
bool operator==(const ComplexNumber &other) const;
bool operator!=(const ComplexNumber &other) const;
bool operator<(const ComplexNumber &other) const;
bool operator>(const ComplexNumber &other) const;
bool operator<=(const ComplexNumber &other) const;
bool operator>=(const ComplexNumber &other) const;
// Assignment Operators
ComplexNumber &operator=(const ComplexNumber &other);
ComplexNumber &operator+=(const ComplexNumber &other);
ComplexNumber &operator-=(const ComplexNumber &other);
ComplexNumber &operator*=(const ComplexNumber &other);
ComplexNumber &operator/=(const ComplexNumber &other);
ComplexNumber &operator%=(const ComplexNumber &other);
// Bitwise Operators
ComplexNumber operator<<(int shift) const;
ComplexNumber operator>>(int shift) const;
ComplexNumber operator&(const ComplexNumber &other) const;
ComplexNumber operator|(const ComplexNumber &other) const;
ComplexNumber operator^(const ComplexNumber &other) const;
ComplexNumber &operator&=(const ComplexNumber &other);
ComplexNumber &operator|=(const ComplexNumber &other);
ComplexNumber &operator^=(const ComplexNumber &other);
// Logical Operators
bool operator!() const;
bool operator&&(const ComplexNumber &other) const;
bool operator||(const ComplexNumber &other) const;
// Subscript Operator
double &operator[](int index);
const double &operator[](int index) const;
// Function Call Operator
ComplexNumber operator()();
// Member Access Operators
// const ComplexNumber* operator->() const;
// const ComplexNumber* operator->*(int) const;
// Object Conversion Operators
operator double() const
// Other Operators
ComplexNumber operator,(const ComplexNumber &other) const;
static void *operator new(size_t size);
static void operator delete(void *ptr);
friend std::ostream &operator<<(std::ostream &os, const ComplexNumber &c);
friend std::istream &operator >> (std::istream &is, ComplexNumber &c);
};
// Overloading Unary Plus operator
ComplexNumber ComplexNumber::operator+() const
{
return *this; // Unary plus has no effect, return the same complex number
}
// Overloading Unary Minus operator
ComplexNumber ComplexNumber::operator-() const
{
return ComplexNumber(-real, -imaginary);
}
// Overloading Increment operator (pre-increment)
ComplexNumber &ComplexNumber::operator++()
{
++real;
++imaginary;
return *this;
}
// Overloading Increment operator (post-increment)
ComplexNumber ComplexNumber::operator++(int)
{
ComplexNumber temp(*this);
++real;
++imaginary;
return temp;
}
// Overloading Decrement operator (pre-decrement)
ComplexNumber &ComplexNumber::operator--()
{
--real;
--imaginary;
return *this;
}
// Overloading Decrement operator (post-decrement)
ComplexNumber ComplexNumber::operator--(int)
{
ComplexNumber temp(*this);
--real;
--imaginary;
return temp;
}
// // Overloading Dereference operator
// double ComplexNumber::operator*() const
// {
// // Returning the magnitude of the complex number for simplicity
// return std::sqrt(real * real + imaginary * imaginary);
// }
// Overloading Address-of operator
ComplexNumber *ComplexNumber::operator&()
{
return this; // Return the address of the current complex number
}
// Overloading Addition operator
ComplexNumber ComplexNumber::operator+(const ComplexNumber &other) const
{
return ComplexNumber(real + other.real, imaginary + other.imaginary);
}
// Overloading Subtraction operator
ComplexNumber ComplexNumber::operator-(const ComplexNumber &other) const
{
return ComplexNumber(real - other.real, imaginary - other.imaginary);
}
// Overloading Multiplication operator
ComplexNumber ComplexNumber::operator*(const ComplexNumber &other) const
{
return ComplexNumber(
real * other.real - imaginary * other.imaginary,
real * other.imaginary + imaginary * other.real);
}
// Overloading Division operator
ComplexNumber ComplexNumber::operator/(const ComplexNumber &other) const
{
double denominator = other.real * other.real + other.imaginary * other.imaginary;
return ComplexNumber(
(real * other.real + imaginary * other.imaginary) / denominator,
(imaginary * other.real - real * other.imaginary) / denominator);
}
// Overloading Modulus operator
ComplexNumber ComplexNumber::operator%(const ComplexNumber &other) const
{
return ComplexNumber(std::sqrt(real * real + imaginary * imaginary), 0);
}
// Overloading Equality operator
bool ComplexNumber::operator==(const ComplexNumber &other) const
{
return (real == other.real) && (imaginary == other.imaginary);
}
// Overloading Inequality operator
bool ComplexNumber::operator!=(const ComplexNumber &other) const
{
return !(*this == other);
}
// Overloading Less than operator
bool ComplexNumber::operator<(const ComplexNumber &other) const
{
// Compare based on magnitude for simplicity
return ((*this % *this).real < (other % other).real);
}
// Overloading Greater than operator
bool ComplexNumber::operator>(const ComplexNumber &other) const
{
return !(*this <= other);
}
// Overloading Less than or equal to operator
bool ComplexNumber::operator<=(const ComplexNumber &other) const
{
return (*this < other) || (*this == other);
}
// Overloading Greater than or equal to operator
bool ComplexNumber::operator>=(const ComplexNumber &other) const
{
return !(*this < other);
}
// Overloading Assignment operator
ComplexNumber &ComplexNumber::operator=(const ComplexNumber &other)
{
if (this != &other)
{ // Avoid self-assignment
real = other.real;
imaginary = other.imaginary;
}
return *this;
}
// Overloading Add and Assign operator
ComplexNumber &ComplexNumber::operator+=(const ComplexNumber &other)
{
real += other.real;
imaginary += other.imaginary;
return *this;
}
// Overloading Subtract and Assign operator
ComplexNumber &ComplexNumber::operator-=(const ComplexNumber &other)
{
real -= other.real;
imaginary -= other.imaginary;
return *this;
}
// Overloading Multiply and Assign operator
ComplexNumber &ComplexNumber::operator*=(const ComplexNumber &other)
{
double resultReal = (real * other.real) - (imaginary * other.imaginary);
double resultImaginary = (real * other.imaginary) + (imaginary * other.real);
real = resultReal;
imaginary = resultImaginary;
return *this;
}
// Overloading Divide and Assign operator
ComplexNumber &ComplexNumber::operator/=(const ComplexNumber &other)
{
double denominator = (other.real * other.real) + (other.imaginary * other.imaginary);
double resultReal = ((real * other.real) + (imaginary * other.imaginary)) / denominator;
double resultImaginary = ((imaginary * other.real) - (real * other.imaginary)) / denominator;
real = resultReal;
imaginary = resultImaginary;
return *this;
}
// Overloading Modulus and Assign operator
ComplexNumber &ComplexNumber::operator%=(const ComplexNumber &other)
{
// Modify based on your requirement for modulus assignment
double modulus = std::sqrt(real * real + imaginary * imaginary);
real = modulus;
imaginary = 0.0;
return *this;
}
// Overloading Left shift operator
ComplexNumber ComplexNumber::operator<<(int shift) const
{
return ComplexNumber((long long)real << shift, (long long)imaginary << shift);
}
// Overloading Right shift operator
ComplexNumber ComplexNumber::operator>>(int shift) const
{
return ComplexNumber((long long)real >> shift, (long long)imaginary >> shift);
}
// Overloading Bitwise AND operator
ComplexNumber ComplexNumber::operator&(const ComplexNumber &other) const
{
return ComplexNumber((long long)real & (long long)other.real, (long long)imaginary & (long long)other.imaginary);
}
// Overloading Bitwise OR operator
ComplexNumber ComplexNumber::operator|(const ComplexNumber &other) const
{
return ComplexNumber((long long)real | (long long)other.real, (long long)imaginary | (long long)other.imaginary);
}
// Overloading Bitwise XOR operator
ComplexNumber ComplexNumber::operator^(const ComplexNumber &other) const
{
return ComplexNumber((long long)real ^ (long long)other.real, (long long)imaginary ^ (long long)other.imaginary);
}
// Overloading Bitwise AND and Assign operator
ComplexNumber &ComplexNumber::operator&=(const ComplexNumber &other)
{
real = (long long)real & (long long)other.real; // real &= other.real;
imaginary = (long long)imaginary & (long long)other.imaginary; // imaginary &= other.imaginary;
return *this;
}
// Overloading Bitwise OR and Assign operator
ComplexNumber &ComplexNumber::operator|=(const ComplexNumber &other)
{
real = (long long)real | (long long)other.real; // real |= (long long)other.real;
imaginary = (long long)imaginary | (long long)other.imaginary; // imaginary |= (long long)other.imaginary;
return *this;
}
// Overloading Bitwise XOR and Assign operator
ComplexNumber &ComplexNumber::operator^=(const ComplexNumber &other)
{
real = (long long)real ^ (long long)other.real; // real ^= (long long)other.real;
imaginary = (long long)imaginary ^ (long long)other.imaginary; // imaginary ^= (long long)other.imaginary;
return *this;
}
// Overloading Logical NOT operator
bool ComplexNumber::operator!() const
{
// Considering the modulus as a condition
return std::abs(real) < 1e-6 && std::abs(imaginary) < 1e-6;
}
// Overloading Logical AND operator
bool ComplexNumber::operator&&(const ComplexNumber &other) const
{
// Considering the modulus as a condition
return !(*this) && !(other);
}
// Overloading Logical OR operator
bool ComplexNumber::operator||(const ComplexNumber &other) const
{
// Considering the modulus as a condition
return !(*this) || !(other);
}
// Overloading Subscript operator
double &ComplexNumber::operator[](int index)
{
if (index == 0)
{
return real;
}
else if (index == 1)
{
return imaginary;
}
else
{
// Handle out-of-range condition or throw an exception
throw std::out_of_range("Invalid index for ComplexNumber");
}
}
// Overloading Subscript operator for const objects
const double &ComplexNumber::operator[](int index) const
{
if (index == 0)
{
return real;
}
else if (index == 1)
{
return imaginary;
}
else
{
// Handle out-of-range condition or throw an exception
throw std::out_of_range("Invalid index for ComplexNumber");
}
}
// Overloading Double object Conversion Operators
operator ComplexNumber::double() const {
return std::sqrt(real * real + imaginary * imaginary);
}
// Overloading Function Call Operator
ComplexNumber ComplexNumber::operator()()
{
// Example of a function call operation (returning a copy of the complex number)
return *this;
}
// Overloading Comma Operator
ComplexNumber ComplexNumber::operator,(const ComplexNumber &other) const
{
// Example of comma operator (returning the second complex number)
return other;
}
// Overloading Dynamic Memory Allocation (new)
void *ComplexNumber::operator new(size_t size)
{
// Example of dynamic memory allocation
return ::operator new(size);
}
// Overloading Dynamic Memory Deallocation (delete)
void ComplexNumber::operator delete(void *ptr)
{
// Example of dynamic memory deallocation
::operator delete(ptr);
}
// Overload the insertion operator to recognize an ostream object on the left
std::ostream &operator<<(std::ostream &os, const ComplexNumber &C)
{
os << C.real << "+";
if (C.imaginary < 0)
os << "(" << C.imaginary << ")i";
else
os << C.imaginary << "i";
return os;
}
// Overload the input operator
friend std::istream &operator >> (std::istream &is, ComplexNumber &C)
{
is >> c.real;
std::cout << "Enter imaginary part: ";
is >> c.imaginary;
return is;
}
int main()
{
{
ComplexNumber a(2.0, 3.0);
std::cout << "a: " << a << std::endl;
// Unary Plus
ComplexNumber b = +a;
std::cout << "Unary Plus: a: " << a << ", b: " << b << std::endl;
// Unary Minus
ComplexNumber c = -a;
std::cout << "Unary Minus: a: " << a << ", c: " << c << std::endl;
// Pre-increment
ComplexNumber d = ++a;
std::cout << "Pre-increment: a: " << a << ", d: " << d << std::endl;
// Post-increment
ComplexNumber e = a++;
std::cout << "Post-increment: a: " << a << ", e: " << e << std::endl;
// Pre-decrement
ComplexNumber f = --a;
std::cout << "Pre-decrement: a: " << a << ", f: " << f << std::endl;
// Post-decrement
ComplexNumber g = a--;
std::cout << "Post-decrement: a: " << a << ", g: " << g << std::endl;
// // Dereference
// double magnitude = *a;
// std::cout << "Dereference (Magnitude): " << magnitude << std::endl;
// Address-of
ComplexNumber *ptr = &a;
std::cout << "Address-of: " << ptr << std::endl;
}
std::cout << "------------------------------\n";
{
ComplexNumber a(2.0, 3.0);
ComplexNumber b(1.0, 2.0);
// Addition
ComplexNumber c = a + b;
std::cout << "Addition: a: " << a << ", b: " << b << ", c: " << c << std::endl;
// Subtraction
ComplexNumber d = a - b;
std::cout << "Subtraction: a: " << a << ", b: " << b << ", d: " << d << std::endl;
// Multiplication
ComplexNumber e = a * b;
std::cout << "Multiplication: a: " << a << ", b: " << b << ", e: " << e << std::endl;
// Division
ComplexNumber f = a / b;
std::cout << "Division: a: " << a << ", b: " << b << ", f: " << f << std::endl;
// Modulus
ComplexNumber modulus = a % b;
std::cout << "Modulus: " << modulus << std::endl;
}
std::cout << "------------------------------\n";
{
ComplexNumber a(2.0, 3.0);
ComplexNumber b(1.0, 2.0);
ComplexNumber c(2, 3);
// Equality
bool isEqual = (a == b);
std::cout << "a: " << a << ", b: " << b << " equality: " << (isEqual ? "true" : "false") << std::endl;
std::cout << "a: " << a << ", c: " << c << " equality: " << ((a == c) ? "true" : "false") << std::endl;
// Inequality
bool isNotEqual = (a != b);
std::cout << "a: " << a << ", b: " << b << " inequality: " << (isNotEqual ? "true" : "false") << std::endl;
std::cout << "a: " << a << ", c: " << c << " inequality: " << ((a != c) ? "true" : "false") << std::endl;
// Less than
bool isLessThan = (a < b);
std::cout << "a: " << a << ", b: " << b << " less than: " << (isLessThan ? "true" : "false") << std::endl;
std::cout << "a: " << a << ", c: " << c << " less than: " << ((a < c) ? "true" : "false") << std::endl;
// Greater than
bool isGreaterThan = (a > b);
std::cout << "a: " << a << ", b: " << b << " greater than: " << (isGreaterThan ? "true" : "false") << std::endl;
std::cout << "a: " << a << ", c: " << c << " greater than: " << ((a > c) ? "true" : "false") << std::endl;
// Less than or equal to
bool isLessThanOrEqual = (a <= b);
std::cout << "a: " << a << ", b: " << b << " less than or equal to: " << (isLessThanOrEqual ? "true" : "false") << std::endl;
std::cout << "a: " << a << ", c: " << c << " less than or equal to: " << ((a <= c) ? "true" : "false") << std::endl;
// Greater than or equal to
bool isGreaterThanOrEqual = (a >= b);
std::cout << "a: " << a << ", b: " << b << " greater than or equal to: " << (isGreaterThanOrEqual ? "true" : "false") << std::endl;
std::cout << "a: " << a << ", c: " << c << " greater than or equal to: " << ((a >= c) ? "true" : "false") << std::endl;
}
std::cout << "------------------------------\n";
{
ComplexNumber a(2.0, 3.0);
// Assignment
ComplexNumber c;
c = a;
std::cout << "Assignment: a: " << a << ", c: " << c << std::endl;
// Add and Assign
ComplexNumber d(1.0, 1.0);
d += a;
std::cout << "Add and Assign: a: " << a << ", d: " << d << std::endl;
// Subtract and Assign
ComplexNumber e(1.0, 1.0);
e -= a;
std::cout << "Subtract and Assign: a: " << a << ", e: " << e << std::endl;
// Multiply and Assign
ComplexNumber f(2.0, 2.0);
f *= a;
std::cout << "Multiply and Assign: a: " << a << ", f: " << f << std::endl;
// Divide and Assign
ComplexNumber g(2.0, 2.0);
g /= a;
std::cout << "Divide and Assign: a: " << a << ", g: " << g << std::endl;
// Modulus and Assign
ComplexNumber h(3.0, 4.0);
h %= a;
std::cout << "Modulus and Assign: a: " << a << ", h: " << h << std::endl;
}
std::cout << "------------------------------\n";
{
ComplexNumber a(5, 3);
ComplexNumber b(3, 2);
// Left shift
ComplexNumber c = a << 1;
std::cout << "Left shift: a: " << a << ", b: " << b << ", c: " << c << std::endl;
// Right shift
ComplexNumber d = a >> 1;
std::cout << "Right shift: a: " << a << ", b: " << b << ", d: " << d << std::endl;
// Bitwise AND
ComplexNumber e = a & b;
std::cout << "Bitwise AND: a: " << a << ", b: " << b << ", e: " << e << std::endl;
// Bitwise OR
ComplexNumber f = a | b;
std::cout << "Bitwise OR: a: " << a << ", b: " << b << ", f: " << f << std::endl;
// Bitwise XOR
ComplexNumber g = a ^ b;
std::cout << "Bitwise XOR: a: " << a << ", b: " << b << ", g: " << g << std::endl;
// Bitwise AND and Assign
a &= b;
std::cout << "Bitwise AND and Assign: a: " << a << ", b: " << b << std::endl;
// Bitwise OR and Assign
a |= b;
std::cout << "Bitwise OR and Assign: a: " << a << ", b: " << b << std::endl;
// Bitwise XOR and Assign
a ^= b;
std::cout << "Bitwise XOR and Assign: a: " << a << ", b: " << b << std::endl;
}
std::cout << "------------------------------\n";
{
ComplexNumber a(0.0, 0.0);
ComplexNumber b(3.0, 4.0);
// Logical NOT
bool notA = !a;
std::cout << "a: " << a << ", b: " << b << " logical NOT for a: " << std::boolalpha << notA << std::endl;
// Logical NOT
bool notB = !b;
std::cout << "a: " << a << ", b: " << b << " logical NOT for b: " << std::boolalpha << notB << std::endl;
// Logical AND
bool andResult = a && b;
std::cout << "a: " << a << ", b: " << b << " logical AND: " << std::boolalpha << andResult << std::endl;
// Logical OR
bool orResult = a || b;
std::cout << "a: " << a << ", b: " << b << " logical OR: " << std::boolalpha << orResult << std::endl;
}
std::cout << "------------------------------\n";
{
ComplexNumber a(2.0, 3.0);
// Access real and imaginary parts using subscript operator
std::cout << "Real part: " << a[0] << "; Imaginary part: " << a[1] << std::endl;
// Modify real and imaginary parts using subscript operator
a[0] = 5.0;
a[1] = -2.0;
// Display the modified complex number
std::cout << "Modified Complex Number: a: " << a << std::endl;
}
std::cout << "------------------------------\n";
{
ComplexNumber a(2.0, 3.0);
// Function Call Operator
ComplexNumber b = a();
std::cout << "Function Call Operator Result: a: " << a << ", b: " << b << std::endl;
}
std::cout << "------------------------------\n";
{
ComplexNumber a(4.0, 3.0);
std::cout << (double) a << std::endl;
}
std::cout << "------------------------------\n";
{
ComplexNumber a(2.0, 3.0);
ComplexNumber b(1.0, 2.0);
// Comma Operator
ComplexNumber c = (a, b);
std::cout << "Comma Operator Result: a: " << a << ", b: " << b << ", c: " << c << std::endl;
// Dynamic Memory Allocation (new)
ComplexNumber *dynamicObject = new ComplexNumber(4.0, 5.0);
std::cout << "Dynamic Memory Allocation Result: " << *dynamicObject << std::endl;
delete dynamicObject; // Dynamic Memory Deallocation (delete)
}
std::cout << "------------------------------\n";
return 0;
}
// clang++ ./operator_overloading.cpp -o app && ./app
// g++ ./operator_overloading.cpp -o app && ./app