Time class definition - C++ Class

C++ examples for Class:Operator Overload

Description

Time class definition

Demo Code

                                                                                                                                      
#include <iomanip>
#include <iostream>
#include <stdexcept>  // for invalid_argument exception class
                                                                                                                                      
                                                                                                                                     
class Time {//www.ja va  2s .  c  om
 private:
    int hour;    // 0-23 (24-hour format)
    int minute;  // 0-59
    int second;  // 0-59
                                                                                                                                      
 public:
    Time();
                                                                                                                                      
    void setTime(int, int, int);  // set hour minute second
    void printUniversal();        // print time in universal time format
    void printStandard();         // print time in standard time format
};
                                                                                                                                      
Time::Time() { hour = minute = second = 0; }
void Time::setTime(int h, int m, int s) {
    if ((h >= 0 && h < 24) && (m >= 0 && m < 60) && (s >= 0 && s < 60)) {
        hour = h;
        minute = m;
        second = s;
    } else {
        throw std::invalid_argument(
            "hour, minute and/or seconds was out of range");
    }
}
void Time::printUniversal() {
    std::cout << std::setfill('0') << std::setw(2) << hour << ":"
              << std::setw(2) << minute << ":" << std::setw(2) << second;
}
void Time::printStandard() {
    std::cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":"
              << std::setfill('0') << std::setw(2) << minute << ":"
              << std::setw(2) << second << (hour < 12 ? " AM" : " PM");
}
                                                                                                                                      
int main(int argc, const char *argv[]) {
    Time t;
                                                                                                                                      
    std::cout << "The initial universal time is ";
    t.printUniversal();
                                                                                                                                      
    std::cout << "\nThe initial standard time is ";
    t.printStandard();
                                                                                                                                      
    t.setTime(13, 27, 6);
                                                                                                                                      
    std::cout << "\n\nUniversal time after setTime is: ";
    t.printUniversal();
    std::cout << "\nStandard time after setTime is: ";
    t.printStandard();
                                                                                                                                      
    try {
        t.setTime(99, 99, 99);
    } catch (std::invalid_argument e) {
        std::cout << "\n\nException: " << e.what() << std::endl << std::endl;
    }
                                                                                                                                      
    // output t's values after specifying invalid values
    std::cout << "After attempting invalid settings: " << "\nUniversal Time: ";
    t.printUniversal();
                                                                                                                                      
    std::cout << "\nStandard Time: ";
    t.printStandard();
                                                                                                                                      
    std::cout << std::endl;
    return 0;
}

Result


Related Tutorials