Advertisement
Guest User

Untitled

a guest
Oct 30th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include "Date.h"
  5.  
  6. ostream& operator<< (ostream &output, const Date &a)
  7. {
  8. output << a.month << '/' << a.day << '/' << a.year;
  9. return output;
  10. }
  11.  
  12. istream& operator>>(istream &input, Date &a)
  13. {
  14. input >> a.month;
  15. cin.ignore(1);
  16. input >> a.day;
  17. cin.ignore(1);
  18. input >> a.year;
  19.  
  20. return input;
  21. }
  22.  
  23. //constructor validates month and calls utility function to validate day
  24. Date::Date(int mn, int dy, int yr)
  25. {
  26. setDate(mn, dy, yr);
  27. }
  28.  
  29.  
  30. Date& Date::setDate(int mn, int dy, int yr)
  31. {
  32. if(mn > 0 && mn <= 12)
  33. {
  34. month = mn;
  35. }
  36.  
  37. else
  38. {
  39. month = 1;
  40. }
  41.  
  42. year = yr; //could also validate year
  43.  
  44. day = checkDay(dy); //to validate the day
  45.  
  46. return *this; //should this be here?
  47. }
  48.  
  49. //PRIVATE COST UTILITY FUNCTION
  50. int Date::checkDay(int testDay) const
  51. {
  52. static const int daysPerMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  53.  
  54. if(testDay > 0 && testDay <= daysPerMonth[month])
  55. {
  56. return testDay;
  57. }
  58.  
  59. //determine whether testDay is valid for a specific month
  60. if(month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
  61. {
  62. return testDay;
  63. }
  64.  
  65. cout << "Invalid Day " << testDay << " was set to 1" << endl;
  66. return 1; //keep data in valid state
  67. }
  68.  
  69.  
  70. int Date::getMonth() const
  71. {
  72. return month;
  73. }
  74.  
  75. Date& Date::setMonth(int m)
  76. {
  77. month = (m >= 1 && m <= 12) ? m : 1; //validates month, if valid set to m, else set to 1
  78. return *this;
  79. }
  80.  
  81.  
  82. int Date::getDay() const
  83. {
  84. return day;
  85. }
  86.  
  87. Date& Date::setDay(int d)
  88. {
  89. day = checkDay(d);
  90. return *this;
  91. }
  92.  
  93.  
  94. int Date::getYear() const
  95. {
  96. return year;
  97. }
  98.  
  99. Date& Date::setYear(int y)
  100. {
  101. year = y;
  102. return *this;
  103. }
  104.  
  105. //void Date::operator= (const Date &a)
  106. //{
  107. // month = a.month;
  108. // day = a.day;
  109. // year = a.year;
  110. //
  111. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement