---
title: "City class with operator overloading"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:city-class-operator-overload"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/city-class-operator-overload
site: BCT Engineering
---

# City class with operator overloading

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2073 Shrawan, 7 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/city-class-operator-overload
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Explain the syntax of operator overloading. Create a class named City that will have two member variables CityName (char[20]) and DistFromKtm (float). Add member functions to set and retrieve the CityName and DistFromKtm separately. Add operator overloading to find the distance between the cities (just find the difference of DistFromKtm) and sum of distance of those cities from Kathmandu. In the main function, initialise three city objects. Set the first and second city to be Pokhara and Dhangadi. Display the sum of DistFromKtm of Pokhara and Dhangadi and distance between Pokhara and Dhangadi.

## Short answer

Operator overloading syntax: return_type ClassName::operator op(args) { … } as member function. For City, overload + to return a City with summed DistFromKtm and – to return absolute difference. With Pokhara (200 km) and Dhangadi (680 km), sum = 880 km, distance = 480 km.

## Answer

#### Syntax of operator overloading

```cpp
return_type ClassName::operator op (argument_list) { body }
```

`operator` is a keyword and `op` is the operator symbol. As a member function, a binary operator takes one argument (the right operand); the left operand is the calling object. `c3 = c1 + c2;` is translated to `c3 = c1.operator+(c2);`.

#### Program

Assumptions: `operator+` returns a `City` whose `DistFromKtm` is the sum; `operator-` returns the distance between two cities (absolute difference) as a `float`. Approximate distances used: Pokhara 200 km, Dhangadi 680 km.

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

class City {
    char CityName[20];
    float DistFromKtm;
public:
    void setName(const char *n)  { strcpy(CityName, n); }
    void setDist(float d)        { DistFromKtm = d; }
    const char* getName()        { return CityName; }
    float getDist()              { return DistFromKtm; }

    // sum of distances from Kathmandu
    City operator+(City c) {
        City t;
        strcpy(t.CityName, "Sum");
        t.DistFromKtm = DistFromKtm + c.DistFromKtm;
        return t;
    }

    // distance between two cities (difference of DistFromKtm)
    float operator-(City c) {
        return fabs(DistFromKtm - c.DistFromKtm);
    }
};

int main() {
    City c1, c2, c3;
    c1.setName("Pokhara");  c1.setDist(200);
    c2.setName("Dhangadi"); c2.setDist(680);

    c3 = c1 + c2;
    cout << "Sum of DistFromKtm of " << c1.getName() << " and "
         << c2.getName() << " = " << c3.getDist() << " km" << endl;
    cout << "Distance between " << c1.getName() << " and "
         << c2.getName() << " = " << c2 - c1 << " km" << endl;
    return 0;
}
```

**Output:**

```text
Sum of DistFromKtm of Pokhara and Dhangadi = 880 km
Distance between Pokhara and Dhangadi = 480 km
```

## Check yourself

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

1. In c3 = c1 + c2 with a member operator+, how does the compiler translate the statement?
   A. c3.operator+(c1, c2)
   B. c1.operator+(c2)
   C. c2.operator+(c1)
   D. operator+(c3)
   Answer: B. The left operand is the calling object and the right is the argument: c1.operator+(c2). (Key point: Syntax)

2. Why does operator- return a float rather than a City?
   A. Because - cannot return objects
   B. Because it must be a friend
   C. Because it modifies DistFromKtm
   D. Because it gives a distance between cities, which is a number
   Answer: D. The distance between cities is a numeric difference of DistFromKtm, so float is natural. (Key point: Return type of difference)

3. Why is fabs used in operator-?
   A. To make the distance non-negative regardless of operand order
   B. To round the result
   C. To convert to km
   D. To avoid overflow
   Answer: A. A distance cannot be negative, so the absolute difference is returned. (Key point: Absolute difference)

4. With Pokhara 200 km and Dhangadi 680 km, what do the program print for sum and distance between?
   A. 880 and 480
   B. 480 and 880
   C. 880 and 880
   D. 200 and 680
   Answer: A. Sum = 200 + 680 = 880; distance = 680 - 200 = 480. (Key point: Output values)

## Related in Object Oriented Programming

- [Syntax of operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-syntax) — Object Oriented Programming, chapter 5, asked 4×
- [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
- [Binary and unary operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-unary-overloading) — Object Oriented Programming, chapter 5
- [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
- [Time class: +, <= and << overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-operator-overload) — Object Oriented Programming, chapter 5
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — Object Oriented Programming, chapter 5

## Related in other subjects

- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [FORTRAN constants, variables, library functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-constants-variables-library) — Computer Programming, chapter 11
- [Student records with two functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-student-read-display-functions) — Computer Programming, chapter 8
- [8085 ALP: add two tables, sum and carry](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-add-two-tables) — Microprocessor, chapter 2
