fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. class Time
  8. {
  9. private:
  10. int hours;
  11. int minutes;
  12. int seconds;
  13.  
  14. public:
  15. Time(string timeStr);
  16. int toSeconds();
  17. void displayTime();
  18. };
  19.  
  20. class Clock
  21. {
  22. private:
  23. string name;
  24. Time currentTime;
  25.  
  26. public:
  27. Clock(string clockName, string timeStr);
  28. void showElapsedTime();
  29. };
  30.  
  31. Time::Time(string timeStr)
  32. {
  33. char colon;
  34. stringstream ss(timeStr);
  35. ss >> hours >> colon >> minutes >> colon >> seconds;
  36. }
  37.  
  38. int Time::toSeconds()
  39. {
  40. return hours * 3600 + minutes * 60 + seconds;
  41. }
  42.  
  43. void Time::displayTime()
  44. {
  45. cout << (hours < 10 ? "0" : "") << hours << ":"
  46. << (minutes < 10 ? "0" : "") << minutes << ":"
  47. << (seconds < 10 ? "0" : "") << seconds;
  48. }
  49.  
  50. Clock::Clock(string clockName, string timeStr) : name(clockName), currentTime(timeStr) {}
  51.  
  52. void Clock::showElapsedTime(){
  53. cout << "Clock " << name << " is displaying time: ";
  54. currentTime.displayTime();
  55. cout << endl;
  56. cout << "Elapsed time since 00:00:00: " << currentTime.toSeconds() << " seconds." << endl;
  57. }
  58.  
  59. int main() {
  60. // Input clock details
  61. string clockName, timeStr;
  62.  
  63. cout << "Enter clock name: ";
  64. getline(cin, clockName);
  65. cout << "Enter current time (HH:MM:SS): ";
  66. cin >> timeStr;
  67.  
  68. // Create a Clock object
  69. Clock myClock(clockName, timeStr);
  70.  
  71. // Display elapsed time
  72. myClock.showElapsedTime();
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter clock name: Enter current time (HH:MM:SS): Clock  is displaying time: 0-1449249208:32765:0-1281761755
Elapsed time since 00:00:00: -191680015 seconds.