---
title: "Relational operators comparing lengths"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:overload-relational-length"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/overload-relational-length
site: BCT Engineering
---

# Relational operators comparing lengths

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2074 Asoj, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/overload-relational-length
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Write a program to overload the relational operators to compare the length (in meter and centimeter) of two objects.

## Short answer

Overload relational operators by converting each length to centimeters (meter*100 + cm) and comparing the totals; operators return bool. For inputs 2 m 50 cm (250 cm) and 1 m 120 cm (220 cm), first length is greater.

## Answer

Class `Length` stores meter and centimetre. To compare two lengths, each is converted to centimetres (`meter * 100 + cm`), and the relational operators `<`, `>`, `==`, `!=`, `<=` and `>=` are overloaded as member functions returning `bool`.

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

class Length {
    int meter;
    float cm;
    float total() { return meter * 100 + cm; }   // length in cm
public:
    void read() {
        cout << "Enter meter and cm: ";
        cin >> meter >> cm;
    }
    void show() { cout << meter << " m " << cm << " cm"; }

    bool operator<(Length l)  { return total() <  l.total(); }
    bool operator>(Length l)  { return total() >  l.total(); }
    bool operator==(Length l) { return total() == l.total(); }
    bool operator!=(Length l) { return total() != l.total(); }
    bool operator<=(Length l) { return total() <= l.total(); }
    bool operator>=(Length l) { return total() >= l.total(); }
};

int main() {
    Length a, b;
    a.read();
    b.read();

    a.show(); cout << " vs "; b.show(); cout << endl;
    if (a == b)      cout << "Both lengths are equal" << endl;
    else if (a > b)  cout << "First length is greater" << endl;
    else             cout << "Second length is greater" << endl;

    cout << "a != b : " << (a != b) << endl;
    cout << "a <= b : " << (a <= b) << endl;
    cout << "a >= b : " << (a >= b) << endl;
    return 0;
}
```

**Sample run:**

```text
Enter meter and cm: 2 50
Enter meter and cm: 1 120
2 m 50 cm vs 1 m 120 cm
First length is greater
a != b : 1
a <= b : 0
a >= b : 1
```

Here 2 m 50 cm = 250 cm and 1 m 120 cm = 220 cm, so the first length is greater.

## Check yourself

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

1. How does the program compare two Length objects?
   A. Compares only the meter parts
   B. Converts both to centimetres with meter*100 + cm and compares
   C. Compares only the cm parts
   D. Subtracts them and checks the sign of meter
   Answer: B. Converting both to one unit (cm) makes comparison correct. (Key point: Common unit)

2. What do the overloaded relational operators return?
   A. int
   B. Length
   C. void
   D. bool
   Answer: D. Relational operators give true/false, so they return bool. (Key point: Return type)

3. Which is greater: 2 m 50 cm or 1 m 120 cm?
   A. 1 m 120 cm because 120 > 50
   B. They are equal
   C. 2 m 50 cm (250 cm vs 220 cm)
   D. Cannot be compared
   Answer: C. 250 cm > 220 cm, so the first is greater, even though 120 > 50. (Key point: Sample comparison)

## Related in Object Oriented Programming

- [Distance class relational operators](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adistance-relational-overload) — Object Oriented Programming, chapter 5
- [Complex numbers relational operators](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acomplex-relational-overload) — Object Oriented Programming, chapter 5
- [Add objects of two different classes](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-add-two-class-objects) — Object Oriented Programming, chapter 4, asked 2×
- [Time class: add two objects](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-class-add-objects) — Object Oriented Programming, chapter 1
- [Namespace with pass by reference program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Anamespace-pass-by-ref-program) — Object Oriented Programming, chapter 2

## 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
- [Compare two strings with function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-compare-function) — Computer Programming, chapter 7
- [River discharge from two-point current meter data](https://www.bctengineer.com/question.md?key=semester_5%2Fengineering_hydrology%3Acurrent-meter-0-02ns) — Engineering Hydrology, chapter 4
