The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Time
By Guest on 11th October 2023 04:08:30 AM | Syntax: TEXT | Views: 105



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. #include <iostream>
  2.  
  3. class Time {
  4. private:
  5.     int hour, minute, second;
  6.  
  7. public:
  8.     // Member function to read time
  9.     void readTime() {
  10.         std::cout << "Enter time (hh mm ss): ";
  11.         std::cin >> hour >> minute >> second;
  12.     }
  13.  
  14.     // Member function to display time
  15.     void displayTime() {
  16.         std::cout << "Time: " << hour << ":" << minute << ":" << second << std::endl;
  17.     }
  18.  
  19.     // Overloading '+' operator to find the sum of two time objects
  20.     Time operator+(const Time& t) const {
  21.         Time sum;
  22.         sum.second = (second + t.second) % 60;
  23.         sum.minute = (minute + t.minute + (second + t.second) / 60) % 60;
  24.         sum.hour = (hour + t.hour + (minute + t.minute + (second + t.second) / 60) / 60) % 24;
  25.         return sum;
  26.     }
  27. };
  28.  
  29. int main() {
  30.     Time time1, time2, sum;
  31.  
  32.     // Reading two times
  33.     time1.readTime();
  34.     time2.readTime();
  35.  
  36.     // Displaying the entered times
  37.     std::cout << "\nEntered Times:\n";
  38.     time1.displayTime();
  39.     time2.displayTime();
  40.  
  41.     // Finding the sum of two times using overloaded '+' operator
  42.     sum = time1 + time2;
  43.  
  44.     // Displaying the sum of times
  45.     std::cout << "\nSum of Times:\n";
  46.     sum.displayTime();
  47.  
  48.     return 0;
  49. }