Advertisement
VSkim

Untitled

Oct 3rd, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. class DayOfYear
  5. {
  6. public:
  7. void input( );
  8. void output( );
  9. void set(int newMonth, int newDay);
  10. //Precondition: newMonth and newDay form a possible date.
  11. void set(int newMonth);
  12. //Precondition: 1 <= newMonth <= 12
  13. //Postcondition: The date is set to the first day of the given month.
  14. int getMonthNumber( ); //Returns 1 for January, 2 for February, etc.
  15. int getDay( );
  16. private:
  17. int month;
  18. int day;
  19. };
  20. int main( )
  21. {
  22. DayOfYear today, bachBirthday;
  23. cout << "Enter today's date:\n";
  24. today.input( );
  25. cout << "Today's date is ";
  26. today.output( );
  27. cout << endl;
  28. bachBirthday.set(3, 21);
  29. cout << "J. S. Bach's birthday is ";
  30. bachBirthday.output( );
  31. cout << endl;
  32. if ( today.getMonthNumber( ) == bachBirthday.getMonthNumber( ) &&
  33. today.getDay( ) == bachBirthday.getDay( ) )
  34. cout << "Happy Birthday Johann Sebastian!\n";
  35. else
  36. cout << "Happy Unbirthday Johann Sebastian!\n";
  37. return 0;
  38. }
  39. void DayOfYear::set(int newMonth, int newDay)
  40. {
  41. if ((newMonth >= 1) && (newMonth <= 12))
  42. month = newMonth;
  43. else
  44. {
  45. cout << "Illegal month value! Program aborted.\n";
  46. exit(1);
  47. }
  48. if ((newDay >= 1) && (newDay <= 31))
  49. day = newDay;
  50. else
  51. {
  52. cout << "Illegal day value! Program aborted.\n";
  53. exit(1);
  54. }
  55. }
  56. //Uses iostream and cstdlib:
  57. void DayOfYear::set(int newMonth)
  58. {
  59. if ((newMonth >= 1) && (newMonth <= 12))
  60. month = newMonth;
  61. else
  62. {
  63. cout << "Illegal month value! Program aborted.\n";
  64. exit(1);
  65. }
  66. day = 1;
  67. }
  68. int DayOfYear::getMonthNumber( )
  69. {
  70. return month;
  71. }
  72. int DayOfYear::getDay( )
  73. {
  74. return day;
  75. }
  76. //Uses iostream and cstdlib:
  77. void DayOfYear::input( )
  78. {
  79. cout << "Enter the month as a number: ";
  80. cin >> month;
  81. cout << "Enter the day of the month: ";
  82. cin >> day;
  83. if ((month < 1) || (month > 12) || (day < 1) || (day > 31))
  84. {
  85. cout << "Illegal date! Program aborted.\n";
  86. exit(1);
  87. }
  88. }
  89. void DayOfYear::output( )
  90. {
  91. switch (month)
  92. {
  93. case 1:
  94. cout << "January "; break;
  95. case 2:
  96. cout << "February "; break;
  97. case 3:
  98. cout << "March "; break;
  99. case 4:
  100. cout << "April "; break;
  101. case 5:
  102. cout << "May "; break;
  103. case 6:
  104. cout << "June "; break;
  105. case 7:
  106. cout << "July "; break;
  107. case 8:
  108. cout << "August "; break;
  109. case 9:
  110. cout << "September "; break;
  111. case 10:
  112. cout << "October "; break;
  113. case 11:
  114. cout << "November "; break;
  115. case 12:
  116. cout << "December "; break;
  117. default:
  118. cout << "Error in DayOfYear::output. Contact software vendor.";
  119. }
  120. cout << day;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement