---
title: "Time class: +, <= and << overloading"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:time-operator-overload"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/time-operator-overload
site: BCT Engineering
---

# Time class: +, <= and << overloading

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2074 Chaitra, 7 marks
- Video: [Overloading Operators for the Time Class](https://www.youtube.com/watch?v=W2VMkVjh-BM) — Mark Maloof
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/time-operator-overload
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Write operator functions as member function of a class to overload arithmetic operator +, logical operator '<=' and stream operator '<<' to operate on the objects of user defined type time (hr, min, sec).

## Short answer

Overload + as member: add seconds, carry to minutes, carry to hours, return new Time. Overload <= as member: convert both to total seconds and compare. Overload << as friend: print formatted like "2h 45m 50s".

## Answer

Three operators are overloaded for class `Time`:
- `+` adds two times (arithmetic, member function).
- `<=` compares two times (logical, member function).
- `<<` prints a time. Its left operand is `cout`, an `ostream` object, so it cannot be a member of `Time` (it could only be a member of `ostream`, which we cannot modify). It is therefore written as a **friend** (non-member) function.

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

class Time {
    int hr, min, sec;
public:
    Time(int h = 0, int m = 0, int s = 0) { hr = h; min = m; sec = s; }

    // arithmetic + : adds two times
    Time operator+(Time t) {
        Time r;
        r.sec = sec + t.sec;
        r.min = min + t.min + r.sec / 60;
        r.sec = r.sec % 60;
        r.hr  = hr + t.hr + r.min / 60;
        r.min = r.min % 60;
        return r;
    }

    // logical <= : true if this time is less than or equal to t
    bool operator<=(Time t) {
        int a = hr * 3600 + min * 60 + sec;
        int b = t.hr * 3600 + t.min * 60 + t.sec;
        return a <= b;
    }

    // stream insertion operator
    friend ostream& operator<<(ostream &out, Time &t) {
        out << t.hr << "h " << t.min << "m " << t.sec << "s";
        return out;
    }
};

int main() {
    Time t1(2, 45, 50), t2(1, 20, 30), t3;
    t3 = t1 + t2;
    cout << "t1 = " << t1 << endl;
    cout << "t2 = " << t2 << endl;
    cout << "t1 + t2 = " << t3 << endl;
    if (t1 <= t2) cout << "t1 <= t2" << endl;
    else          cout << "t1 > t2" << endl;
    return 0;
}
```

**Output:**

```text
t1 = 2h 45m 50s
t2 = 1h 20m 30s
t1 + t2 = 4h 6m 20s
t1 > t2
```

Seconds 50 + 30 = 80 gives 1 min carry and 20 s; minutes 45 + 20 + 1 = 66 gives 1 hr carry and 6 min; hours 2 + 1 + 1 = 4.

## Check yourself

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

1. Why is << written as a friend and not a member of Time?
   A. Left operand is cout (ostream), not Time
   B. Because it is unary
   C. Because Time is private
   D. Because it is faster
   Answer: A. A member operator needs the class object as left operand, so << is a non-member. (Key point: Why << friend)

2. How is carry handled in operator+ ?
   A. Seconds / 60 to minutes, minutes / 60 to hours
   B. No carry
   C. Hours first
   D. Only sec
   Answer: A. Seconds carry to minutes at 60, minutes to hours at 60. (Key point: Adding times)

3. How does <= compare two times?
   A. Converts both to seconds and compares
   B. Compares only hours
   C. Compares strings
   D. Compares addresses
   Answer: A. hr*3600 + min*60 + sec for each. (Key point: Compare method)

4. 2h45m50s + 1h20m30s = ?
   A. 3h 66m 80s
   B. 4h 6m 20s
   C. 4h 5m 20s
   D. 3h 6m 20s
   Answer: B. 50+30=80 gives 1m carry 20s; 45+20+1=66 gives 1h carry 6m; 2+1+1=4h. (Key point: Sum result)

## Related in Object Oriented Programming

- [City class with operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acity-class-operator-overload) — 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
- [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
- [Overloading via member and non-member function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-vs-nonmember-overload) — 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
- [Member function as friend of another class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-function-friend-of-other-class) — Object Oriented Programming, chapter 4

## Related in other subjects

- [Significance of user defined functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-user-defined-functions) — Computer Programming, chapter 6
- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [Arithmetic versus logical IF](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-arithmetic-vs-logical-if) — Computer Programming, chapter 11, asked 3×
- [How a function is defined in C](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-in-c) — Computer Programming, chapter 6
