Public feed
#723807
// Fig. 21.19: Time.cpp // Member-function definitions for Time class./* Josh Garst10-02-07Purpose: To store only the second variable in the private sector of the class.Inputs: variables that represent time are passed in from the testmain.cppOutputs: The time for the examples that are given*/#include <iostream>using std::cout;#include <iomanip>using std::setfill;using std::setw;#include "Time.h" // Time class definition// 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 ) { second = 0; setTime( hr, min, sec ); } // end Time constructor// set values of hour, minute, and secondTime &Time::setTime( int h, int m, int s ) // note Time & return{ setHour( h ); setMinute( m ); setSecond( s ); return *this; // enables cascading}// set hour valueTime &Time::setHour( int h ) // note Time & return{ int hour; int minute; int sec; // Breaks down the different variables into a total hour = second / 3600; second %= 3600; minute = second / 60; second %= 60; sec = second; hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour second = hour* 3600; second += minute * 60; second += sec; return *this; // enables cascading} // set minute valueTime &Time::setMinute( int m ) // note Time & return{ int hour; int minute; int sec; hour = second / 3600; second %= 3600; minute = second / 60; second %= 60; sec = second; minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute second = hour * 3600; second += minute * 60; second += sec; return *this; // enables cascading} // set second valueTime &Time::setSecond( int s ) // note Time & return{ int hour; int minute; int sec; hour = second / 3600; second %= 3600; minute = second / 60; second %= 60; sec = second; sec = ( s >= 0 && s < 60 ) ? s : 0; // validate second second = hour* 3600; second += minute * 60; second += sec; return *this; // enables cascading} // get hour valueint Time::getHour() const { //Returns the amount of hours return second / 3600; } // get minute valueint Time::getMinute() const { int temp = second; temp %= 3600; temp /= 60; //Returns how many minutes return temp; } // get second valueint Time::getSecond() const { int temp = second; temp %= 3600; temp %= 60; //Returns the remaining seconds return temp; } // print Time in universal-time format (HH:MM:SS) void Time::printUniversal() const{ cout << setfill( '0' ) << setw( 2 ) << getHour() << ":" << setw( 2 ) << getMinute() << ":" << setw( 2 ) << getSecond();} // print Time in standard-time format (HH:MM:SS AM or PM)void Time::printStandard() const{ int hour = getHour(); int minute = getMinute(); int sec = getSecond(); cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":" << setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 ) << sec << ( hour < 12 ? " AM" : " PM" );}