Given a complete clock class specification, I am supposed to write an implementation and also write a program to get the current time and display the time in military format or in standard format.
Here is the complete clock specification.
#include <string>
usingnamespace std;
class Clock
{
public:
/* Constructs a clock that can tell the local time. It also sets the
location to "Local"
@param use_military true if the clock uses military format */
Clock(bool use_military);
/* display this clock. It display the current time.
Display clock in military or standard format */
void displayClock() const;
private:
/* Gets the location of this clock.
@return the location */
string get_location() const;
/* Gets the hours of this clock.
Use the ctime struct tm
@return the hours, in military or standard format */
int get_hours() const;
/* Gets the minutes of this clock.
Use the ctime struct tm
@return the minutes */
int get_minutes() const;
/* Checks whether this clock uses military format.
@return true if military format */
bool is_military() const;
// declare data members
bool military;
}; //end of class Clock
This is my program to display the current time. I am supposed to use an input delay so the time can be tested at a later time.
#include <iostream>
#include "clock.h"
usingnamespace std;
int main()
{
char ch;
Clock clock1(true);
Clock clock2(false);
clock1.displayClock();
clock2.displayClock();
//delay my response for a while
cin >> ch; //enter data at a later time
clock1.displayClock();
clock2.displayClock();
return 0;
}
My problem is in the implementation. I am having trouble understanding how to work with a non default boolean constructor. The question marks are the points I'm confused about. I've agonized over it all day. I assume i am just over thinking it. Any and all hints or guidance is appreciated. While i wouldn't mind the code for it, i want to understand how this process works.
#include <ctime>
#include <iomanip>
#include <iostream>
#include <string>
#include "clock.h"
usingnamespace std;
Clock::Clock(?)
{
?
}
void Clock::displayClock() const
{
int hours = get_hours();
int minutes = get_minutes();
if(?) // logic needed to display in military format
{
// this is simply to add a 0 to single digit minutes
if(minutes < 10)
cout << get_location() << " time is " << hours << ":" << setfill('0') << setw(2) << minutes << endl;
else
cout << get_location() << " time is " << hours << ":" << minutes << endl;
}
else
{
/* this is the logic needed to convert military to standard time
i can sort this out after i figure out my problem */
if(minutes < 10)
cout << get_location() << " time is " << hours << ":" << setfill('0') << setw(2) << minutes << endl;
else
cout << get_location() << " time is " << hours << ":" << minutes << endl;
}
}
string Clock::get_location() const
{
return"Local";
}
int Clock::get_hours() const
{
time_t rawtime;
struct tm *info;
info = new tm;
time( &rawtime );
info = localtime( &rawtime );
return info->tm_hour;
}
int Clock::get_minutes() const
{
time_t rawtime;
struct tm *info;
info = new tm;
time( &rawtime );
info = localtime( &rawtime );
return info->tm_min;
}
bool Clock::is_military() const
{
?
}
// initialise the member 'military' with 'use_military'
Clock::Clock( bool use_military ) : military(use_military)
{
}
void Clock::displayClock() const
{
int hours = get_hours();
int minutes = get_minutes();
if( military ) // logic needed to display in military format
{
// this is simply to add a 0 to single digit minutes
// ...
}
else
{
// this is the logic needed to convert military to standard time
// ...
}
}
bool Clock::is_military() const
{
return military ;
}
// etc.
Thank you. I was seriously over thinking the problem. I also realized the (minutes < 10) is unnecessary as it will only auto fill if there isn't a value in the first field. This allowed me to do the output in a single line of code.