Create Time Class and use number of seconds to represent time - C++ Class

C++ examples for Class:Member Function

Description

Create Time Class and use number of seconds to represent time

Demo Code

                                                                                                                                                          
class Time {/*w  ww. ja v  a 2 s  . com*/
 public:
    explicit Time(int = 0, int = 0, int = 0);
                                                                                                                                                          
    // SETTERS
    Time& setTime(int, int, int);
    Time& setHour(int);
    Time& setMinute(int);
    Time& setSecond(int);
                                                                                                                                                          
    // GETTERS
    int getHour() const;
    int getMinute() const;
    int getSecond() const;
                                                                                                                                                          
    // IO OPERATIONS
    void printUniversal() const;
    void printStandard() const;
                                                                                                                                                          
 private:
    const int SECONDS_HOUR = 3600;
    const int SECONDS_MINUTE = 60;
                                                                                                                                                          
    int secondsSinceMidnight;
};
                                                                                                                                                          
#include <iomanip>
#include <iostream>
                                                                                                                                                          
Time::Time(int hr, int min, int sec) { setTime(hr, min, sec); }
// SETTERS
Time& Time::setTime(int h, int m, int s) {
    setHour(h);
    setMinute(m);
    setSecond(s);
                                                                                                                                                          
    return *this;  // enables cascading
}
Time& Time::setHour(int h) {
    if (h >= 0 && h < 24)
        secondsSinceMidnight = h * SECONDS_HOUR;
    else
        throw std::invalid_argument("hour must be 0-23");
                                                                                                                                                          
    return *this;  // enables cascading
}
Time& Time::setMinute(int m) {
    if (m >= 0 && m < 60)
        secondsSinceMidnight += m * SECONDS_MINUTE;
    else
        throw std::invalid_argument("minute must be 0-59");
                                                                                                                                                          
    return *this;  // enables cascading
}
Time& Time::setSecond(int s) {
    if (s >= 0 && s < 60)
        secondsSinceMidnight += s;
    else
        throw std::invalid_argument("second must be 0-59");
                                                                                                                                                          
    return *this;  // enables cascading
}
// GETTERS
int Time::getHour() const { return secondsSinceMidnight / SECONDS_HOUR; }
int Time::getMinute() const {
    return (secondsSinceMidnight - (getHour() * SECONDS_HOUR)) / SECONDS_MINUTE;
}
int Time::getSecond() const {
    return secondsSinceMidnight -
           ((getHour() * SECONDS_HOUR) + (getMinute() * SECONDS_MINUTE));
}
// IO OPERATIONS
// print time in universal-time format (HH:MM:SS)
void Time::printUniversal() const {
    std::cout << std::setfill('0') << std::setw(2) << getHour() << ":"
              << std::setw(2) << getMinute() << ":" << std::setw(2)
              << getSecond();
}
// print Time in standard-time format (HH:MM:SS AM/PM)
void Time::printStandard() const {
    std::cout << ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12)
              << ":" << std::setfill('0') << std::setw(2) << getMinute() << ":"
              << std::setw(2) << getSecond()
              << (getHour() < 12 ? " AM" : " PM");
}
#include <iostream>
                                                                                                                                                          
int main(int argc, const char *argv[]) {
    Time t1(13, 25, 59);
    Time t2(7, 51, 12);
                                                                                                                                                          
    t1.printUniversal();
    printf("\n");
    t2.printUniversal();
    printf("\n");
    t1.printStandard();
    printf("\n");
    t2.printStandard();
                                                                                                                                                          
    std::cout << std::endl;
    return 0;
}

Result


Related Tutorials