Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- #include "Time.h"
- ostream& operator<< (ostream &output, const Time &a)
- {
- output << ((a.hour == 0 || a.hour == 12) ? 12 : a.hour % 12) << ":"
- << setfill ('0') << setw(2) << a.minute << (a.hour < 12 ? "AM" : "PM" );
- return output;
- }
- istream& operator>>(istream &input, Time &a)
- {
- input >> a.hour;
- cin.ignore(1);
- input >> a.minute;
- return input;
- }
- //Constructor function to initialize private data
- //remember a constructor is called whenever a new object of
- //a class data type is instantiated, if a constructor is not defined the C++ do
- //nothing constructor is run, it is better to ALWAYS have your own contructor
- //once you have your own constructor the default is no longer available
- //Constructors can and should be overloaded
- //Constructors cannot be const since they always manipulate private data
- Time::Time(int hour, int minute)
- {
- setTime(hour, minute);
- }
- /*SET FUNCTIONS: Never const since they need to modify private member data*/
- //setTime function now is set up to enable cascading
- Time& Time::setTime(int hour, int minute)
- {
- setHour(hour);
- setMinute(minute);
- return *this;
- }
- //setHour function is now set up to enable cascading
- Time& Time::setHour(int h)
- {
- hour = (h >= 0 && h < 24) ? h : 0; //validates hour, if valid set to h, else set to 0
- return *this;
- }
- //setMinute function is now set up to enable cascading
- Time& Time::setMinute(int m)
- {
- minute = (m >= 0 && m < 60) ? m : 0; //validates minute, if valid set to m, else set to 0
- return *this;
- }
- /*GET FUNCTIONS: Do not modify private member data normally always const*/
- //get Hour
- int Time::getHour() const //must be const since prototype is const
- {
- return hour;
- }
- //get Minute
- int Time::getMinute() const //must be const since prototype is const
- {
- return minute;
- }
- /*PRINT FUNCTIONS: Normally do not modify private member data so should be const*/
- void Time::printUniversal()const //must be const since prototype is const
- {
- cout << setfill('0') << setw(2) << hour << ":"
- << setw(2) << minute << endl;
- }
- void Time::printStandard()const //must be const since prototype is const
- {
- cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":"
- << setfill ('0') << setw(2) << minute << (hour < 12 ? "AM" : "PM" )<< endl;
- }
- //void Time::operator= (const Time &a)
- //{
- // hour = a.hour;
- // minute = a.minute;
- //}
- bool Time::operator== (const Time &a)
- {
- if ( hour == a.getHour() )
- {
- if( minute == a.getMinute() )
- {
- return true;
- }
- }
- return false;
- }
- bool Time::operator!= (const Time &a)
- {
- if ( getHour() == a.getHour() )
- {
- if( getMinute() == a.getMinute() )
- {
- return false;
- }
- }
- return true;
- }
- bool Time::operator<= (const Time &a)
- {
- if ( getHour() < a.getHour() )
- {
- return true;
- }
- if( getHour() == a.getHour() )
- {
- if( getMinute() <= a.getMinute() )
- {
- return true;
- }
- }
- return false;
- }
- bool Time::operator< (const Time &a)
- {
- if ( getHour() < a.getHour() )
- {
- return true;
- }
- if( getHour() == a.getHour() )
- {
- if( getMinute() < a.getMinute() )
- {
- return true;
- }
- }
- return false;
- }
- bool Time::operator>= (const Time &a)
- {
- if ( getHour() > a.getHour() )
- {
- return true;
- }
- if( getHour() == a.getHour() )
- {
- if( getMinute() >= a.getMinute() )
- {
- return true;
- }
- }
- return false;
- }
- bool Time::operator> (const Time &a)
- {
- if ( getHour() > a.getHour() )
- {
- return true;
- }
- if( getHour() == a.getHour() )
- {
- if( getMinute() > a.getMinute() )
- {
- return true;
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement