Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. class Date
  8. {
  9. private:
  10. int year;
  11. int monthNum;
  12. int dayNum;
  13.  
  14. public:
  15.  
  16. Date(int newYear = 1900, int newMonth = 1, int newDay = 1);
  17.  
  18. void SetDate(int ChangeYear, int ChangeMonth, int ChangeDay);
  19. void DisplayNumerically();
  20. void DisplayTextually();
  21. };
  22.  
  23. int main()
  24. {
  25.  
  26. Date f(1980, 6, 8);
  27. f.DisplayNumerically(); /// prints "06/08/1980"
  28. f.DisplayTextually(); /// prints "June 8, 1980"
  29.  
  30. f.SetDate(1980, 6, 31); /// prints "Error: SetDate() called with invalid date information"
  31. f.DisplayNumerically(); /// prints "06/08/1980"
  32. f.DisplayTextually(); /// prints "June 8, 1980"*
  33.  
  34. Date::Date(int newYear, int newMonth, int newDay)
  35. {
  36. year = newYear;
  37. monthNum = newMonth;
  38. dayNum = newDay;
  39. if(newDay > 30)
  40. {
  41. cout <<"Error: Date created with invalid date information."<<endl;
  42. year = 1900;
  43. monthNum = 1;
  44. dayNum = 1;
  45. }
  46. }
  47. void Date::SetDate(int ChangeYear, int ChangeMonth, int ChangeDay)
  48. {
  49. year = ChangeYear;
  50. monthNum = ChangeMonth;
  51. dayNum = ChangeDay;
  52. if(ChangeMonth > 12||ChangeDay > 30)
  53. {
  54. cout <<"Error: SetDate() called with invalid date information."<<endl;
  55. year=1;
  56. monthNum=1;
  57. dayNum=1;
  58. }
  59. }
  60. void Date::DisplayNumerically()
  61. {
  62. printf("%02d",monthNum);
  63. printf("/%02d",dayNum);
  64. printf("/%04dn",year);
  65. }
  66.  
  67. void Date::DisplayTextually()
  68. {
  69. string m;
  70. if( monthNum == 1)
  71. m = "January";
  72. else if( monthNum == 2)
  73. m = "February";
  74. else if( monthNum == 3)
  75. m = "March";
  76. else if( monthNum == 4)
  77. m = "April";
  78. else if( monthNum == 5)
  79. m = "May";
  80. else if( monthNum == 6)
  81. m = "June";
  82. else if( monthNum == 7)
  83. m = "July";
  84. else if( monthNum == 8)
  85. m = "August";
  86. else if( monthNum == 9)
  87. m = "September";
  88. else if( monthNum == 10)
  89. m = "October";
  90. else if( monthNum == 11)
  91. m = "November";
  92. else if( monthNum == 12)
  93. m = "December";
  94. else
  95. cout <<"Invalid month input: "<< monthNum << endl;
  96.  
  97. cout << m <<" "<<fixed<<setprecision(2)<<dayNum<<","<<year<<endl;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement