Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Time.cpp

By: darearkin on Mar 16th, 2013  |  syntax: None  |  size: 2.47 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Time.cpp
  2. // Member-function definitions for class Time.
  3. #include <iostream>
  4. using std::cout;
  5.  
  6. #include <iomanip>
  7. using std::setfill;
  8. using std::setw;
  9.  
  10. #include "Time.h" // include definition of class Time from Time.h
  11.  
  12. // Time constructor initializes each data member to zero;
  13. // ensures that Time objects start in a consistent state
  14. Time::Time( int hr, int min, int sec )                  
  15. {                                                        
  16.    setTime( hr, min, sec ); // validate and set time    
  17.    cout << "Time object constructor is called.";
  18.    printStandard();
  19.    printUniversal();
  20. } // end Time constructor                                
  21.  
  22. // set new Time value using universal time; ensure that
  23. // the data remains consistent by setting invalid values to zero
  24. void Time::setTime( int h, int m, int s )
  25. {
  26.    setHour( h ); // set private field hour
  27.    setMinute( m ); // set private field minute
  28.    setSecond( s ); // set private field second
  29. } // end function setTime
  30.  
  31. // set hour value
  32. void Time::setHour( int h )
  33. {
  34.    hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour
  35.  
  36. } // end function setHour
  37.  
  38. // set minute value
  39. void Time::setMinute( int m )
  40. {
  41.    minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute
  42.  
  43. } // end function setMinute
  44.  
  45. // set second value
  46. void Time::setSecond( int s )
  47. {
  48.    second = ( s >= 0 && s < 60 ) ? s : 0; // validate second
  49.  
  50. } // end function setSecond
  51.  
  52. // return hour value
  53. int Time::getHour() const
  54. {
  55.    return hour;
  56. } // end function getHour
  57.  
  58. // return minute value
  59. int Time::getMinute() const
  60. {
  61.    return minute;
  62. } // end function getMinute
  63.  
  64. // return second value
  65. int Time::getSecond() const
  66. {
  67.    return second;
  68. } // end function getSecond
  69.  
  70. // print Time in universal-time format (HH:MM:SS)
  71. void Time::printUniversal() const
  72. {
  73.    cout << setfill( '0' ) << setw( 2 ) << getHour() << ":"
  74.       << setw( 2 ) << getMinute() << ":" << setw( 2 ) << getSecond();
  75. } // end function printUniversal
  76.  
  77. // print Time in standard-time format (HH:MM:SS AM or PM)
  78. void Time::printStandard() const
  79. {
  80.    cout << ( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 )
  81.       << ":" << setfill( '0' ) << setw( 2 ) << getMinute()
  82.       << ":" << setw( 2 ) << getSecond() << ( hour < 12 ? " AM" : " PM" );
  83. } // end function printStandard
  84.  
  85. Time::~Time() //begin ~Time destructor
  86. {
  87.   cout << "The Time Object destructor is Called\n";
  88.   printStandard();
  89.   printUniversal();
  90. }//end ~Time destructor