// DateTime.cpp // Member function definitions for Date and Time class. #include using std::cout; #include "DateTime.h" // Constructor function to initialize private data. // Calls member function setTime to set variables. // Default values are 0 (see class definition). Time::Time( int hr, int min, int sec ) { setTime( hr, min, sec ); } // Set the values of hour, minute, and second. void Time::setTime( int h, int m, int s ) { setHour( h ); setMinute( m ); setSecond( s ); } // Set the hour value void Time::setHour( int h ) { hour = ( h >= 0 && h < 24 ) ? h : 0; } // Set the minute value void Time::setMinute( int m ) { minute = ( m >= 0 && m < 60 ) ? m : 0; } // Set the second value void Time::setSecond( int s ) { second = ( s >= 0 && s < 60 ) ? s : 0; } // Get the hour value int Time::getHour() { return hour; } // Get the minute value int Time::getMinute() { return minute; } // Get the second value int Time::getSecond() { return second; } // Print time is military format void Time::printMilitary() { cout << ( hour < 10 ? "0" : "" ) << hour << ":" << ( minute < 10 ? "0" : "" ) << minute << ":" << ( second < 10 ? "0" : "" ) << second; } // Print time in standard format void Time::printStandard() { cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":" << ( minute < 10 ? "0" : "" ) << minute << ":" << ( second < 10 ? "0" : "" ) << second << ( hour < 12 ? " AM" : " PM" ); } // Simple Date constructor with no range checking Date::Date( int m, int d, int y ) { month = m; day = d; year = y; } // Print the Date in the form mm-dd-yyyy void Date::print() { cout << "Date Is: "; cout << month << '-' << day << '-' << year; } void Date::nextDay() { const int Days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ; int daysinmonth = Days[month-1] ; if (isLeapYear() && (month == 2)) ++daysinmonth ; if (day < daysinmonth) day++ ; else {day = 1 ; if (month < 12) month ++ ; else {month = 1 ; year ++ ;} } } bool Date::isLeapYear() { if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) return true; else return false; // Not A Leap Year }