Advertisement
darearkin

date.cpp

Mar 16th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. // Fig. 10.11: Date.cpp
  2. // Date class member-function definitions.
  3. #include <iostream>
  4. #include "Time.h" // include Time class definition
  5. #include "Date.h" // include Date class definition
  6. using namespace std;
  7.  
  8. // constructor confirms proper value for month; calls
  9. // utility function checkDay to confirm proper value for day
  10. Date::Date( int month, int day, int year, Time time)
  11. {
  12. if ( month > 0 && month <= monthsPerYear ) // validate the month
  13. month = month;
  14. else
  15. {
  16. month = 1; // invalid month set to 1
  17. cout << "Invalid month (" << month << ") set to 1.\n";
  18. } // end else
  19.  
  20. time = time;
  21. year = year; // could validate year
  22. day = checkDay( day ); // validate the day
  23.  
  24. // output Date object to show when its constructor is called
  25. cout << "Date object constructor for date ";
  26. print();
  27. cout << endl;
  28. } // end Date constructor
  29.  
  30.  
  31. void Date::increaseADay()
  32. {
  33. static const int daysPerMonth[ monthsPerYear + 1 ] =
  34. { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  35. if (day + 1 <= daysPerMonth[month]) //check to see if the added day will
  36. //put the date over the month limit
  37. //as defined by daysPerMonth.
  38. day++; //add one to day if the above check passes
  39. else
  40. {day = 1; //else, reset day to 1 and check below.
  41. //If month is december, reset month to
  42. //january and add one to the year.
  43. if (month == 12)
  44. {month = 1;
  45. year++;}
  46. else
  47. month++;
  48. }//end function increaseADay */
  49. }
  50.  
  51. void Date::tick()
  52. {
  53. time.setSecond( time.getSecond() + 1 ); // increment second by 1
  54.  
  55. if ( time.getSecond() == 0 )
  56. {
  57. time.setMinute( time.getMinute() + 1 ); // increment minute by 1
  58.  
  59. if ( time.getMinute() == 0 )
  60. {
  61. time.setHour( time.getHour() + 1 ); // increment hour by 1
  62. if ( time.getHour() == 0 )
  63. {
  64. increaseADay(); // call function to increment day by 1.
  65. }// end if
  66. } // end if
  67. } // end if
  68. } // end function tick
  69.  
  70.  
  71. // print Date object in form month/day/year time/time 24hr
  72. void Date::print() const
  73. {
  74. cout << "\n" << month << '/' << day << '/' << year;
  75. time.printStandard();
  76. time.printUniversal();
  77. } // end function print
  78.  
  79. // output Date object to show when its destructor is called
  80. Date::~Date()
  81. {
  82. cout << "Date object destructor for date ";
  83. print();
  84. cout << endl;
  85. } // end ~Date destructor
  86.  
  87. // utility function to confirm proper day value based on
  88. // month and year; handles leap years, too
  89. int Date::checkDay( int testDay ) const
  90. {
  91. static const int daysPerMonth[ monthsPerYear + 1 ] =
  92. { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  93.  
  94. // determine whether testDay is valid for specified month
  95. if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
  96. return testDay;
  97.  
  98. // February 29 check for leap year
  99. if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
  100. ( year % 4 == 0 && year % 100 != 0 ) ) )
  101. return testDay;
  102.  
  103. cout << "Invalid day (" << testDay << ") set to 1.\n";
  104. return 1; // leave object in consistent state if bad value
  105. } // end function checkDay
  106.  
  107. /**************************************************************************
  108. * (C) Copyearight 1992-2010 by Deitel & Associates, Inc. and *
  109. * Pearson Education, Inc. All Rights Reserved. *
  110. * *
  111. * DISCLAIMER: The authors and publisher of this book have used their *
  112. * best efforts in preparing the book. These efforts include the *
  113. * development, research, and testing of the theories and programs *
  114. * to determine their effectiveness. The authors and publisher make *
  115. * no warranty of any kind, expressed or implied, with regard to these *
  116. * programs or to the documentation contained in these books. The authors *
  117. * and publisher shall not be liable in any event for incidental or *
  118. * consequential damages in connection with, or arising out of, the *
  119. * furnishing, performance, or use of these programs. *
  120. **************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement