Define output operators for Clock and Travelclock. Modify the classes Clock and
ID: 3550652 • Letter: D
Question
Define output operators for Clock and Travelclock. Modify the classes Clock and TravelClock to declare the output operators as friends.#ifndef CCC_TIME_H #define CCC_TIME_H
#include <string>
using namespace std;
class Time { public: Time::Time(); // Constructs the current time. Time::Time(int h, int m, int s); // Constructs the time with hours h, minutes m, and seconds s. int Time::get_seconds(void) const; int Time::get_minutes(void) const; // Function: Returns the minutes value of this time. int Time::get_hours(void) const; // Function: Returns the hours value of this time. void Time::add_seconds(int n); // Function: Changes this time to move by n seconds. int Time::seconds_from(void) const;
}; #endif
======================================================================= #ifndef CLOCK_H #define CLOCK_H
#include <string>
using namespace std;
class Clock { public: /** Constructs a clock that can tell the local time. @param use_military true if the clock uses military format */ Clock(bool use_military); /** Gets the location of this clock. @return the location */ string get_location() const; /** Gets the hours of this clock. @return the hours, in military or am/pm format */ int get_hours() const;
/** Gets the minutes of this clock. @return the minutes */ int get_minutes() const;
/** Checks whether this clock uses military format. @return true if military format */ bool is_military() const; private: bool military; }; #endif
========================================================================= #include "ccc_time.h" #include "clock.h"
Clock::Clock(bool use_military) { military = use_military; }
string Clock::get_location() const { return "Local"; }
int Clock::get_hours() const { Time now; int hours = now.get_hours(); if (military) return hours; if(hours == 0) return 12; else if (hours > 12) return hours - 12; else return hours; }
int Clock::get_minutes() const { Time now; return now.get_minutes(); }
bool Clock::is_military() const { return military; }
========================================================================== #include <iostream> #include <iomanip> #include <string>
using namespace std;
#include "clock.h"
int main() { Clock clock1(true); Clock clock2(false);
bool more = true; while (more) { cout << "military time is " << clock1.get_hours() << ":" << setw(2) << setfill('0') << clock1.get_minutes() << setfill(' ') << " "; cout << "am/pm time is " << clock2.get_hours() << ":" << setw(2) << setfill('0') << clock2.get_minutes() << setfill(' ') << " "; cout << "Try again? (y/n) "; string input; getline(cin, input); if (input != "y") more = false; } system("pause"); return 0; }
============================================================================
class TravelClock : public Clock { public: TravelClock(bool mil, string loc, int diff); int get_hours() const; string get_location() const; private: string location; int time_difference; };
TravelClock clock(true, "New York", 3); cout << "The time in " << clock.get_location() << " is " << clock.get_hours() << ":" << clock.get_minutes(); Define output operators for Clock and Travelclock. Modify the classes Clock and TravelClock to declare the output operators as friends.
#ifndef CCC_TIME_H #define CCC_TIME_H
#include <string>
using namespace std;
class Time { public: Time::Time(); // Constructs the current time. Time::Time(int h, int m, int s); // Constructs the time with hours h, minutes m, and seconds s. int Time::get_seconds(void) const; int Time::get_minutes(void) const; // Function: Returns the minutes value of this time. int Time::get_hours(void) const; // Function: Returns the hours value of this time. void Time::add_seconds(int n); // Function: Changes this time to move by n seconds. int Time::seconds_from(void) const;
}; #endif
======================================================================= #ifndef CLOCK_H #define CLOCK_H
#include <string>
using namespace std;
class Clock { public: /** Constructs a clock that can tell the local time. @param use_military true if the clock uses military format */ Clock(bool use_military); /** Gets the location of this clock. @return the location */ string get_location() const; /** Gets the hours of this clock. @return the hours, in military or am/pm format */ int get_hours() const;
/** Gets the minutes of this clock. @return the minutes */ int get_minutes() const;
/** Checks whether this clock uses military format. @return true if military format */ bool is_military() const; private: bool military; }; #endif
========================================================================= #ifndef CLOCK_H #define CLOCK_H
#include <string>
using namespace std;
class Clock { public: /** Constructs a clock that can tell the local time. @param use_military true if the clock uses military format */ Clock(bool use_military); /** Gets the location of this clock. @return the location */ string get_location() const; /** Gets the hours of this clock. @return the hours, in military or am/pm format */ int get_hours() const;
/** Gets the minutes of this clock. @return the minutes */ int get_minutes() const;
/** Checks whether this clock uses military format. @return true if military format */ bool is_military() const; private: bool military; }; #endif
========================================================================= #include "ccc_time.h" #include "clock.h"
Clock::Clock(bool use_military) { military = use_military; }
string Clock::get_location() const { return "Local"; }
int Clock::get_hours() const { Time now; int hours = now.get_hours(); if (military) return hours; if(hours == 0) return 12; else if (hours > 12) return hours - 12; else return hours; }
int Clock::get_minutes() const { Time now; return now.get_minutes(); }
bool Clock::is_military() const { return military; } #include "ccc_time.h" #include "clock.h"
Clock::Clock(bool use_military) { military = use_military; }
string Clock::get_location() const { return "Local"; }
int Clock::get_hours() const { Time now; int hours = now.get_hours(); if (military) return hours; if(hours == 0) return 12; else if (hours > 12) return hours - 12; else return hours; }
int Clock::get_minutes() const { Time now; return now.get_minutes(); }
bool Clock::is_military() const { return military; }
========================================================================== #include <iostream> #include <iomanip> #include <string>
using namespace std;
#include "clock.h"
int main() { Clock clock1(true); Clock clock2(false);
bool more = true; while (more) { cout << "military time is " << clock1.get_hours() << ":" << setw(2) << setfill('0') << clock1.get_minutes() << setfill(' ') << " "; cout << "am/pm time is " << clock2.get_hours() << ":" << setw(2) << setfill('0') << clock2.get_minutes() << setfill(' ') << " "; cout << "Try again? (y/n) "; string input; getline(cin, input); if (input != "y") more = false; } system("pause"); return 0; } #include <iostream> #include <iomanip> #include <string>
using namespace std;
#include "clock.h"
int main() { Clock clock1(true); Clock clock2(false);
bool more = true; while (more) { cout << "military time is " << clock1.get_hours() << ":" << setw(2) << setfill('0') << clock1.get_minutes() << setfill(' ') << " "; cout << "am/pm time is " << clock2.get_hours() << ":" << setw(2) << setfill('0') << clock2.get_minutes() << setfill(' ') << " "; cout << "Try again? (y/n) "; string input; getline(cin, input); if (input != "y") more = false; } system("pause"); return 0; }
============================================================================
class TravelClock : public Clock { public: TravelClock(bool mil, string loc, int diff); int get_hours() const; string get_location() const; private: string location; int time_difference; };
class TravelClock : public Clock { public: TravelClock(bool mil, string loc, int diff); int get_hours() const; string get_location() const; private: string location; int time_difference; };
TravelClock clock(true, "New York", 3); cout << "The time in " << clock.get_location() << " is " << clock.get_hours() << ":" << clock.get_minutes(); TravelClock clock(true, "New York", 3); cout << "The time in " << clock.get_location() << " is " << clock.get_hours() << ":" << clock.get_minutes();
Explanation / Answer
#ifndef CCC_TIME_H
#define CCC_TIME_H
#include <string>
using namespace std;
class Time
{
public:
Time::Time(); // Constructs the current time.
Time::Time(int h, int m, int s); // Constructs the time with hours h, minutes m, and seconds s.
int Time::get_seconds(void) const;
int Time::get_minutes(void) const; // Function: Returns the minutes value of this time.
int Time::get_hours(void) const; // Function: Returns the hours value of this time.
void Time::add_seconds(int n); // Function: Changes this time to move by n seconds.
int Time::seconds_from(void) const;
};
#endif
=======================================================================
#ifndef CLOCK_H
#define CLOCK_H
#include <string>
using namespace std;
class Clock
{
public:
/** Overloading Output Operator for Clock class
*/
friend ostream& operator<< (ostream &out, Clock &cClock);
/**
Constructs a clock that can tell the local time.
@param use_military true if the clock uses military format
*/
Clock(bool use_military);
/**
Gets the location of this clock.
@return the location
*/
string get_location() const;
/**
Gets the hours of this clock.
@return the hours, in military or am/pm format
*/
int get_hours() const;
/**
Gets the minutes of this clock.
@return the minutes
*/
int get_minutes() const;
/**
Checks whether this clock uses military format.
@return true if military format
*/
bool is_military() const;
private:
bool military;
};
#endif
=========================================================================
#include "ccc_time.h"
#include "clock.h"
Clock::Clock(bool use_military)
{
military = use_military;
}
string Clock::get_location() const
{
return "Local";
}
int Clock::get_hours() const
{
Time now;
int hours = now.get_hours();
if (military) return hours;
if(hours == 0)
return 12;
else if (hours > 12)
return hours - 12;
else
return hours;
}
int Clock::get_minutes() const
{
Time now;
return now.get_minutes();
}
bool Clock::is_military() const
{
return military;
}
ostream& operator<< (ostream &out, Clock &cClock)
{
// Since operator<< is a friend of the Clock class, we can access Clock's members directly.
out << (cClock.military?"military time is ": "am/pm time is ") << << cClock.get_hours() << ":" << setw(2) << setfill('0')
<< cClock.get_minutes() << setfill(' ') << " ";
return out;
}
==========================================================================
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "clock.h"
int main()
{
Clock clock1(true);
Clock clock2(false);
bool more = true;
while (more)
{
cout << clock1;
cout << clock2
cout << "Try again? (y/n) ";
string input;
getline(cin, input);
if (input != "y") more = false;
}
system("pause");
return 0;
}
============================================================================
class TravelClock : public Clock
{
public:
TravelClock(bool mil, string loc, int diff);
int get_hours() const; string get_location() const;
/** Overloading Output Operator for Clock class
*/
friend ostream& operator<< (ostream &out, TravelClock &cTravelClock);
private:
string location;
int time_difference;
};
ostream& operator<< (ostream &out, TravelClock &cTravelClock)
{
// Since operator<< is a friend of the TravelClock class, we can access TravelClock's members directly.
out << "The time in " << cTravelClock.get_location() << " is " << cTravelClock.get_hours() << ":" << cTravelClock.get_minutes();
return out;
}
TravelClock clock(true, "New York", 3);
cout << clock;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.