Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fig. 10.11: Date.cpp
- // Date class member-function definitions.
- #include <iostream>
- #include "Time.h" // include Time class definition
- #include "Date.h" // include Date class definition
- using namespace std;
- // constructor confirms proper value for month; calls
- // utility function checkDay to confirm proper value for day
- Date::Date( int month, int day, int year, Time time)
- {
- if ( month > 0 && month <= monthsPerYear ) // validate the month
- month = month;
- else
- {
- month = 1; // invalid month set to 1
- cout << "Invalid month (" << month << ") set to 1.\n";
- } // end else
- time = time;
- year = year; // could validate year
- day = checkDay( day ); // validate the day
- // output Date object to show when its constructor is called
- cout << "Date object constructor for date ";
- print();
- cout << endl;
- } // end Date constructor
- void Date::increaseADay()
- {
- static const int daysPerMonth[ monthsPerYear + 1 ] =
- { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- if (day + 1 <= daysPerMonth[month]) //check to see if the added day will
- //put the date over the month limit
- //as defined by daysPerMonth.
- day++; //add one to day if the above check passes
- else
- {day = 1; //else, reset day to 1 and check below.
- //If month is december, reset month to
- //january and add one to the year.
- if (month == 12)
- {month = 1;
- year++;}
- else
- month++;
- }//end function increaseADay */
- }
- void Date::tick()
- {
- time.setSecond( time.getSecond() + 1 ); // increment second by 1
- if ( time.getSecond() == 0 )
- {
- time.setMinute( time.getMinute() + 1 ); // increment minute by 1
- if ( time.getMinute() == 0 )
- {
- time.setHour( time.getHour() + 1 ); // increment hour by 1
- if ( time.getHour() == 0 )
- {
- increaseADay(); // call function to increment day by 1.
- }// end if
- } // end if
- } // end if
- } // end function tick
- // print Date object in form month/day/year time/time 24hr
- void Date::print() const
- {
- cout << "\n" << month << '/' << day << '/' << year;
- time.printStandard();
- time.printUniversal();
- } // end function print
- // output Date object to show when its destructor is called
- Date::~Date()
- {
- cout << "Date object destructor for date ";
- print();
- cout << endl;
- } // end ~Date destructor
- // utility function to confirm proper day value based on
- // month and year; handles leap years, too
- int Date::checkDay( int testDay ) const
- {
- static const int daysPerMonth[ monthsPerYear + 1 ] =
- { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- // determine whether testDay is valid for specified month
- if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
- return testDay;
- // February 29 check for leap year
- if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
- ( year % 4 == 0 && year % 100 != 0 ) ) )
- return testDay;
- cout << "Invalid day (" << testDay << ") set to 1.\n";
- return 1; // leave object in consistent state if bad value
- } // end function checkDay
- /**************************************************************************
- * (C) Copyearight 1992-2010 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. *
- **************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement