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

# Distance class relational operators

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2070 Chaitra, 8 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/distance-relational-overload
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Write a program to define a class Distance with necessary data members and functions. Then overload the relational operators to compare the two objects of Distance class.

## Short answer

Convert each Distance to total inches (feet*12+inches) and overload relational operators to compare these totals, returning bool. For 5'8" (68 in) and 6'2" (74 in), d1 < d2 is true.

## Answer

Class `Distance` stores a distance in feet and inches. For comparison, both objects are converted into inches (`feet * 12 + inches`). The six relational operators are overloaded as member functions.

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

class Distance {
    int feet;
    float inches;
public:
    Distance(int f = 0, float i = 0) { feet = f; inches = i; }

    void getdist() {
        cout << "Enter feet and inches: ";
        cin >> feet >> inches;
    }
    void showdist() { cout << feet << "'-" << inches << "\""; }

    float toInches() const { return feet * 12 + inches; }

    bool operator<(const Distance &d)  const { return toInches() <  d.toInches(); }
    bool operator>(const Distance &d)  const { return toInches() >  d.toInches(); }
    bool operator==(const Distance &d) const { return toInches() == d.toInches(); }
    bool operator!=(const Distance &d) const { return toInches() != d.toInches(); }
    bool operator<=(const Distance &d) const { return toInches() <= d.toInches(); }
    bool operator>=(const Distance &d) const { return toInches() >= d.toInches(); }
};

int main() {
    Distance d1, d2;
    d1.getdist();
    d2.getdist();

    cout << "d1 = "; d1.showdist();
    cout << ", d2 = "; d2.showdist(); cout << endl;

    cout << "d1 <  d2 : " << (d1 <  d2) << endl;
    cout << "d1 >  d2 : " << (d1 >  d2) << endl;
    cout << "d1 == d2 : " << (d1 == d2) << endl;
    cout << "d1 != d2 : " << (d1 != d2) << endl;
    cout << "d1 <= d2 : " << (d1 <= d2) << endl;
    cout << "d1 >= d2 : " << (d1 >= d2) << endl;
    return 0;
}
```

**Sample run:**

```text
Enter feet and inches: 5 8
Enter feet and inches: 6 2
d1 = 5'-8", d2 = 6'-2"
d1 <  d2 : 1
d1 >  d2 : 0
d1 == d2 : 0
d1 != d2 : 1
d1 <= d2 : 1
d1 >= d2 : 0
```

Here 5'8" = 68 in and 6'2" = 74 in, so d1 is smaller. Each operator returns a `bool` (1 for true, 0 for false), so the result can be used directly in `if` statements.

## Check yourself

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

1. How is a Distance converted for comparison?
   A. feet * 100 + inches
   B. feet + inches
   C. feet * 12 + inches
   D. feet / 12 + inches
   Answer: C. There are 12 inches in a foot, so total inches = feet*12 + inches. (Key point: Common unit)

2. Which operators are overloaded?
   A. Only < and >
   B. <, >, ==, !=, <=, >=
   C. +, -, *, /
   D. Only == and !=
   Answer: B. All six relational operators are overloaded as member functions. (Key point: Six operators)

3. For d1 = 5'8" and d2 = 6'2", what are d1 < d2 and d1 >= d2?
   A. 0 and 1
   B. 1 and 1
   C. 0 and 0
   D. 1 and 0
   Answer: D. 68 in < 74 in, so < is true (1) and >= is false (0). (Key point: Sample)

4. Why are the operators declared const and take const Distance &?
   A. They then run at compile time
   B. They promise not to modify either object and avoid copying
   C. They become friend functions
   D. They can change feet
   Answer: B. Comparisons should not modify objects; a const reference avoids copying and allows const objects. (Key point: const usage)

## 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
- [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
- [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×
- [Overload index operator [] for array class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aindex-operator-array-class) — Object Oriented Programming, chapter 5
- [Program using static members](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-program) — Object Oriented Programming, chapter 4

## 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
- [8085 ALP: compare two tables of 16-bit data](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-compare-tables-1ffffh) — Microprocessor, chapter 2
- [Data types and operators in C](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adata-types-operators-precedence) — Computer Programming, chapter 3
