---
title: "Complex numbers relational operators"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:complex-relational-overload"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/complex-relational-overload
site: BCT Engineering
---

# Complex numbers relational operators

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2069 Chaitra, 5 marks
- Video: [Assignment 1 : Overloading of Operators for Complex Numbers.](https://www.youtube.com/watch?v=8FVq5f1Csns) — Rachana Satao
- 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-relational-overload
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Write a program to overload relational operators (==, !=, >, <, >=, <=) to compare complex numbers.

## Short answer

Overload == and != to compare real and imaginary parts exactly; over load >, <, >=, <= to compare magnitudes sqrt(re²+im²). For a=3+4i (mag 5) and b=1+2i (mag ≈2.236), a > b is true.

## Answer

Complex numbers are not ordered like real numbers, so we state a rule:
- `==` and `!=` compare the real and imaginary parts.
- `>`, `<`, `>=`, `<=` compare the **magnitude** √(re² + im²).

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

class Complex {
    float re, im;
public:
    Complex(float r = 0, float i = 0) { re = r; im = i; }
    float mag() const { return sqrt(re * re + im * im); }
    void show() const { cout << re << (im < 0 ? " - " : " + ")
                             << fabs(im) << "i"; }

    bool operator==(const Complex &c) const { return re == c.re && im == c.im; }
    bool operator!=(const Complex &c) const { return !(*this == c); }
    bool operator> (const Complex &c) const { return mag() >  c.mag(); }
    bool operator< (const Complex &c) const { return mag() <  c.mag(); }
    bool operator>=(const Complex &c) const { return mag() >= c.mag(); }
    bool operator<=(const Complex &c) const { return mag() <= c.mag(); }
};

int main() {
    Complex a(3, 4), b(1, 2);
    cout << "a = "; a.show(); cout << ",  b = "; b.show(); cout << 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;
    return 0;
}
```

**Output:**

```text
a = 3 + 4i,  b = 1 + 2i
a == b : 0
a != b : 1
a >  b : 1
a <  b : 0
a >= b : 1
a <= b : 0
```

|a| = 5 and |b| = √5 ≈ 2.236, so a is greater.

## Check yourself

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

1. Why does the program define a rule for >, <, >=, <= on complex numbers?
   A. C++ forbids them
   B. Complex numbers have no natural ordering like real numbers
   C. They need a friend function
   D. Because imaginary parts are integers
   Answer: B. Complex numbers are not ordered, so we choose magnitude as the basis. (Key point: Why a rule)

2. What does == compare in this program?
   A. Magnitudes
   B. Only real parts
   C. Both real and imaginary parts
   D. Only imaginary parts
   Answer: C. Equality means both parts match; the ordering operators use magnitude. (Key point: == rule)

3. What is the magnitude of 3 + 4i?
   A. 7
   B. 5
   C. 25
   D. 1
   Answer: B. sqrt(9 + 16) = 5. (Key point: Magnitude)

4. How is != implemented?
   A. Compares magnitudes with <
   B. By subtracting the numbers
   C. As !(*this == c)
   D. By comparing only the real part
   Answer: C. != is simply the negation of ==. (Key point: Reuse)

## Related in Object Oriented Programming

- [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
- [Compare complex magnitude by overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acomplex-magnitude-compare) — 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

## Related in other subjects

- [Relational and logical operators](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Arelational-logical-operators) — Computer Programming, chapter 3
- [Relational and assignment operators](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Arelational-assignment-operators) — Computer Programming, chapter 3
- [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
