In: Computer Science
####################################### Time.cpp ####################################### #include "Time.hpp" #include <sstream> #include <iomanip> using namespace std; Time::Time(int hour, int minute) { this->hour = hour; this->minute = minute; } Time::Time(const Time& time) { this->hour = time.hour; this->minute = time.minute; } void Time::SetTime(int hour, int minute) { if(hour < 0 || hour > 23) { return; } if(minute < 0 || minute > 59) { return; } this->hour = hour; this->minute = minute; } int Time::getHour() const { return hour; } int Time::getMinute() const { return minute; } void Time::ShowTime() const { cout << toString() << endl; } string Time::toString() const { stringstream ss; ss << setfill('0') << setw(2) << hour; ss << ":"; ss << setfill('0') << setw(2) << minute; return ss.str(); } bool Time::isValid() const { return (hour >= 0 && hour < 24 && minute >= 0 && minute < 60); } Time Time::Sum(Time &other) const { Time result; result.minute = minute + other.minute; result.hour = hour + other.hour; if(result.minute >= 60) { result.hour += 1; result.minute -= 60; } return result; } ####################################### Time.hpp ####################################### #ifndef TIME_HPP #define TIME_HPP #include <string> #include <iostream> using namespace std; class Time { private: int hour; int minute; public: Time(int hour = 0, int minute = 0); Time(const Time& time); void SetTime(int hour, int minute); void ShowTime() const; int getHour() const; int getMinute() const; string toString() const; bool isValid() const; Time Sum(Time &other) const; }; #endif ####################################### main.cpp ####################################### #include <iostream> #include "Time.hpp" using namespace std; int main() { Time time1 = Time(1, 25); Time time2 = Time(15, 59); time1.ShowTime(); time2.ShowTime(); Time time3 = time1.Sum(time2); time3.ShowTime(); }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.