📄 time.cpp
字号:
// Tutorial 15: Time.cpp
// Member-function definitions for class Time.
#include <iostream> // required to perform C++ stream I/O
#include <iomanip> // required for parameterized stream manipulators
// include definition of class Time from Time.h
#include "Time.h"
using namespace std; // for accessing C++ Standard Library members
// Time constructor initializes each data member and ensures
// that all Time objects start in a consistent state
Time::Time()
{
// initialize each value
setHour( 12 );
setMinute( 0 );
setSecond( 0 );
} // end Time default constructor
// Time constructor initializes each data member to values
// specified in the parameter list
Time::Time( int hourValue, int minuteValue, int secondValue )
{
// initialize each value
setHour( hourValue );
setMinute( minuteValue );
setSecond( secondValue );
} // end Time overloaded constructor
// return hour value
int Time::getHour()
{
return hour;
} // end function getHour
// sets new hour value, performs validity checks on the data value
// and sets invalid values to 12.
void Time::setHour( int hourValue )
{
// update hour value
if ( hourValue > 0 && hourValue < 13 )
{
hour = hourValue; // use hourValue if it is in the valid range
} // end if
else // otherwise, set hour to 12
{
hour = 12;
} // end else
} // end function setHour
// return minute value
int Time::getMinute()
{
return minute;
} // end function getMinute
// sets new minute value, performs validity checks on the data value
// and sets invalid values to 0.
void Time::setMinute( int minuteValue )
{
// update minute value
if ( minuteValue >= 0 && minuteValue < 60 )
{
minute = minuteValue; // use minuteValue if within valid range
} // end if
else // otherwise, set minute to 0
{
minute = 0;
} // end else
} // end function setMinute
// return second value
int Time::getSecond()
{
return second;
} // end function getSecond
// sets new second value, performs validity checks on the data value
// and sets invalid values to 0.
void Time::setSecond( int secondValue )
{
// update second value
if ( secondValue >= 0 && secondValue < 60 )
{
second = secondValue; // use secondValue if within valid range
} // end if
else // otherwise, set second to 0
{
second = 0;
} // end else
} // end function setSecond
// display Time
void Time::displayTime()
{
cout << setfill( '0' ) << setw( 2 ) << getHour() << ":"
<< setw( 2 ) << getMinute() << ":"
<< setw( 2 ) << getSecond() << flush;
} // end function displayTime
// increments the current time by one second
void Time::tick()
{
// increment time by one second
setSecond( ( getSecond() + 1 ) % 60 );
// update minutes if seconds was previously 59
if ( getSecond() == 0 )
{
setMinute( ( getMinute() + 1 ) % 60 );
// update hours if minutes previously was 59
if ( getMinute() == 0 )
{
setHour( ( getHour() % 12 ) + 1 );
} // end if
} // end if
} // end function tick
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -