---
title: "Time class: add two objects"
subject: "Object Oriented Programming"
chapter: 1
key: "semester_3/object_oriented_programming:time-class-add-objects"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/time-class-add-objects
site: BCT Engineering
---

# Time class: add two objects

Object Oriented Programming · Chapter 1: Introduction to Object Oriented Programming

- Asked in: 2073 Shrawan, 6 marks
- Video: [C++ program||Lecture-02 ||representing time as a class and adding time objects .](https://www.youtube.com/watch?v=AOIa_sklwR8) — Learn with Debjit
- 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-class-add-objects
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-1

## Question

Write a program to create class "time" with data members hours, minute and second. Then add two "time" objects by taking object as argument and also returning object as argument.

## Short answer

Define a Time class with hours, minutes, seconds. The add(Time) method sums two times, with carry of 60 seconds to minutes and 60 minutes to hours, returning a new Time object. Example: 2h 45m 50s + 1h 20m 30s = 4h 6m 20s.

## Answer

The class `Time` has data members hours, minutes and seconds. The function `add()` receives one `Time` object as an argument, is called by another object, and returns a new `Time` object as the result. Seconds and minutes above 59 are carried over.

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

class Time {
    int hours, minutes, seconds;
public:
    void getTime() {
        cout << "Enter hours, minutes, seconds: ";
        cin >> hours >> minutes >> seconds;
    }
    Time add(Time t2) {              // object as argument, object returned
        Time t3;
        t3.seconds = seconds + t2.seconds;
        t3.minutes = minutes + t2.minutes + t3.seconds / 60;
        t3.seconds = t3.seconds % 60;
        t3.hours   = hours + t2.hours + t3.minutes / 60;
        t3.minutes = t3.minutes % 60;
        return t3;
    }
    void display() {
        cout << hours << " hr " << minutes << " min "
             << seconds << " sec" << endl;
    }
};

int main() {
    Time t1, t2, t3;
    t1.getTime();
    t2.getTime();
    t3 = t1.add(t2);                 // t1 calls add, t2 is passed
    cout << "Sum = ";
    t3.display();
    return 0;
}
```

**Sample run**

```text
Enter hours, minutes, seconds: 2 45 50
Enter hours, minutes, seconds: 1 20 30
Sum = 4 hr 6 min 20 sec
```

Check: seconds 50 + 30 = 80, so 20 s with carry 1; minutes 45 + 20 + 1 = 66, so 6 min with carry 1; hours 2 + 1 + 1 = 4.

## Check yourself

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

1. If seconds sum to 80, what carries into minutes?
   A. Seconds 80, no carry
   B. Seconds 20 and carry 1 minute
   C. Seconds 0, carry 80
   D. Seconds 20, carry 20
   Answer: B. 80 / 60 = 1, 80 % 60 = 20. (Key point: Carry seconds)

2. How are objects used in t3 = t1.add(t2)?
   A. t2 calls add; t1 is the argument
   B. Both passed by pointer
   C. t1 calls add; t2 is the argument; a Time object is returned
   D. Nothing returned
   Answer: C. t1 is the calling object, t2 is passed, result stored in t3. (Key point: Object passing)

3. What is 2:45:50 + 1:20:30?
   A. 3 hr 65 min 80 sec
   B. 4 hr 5 min 20 sec
   C. 3 hr 6 min 20 sec
   D. 4 hr 6 min 20 sec
   Answer: D. 50+30=80 gives 20 s carry 1; 45+20+1=66 gives 6 min carry 1; 2+1+1=4. (Key point: Sample sum)

4. Why are seconds handled before minutes?
   A. Carry from seconds is needed to compute minutes, then minutes' carry for hours
   B. Alphabetical order
   C. Hours are always first
   D. Order does not matter
   Answer: A. Carries propagate upward. (Key point: Order of carry)

## Related in Object Oriented Programming

- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1
- [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×
- [Default argument and overloading program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-overloading-program) — Object Oriented Programming, chapter 3
- [Exception with argument and specification program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-program-arg-specification) — Object Oriented Programming, chapter 10
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [LandMeasure class and sum function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Alandmeasure-class-sum) — Object Oriented Programming, chapter 4

## 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×
- [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
- [Paired test: safety program worker hours](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Apaired-before-after-safety-program) — Probability and Statistics, chapter 6, asked 3×
- [Add two matrices](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Amatrix-add-two) — Computer Programming, chapter 7
