1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
//
// main.cpp
// Exercise 9.4 Enhancing time class
//
// Created by James Farrow on 25/08/2016.
// Copyright © 2016 James Farrow. All rights reserved.
//
/* localtime example */
#include <cstdio>
#include <ctime>
#include<iostream>
#include "Time.h" // include definition of class Time from Time.h
using namespace std;
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
int hours = timeinfo->tm_hour;
int minutes = timeinfo->tm_min;
int seconds = timeinfo->tm_sec;
cout << "Hours: " << hours << " Mins: " << minutes << " seconds: " << seconds << endl;
Time timeObject ( hours, minutes, seconds );
timeObject.printStandard();
std::cout << std::endl;
timeObject.printUniversal();
std::cout << std::endl;
return 0;
}
//
// time.cpp
// Fig 9.8,9.9,9.10 time class
//
// Created by James Farrow on 23/08/2016.
// Copyright © 2016 James Farrow. All rights reserved.
//
// Fig. 9.9: Time.cpp
// Member-function definitions for class Time.
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include "Time.h" // include definition of class Time from Time.h
using namespace std;
// Time constructor initializes each data member to zero
Time::Time( int hour, int minute, int second )
{
setTime( hour, minute, second ); // validate and set time
} // end Time constructor
// set new Time value using universal time
void Time::setTime( int h, int m, int s )
{
setHour( h ); // set private field hour
setMinute( m ); // set private field minute
setSecond( s ); // set private field second
} // end function setTime
// set hour value
void Time::setHour( int h )
{
if ( h >= 0 && h < 24 )
{
hour = h;
}
else
throw invalid_argument("hour must be 0-23" );
} // end function setHour
// set minute value
void Time::setMinute( int m )
{
if ( m >= 0 && m < 60 )
{
minute = m;
}
else
throw invalid_argument("minute must be 0-59" );
} // end function setMinute
// set second value
void Time::setSecond( int s )
{
if ( s >= 0 && s < 60 )
{
second = s;
}
else
throw invalid_argument("second must be 0-59" ); } // end function setSecond
// return hour value
int Time::getHour()
{
return hour;
} // end function getHour
// return minute value
int Time::getMinute()
{
return minute;
} // end function getMinute
// return second value
int Time::getSecond()
{
return second;
} // end function getSecond
// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal()
{
cout << setfill( '0' ) << setw( 2 ) << getHour() << ":"
<< setw( 2 ) << getMinute() << ":" << setw( 2 ) << getSecond();
} // end function printUniversal
// print Time in standard-time format (HH:MM:SS AM or PM)
void Time::printStandard()
{
cout << ( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 ) << ":" << setfill( '0' ) << setw( 2 ) << getMinute()
<< ":" << setw( 2 ) << getSecond() << ( hour < 12 ? " AM" : " PM" );
} // end function printStandard
// Fig. 9.8: Time.h
// Time class containing a constructor with default arguments.
// Member functions defined in Time.cpp.
// prevent multiple inclusions of header
#ifndef TIME_H
#define TIME_H
// Time abstract data type definition
class Time
{
public:
Time( int = 0, int = 0, int = 0 ); // default constructor
// set functions
void setTime( int, int, int ); // set hour, minute, second
void setHour( int ); // set hour (after validation)
void setMinute( int ); // set minute (after validation)
void setSecond( int ); // set second (after validation)
// get functions
int getHour(); // return hour
int getMinute(); // return minute
int getSecond(); // return second
void printUniversal(); // output time in universal-time format
void printStandard(); // output time in standard-time format
private:
int hour; // 0 - 23 (24-hour clock format)
int minute; // 0 - 59
int second; // 0 - 59
};// end class Time
#endif /* Header_h */
|