---
title: "Compare complex magnitude by overloading"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:complex-magnitude-compare"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/complex-magnitude-compare
site: BCT Engineering
---

# Compare complex magnitude by overloading

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2068 Baisakh, 6 marks
- Video: [Adding Complex Number | Using Overloading + Operator | C++ In Hindi](https://www.youtube.com/watch?v=3sTQsSY0twE) — codeitup
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/complex-magnitude-compare
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Write a program to compare the magnitude of complex numbers by overloading <, > and == operators.

## Short answer

Overload <, >, and == to compare magnitudes sqrt(re²+im²); == uses a small tolerance. For c1=3+4i (mag 5) and c2=5+12i (mag 13), c2 has greater magnitude.

## Answer

The magnitude (modulus) of a complex number z = a + bi is |z| = √(a² + b²). The operators `<`, `>` and `==` are overloaded as member functions that compare these magnitudes and return `bool`. For `==`, floating-point values are compared with a small tolerance.

```cpp
#include <iostream>
#include <cmath>
using namespace std;

class Complex {
    float re, im;
public:
    void read() {
        cout << "Enter real and imaginary part: ";
        cin >> re >> im;
    }
    float magnitude() const { return sqrt(re * re + im * im); }

    bool operator<(const Complex &c) const {
        return magnitude() < c.magnitude();
    }
    bool operator>(const Complex &c) const {
        return magnitude() > c.magnitude();
    }
    bool operator==(const Complex &c) const {
        return fabs(magnitude() - c.magnitude()) < 1e-6;
    }
};

int main() {
    Complex c1, c2;
    c1.read();
    c2.read();
    cout << "|c1| = " << c1.magnitude()
         << ", |c2| = " << c2.magnitude() << endl;

    if (c1 > c2)       cout << "c1 has the greater magnitude" << endl;
    else if (c1 < c2)  cout << "c2 has the greater magnitude" << endl;
    else if (c1 == c2) cout << "Both have equal magnitude" << endl;
    return 0;
}
```

**Sample run:**

```text
Enter real and imaginary part: 3 4
Enter real and imaginary part: 5 12
|c1| = 5, |c2| = 13
c2 has the greater magnitude
```

For `3 4` and `4 3` the program prints "Both have equal magnitude", because both have |z| = 5, even though the numbers are different.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the magnitude of z = a + bi?
   A. sqrt(a^2 + b^2)
   B. a + b
   C. a^2 + b^2 without a root
   D. sqrt(a) + sqrt(b)
   Answer: A. The modulus is the square root of the sum of squares. (Key point: Magnitude formula)

2. Why does == compare with a small tolerance (fabs(...) < 1e-6)?
   A. To ignore the sign
   B. To compare integers
   C. Because == cannot be overloaded
   D. Floating-point values may not be exactly equal
   Answer: D. Floating-point rounding means exact equality is unreliable. (Key point: Tolerance)

3. For 3 + 4i and 4 + 3i, what does the program report?
   A. c1 has the greater magnitude
   B. c2 has the greater magnitude
   C. Both have equal magnitude
   D. Error
   Answer: C. Both have magnitude 5, even though the numbers differ. (Key point: Equal magnitudes)

## Related in Object Oriented Programming

- [Complex numbers relational operators](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acomplex-relational-overload) — Object Oriented Programming, chapter 5
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [Default argument and overloading program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-overloading-program) — Object Oriented Programming, chapter 3
- [Relational operators comparing lengths](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoverload-relational-length) — Object Oriented Programming, chapter 5
- [Distance class relational operators](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adistance-relational-overload) — Object Oriented Programming, chapter 5

## Related in other subjects

- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [Complex number multiplication using structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-complex-multiply) — Computer Programming, chapter 8
- [Complex number sum via functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-complex-sum-function) — Computer Programming, chapter 8
