Develop a C++ program that uses a class called time A time is represented in hours, minutes and seconds. For example (3:35:50). Define three objects for time class and get the user input values for first object. Initialize the second object using constructors. Set the third object equal to the sum of other two and display the value of the new timings using operator overloading, destructors and friend functions. Sample Input 3:35:504:30:20Sample Output: 8:06:10.
Question
Develop a C++ program that uses a class called time A time is represented in hours, minutes and seconds. For example (3:35:50). Define three objects for time class and get the user input values for first object. Initialize the second object using constructors. Set the third object equal to the sum of other two and display the value of the new timings using operator overloading, destructors and friend functions. Sample Input 3:35:504:30:20Sample Output: 8:06:10.
Solution
Here is a simple C++ program that fulfills your requirements:
#include<iostream>
using namespace std;
class Time {
int hours;
int minutes;
int seconds;
public:
Time() : hours(0), minutes(0), seconds(0) {}
Time(int h, int m, int s) : hours(h), minutes(m), seconds(s) {}
void input() {
cout << "Enter time:\n";
cout << "Hours? "; cin >> hours;
cout << "Minutes? "; cin >> minutes;
cout << "Seconds? "; cin >> seconds;
}
void display() {
cout << hours << ":" << minutes << ":" << seconds << endl;
}
Time operator + (Time t2) {
Time t3;
t3.seconds = this->seconds + t2.seconds;
t3.minutes = this->minutes + t2.minutes + (t3.seconds/60);
t3.hours = this->hours + t2.hours + (t3.minutes/60);
t3.minutes %= 60;
t3.seconds %= 60;
return t3;
}
~Time() {}
};
int main() {
Time t1, t2(4, 30, 20), t3;
t1.input();
t3 = t1 + t2;
cout << "The sum of the two times is: ";
t3.display();
return 0;
}
This program defines a class Time with three private data members: hours, minutes, and seconds. It has two constructors: a default constructor that initializes the time to 0:0:0, and a parameterized constructor that allows you to set the time.
The input member function prompts the user to enter the time, and the display member function prints the time.
The operator+ function is overloaded to add two Time objects. It adds the seconds, minutes, and hours separately, then adjusts for overflow (more than 60 seconds or minutes).
The main function creates three Time objects. It gets user input for t1, initializes t2 with the constructor, and sets t3 to the sum of t1 and t2. It then displays t3.
The destructor ~Time() is also defined, but it doesn't need to do anything in this case because the class doesn't allocate any dynamic memory.
Similar Questions
Create a Time class and initialize it with hours and minutes.1. Make a method addTime which should take two time object and add them. E.g.- (2 hour and 50 min)+(1 hr and 20 min) is (4 hr and 10 min)2. Make a method displayTime which should print the time.3. Make a method DisplayMinute which should display the total minutes in the Time. E.g.- (1 hr 2 min) should display 62 minute.
What is the output of below C++ program?#include <iostream>using namespace std;struct Time{int hours;int minutes;int seconds;};int toSeconds(Time now);int main(){Time t;t.hours = 5;t.minutes = 30;t.seconds=45;cout << toSeconds(t) << endl;return 0;}int toSeconds(Time now){return 3600 * now.hours + 60 * now.minutes + now.seconds;} 15000198452000019844
Janu is developing a time conversion program that consists of a base class called Time and two derived classes named TimeToSeconds and TimeToMinutes.Time class: - Stores hours and minutes and has a virtual function display that prints the input time.TimeToSeconds class: Overrides the display method to convert the time to seconds and displays it.TimeToMinutes class: Overrides the display method to convert the time to minutes and displays it.To assist Janu, we need to create a program that accepts input for hours and minutes, creates instances of the derived classes, and displays the original time, the time in seconds, and the time in minutes.
Problem StatementDuring sports events, especially in races or competitions, the time taken by a participant is recorded in seconds. Convert the time into HH:MM:SS format allows event organizers, participants, and spectators to understand and compare the timings easily.Write a program that displays the final time in the desired format.Input format :The input consists of an integer n, representing the time taken in seconds.Output format :The output prints the equivalent time in the format HH:MM:SS.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the given test cases fall under the following constraints:60 ≤ n ≤ 7200Sample test cases :Input 1 :60Output 1 :00:01:00Input 2 :7200Output 2 :02:00:00Input 3 :3760Output 3 :01:02:40
if(t2.seconds > t1.seconds) { --t1.minutes; t1.seconds += 60; } difference->seconds = t1.seconds - t2.seconds; if(t2.minutes > t1.minutes) { --t1.hours; t1.minutes += 60; } difference->minutes = t1.minutes-t2.minutes; difference->hours = t1.hours-t2.hours;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.