Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. // File Date.cpp
  2. // Implementation file for the Date class
  3. // IEA February 1997
  4. #include <iostream>
  5. #include "Date.h"
  6. using namespace std;
  7. // Constructor functions
  8. Date::Date(int dd,int mm,int yy)
  9. {
  10.     day = dd;
  11.     month = mm;
  12.     year = yy;
  13. }
  14. Date::Date()
  15. {
  16.     day = 1;
  17.     month = 1;
  18.     year = 1900;
  19. }
  20. // Accessor functions
  21. int Date::get_day()
  22. {
  23.     return day;
  24. }
  25. int Date::get_month()
  26. {
  27.     return month;
  28. }
  29. int Date::get_year()
  30. {
  31.     return year;
  32. }
  33. // Other functions
  34. void Date::print()
  35. {
  36.     cout << day << "/" << month << "/" << year;
  37. }
  38. void Date::next_day()
  39. {
  40.     int days_in_month;
  41.     day++;
  42.     switch (month)
  43.     {
  44.         case 1 :
  45.         case 3 :
  46.         case 5 :
  47.         case 7 :
  48.         case 8 :
  49.         case 10:
  50.         case 12: days_in_month = 31;
  51.         break;
  52.         case 4 :
  53.         case 6 :
  54.         case 9 :
  55.         case 11: days_in_month = 30;
  56.         break;
  57.         case 2 : if (leap_year())
  58.         days_in_month = 29;
  59.         else days_in_month = 28;
  60.         break;
  61.     }
  62.     if (day > days_in_month)
  63.     {
  64.         day = 1;
  65.         month++;
  66.         if (month > 12)
  67.         {
  68.             month = 1;
  69.             year++;
  70.         }
  71.     }
  72. }
  73. void Date::week_later()
  74. // This function returns the date a week
  75. // later than date.
  76. {
  77.     Date temp;
  78.     int days_in_month;
  79.     int day,month,year;
  80.     day = date.get_day();
  81.     month = date.get_month();
  82.     year = date.get_year();
  83.     day+= 7;
  84.     switch (month)
  85.     {
  86.         case 1 :
  87.         case 3 :
  88.         case 5 :
  89.         case 7 :
  90.         case 8 :
  91.         case 10:
  92.         case 12: days_in_month = 31;
  93.         break;
  94.         case 4 :
  95.         case 6 :
  96.         case 9 :
  97.         case 11: days_in_month = 30;
  98.         break;
  99.         case 2 : if (leap_year(year))
  100.         days_in_month = 29;
  101.         else days_in_month = 28;
  102.         break;
  103.     }
  104.     if (day > days_in_month) {
  105.         day = day-days_in_month;
  106.         month++;
  107.         if (month > 12) {
  108.             month = 1;
  109.             year++;
  110.         }
  111.     }
  112.     temp = Date(day,month,year);
  113.     return temp;
  114. }
  115. // Private function
  116. int Date::leap_year()
  117. {
  118.     if (year % 400 == 0)
  119.     return 1;
  120.     else if (year % 100 == 0)
  121.     return 0;
  122.     else if (year % 4 == 0)
  123.     return 1;
  124.     else return 0;
  125. }
  126.  
  127. *****************************
  128.  
  129. // File Date.h
  130. // Definition file for the class Date
  131. // which represents a simple date class
  132. // IEA February 1997
  133. #ifndef DATE_H
  134. #define DATE_H
  135. class Date
  136. {
  137.     public:
  138.         // Constructors
  139.         // initialise to 1/1/1900
  140.         Date();
  141.         // initialise to dd/mm/yy
  142.         Date(int dd,int mm,int yy);
  143.         // member functions
  144.         void print(); // prints date
  145.         void next_day(); // sets date to next day
  146.         void week_later(); //+7days
  147.         // Accessor functions
  148.         int get_day(); // returns day
  149.         int get_month(); // returns month
  150.         int get_year(); // returns year
  151.     private:
  152.         int day;
  153.         int month;
  154.         int year;
  155.         int leap_year(); // returns true if year is a leap
  156.         // year. Required in next_day()
  157. };
  158. #endif
  159.  
  160. ****************************
  161.  
  162. // A test program for the class Date
  163. // IEA February 1997
  164. #include <iostream>
  165. #include "Date.h"
  166. int main()
  167. {
  168.     Date d1, d2(21,8,34);
  169.     int d,m,y;
  170.     // check initialisation is correct
  171.     std::cout << "First date is ";
  172.     d1.print();
  173.     std::cout << std::endl;
  174.     std::cout << "Second date is ";
  175.     d2.print();
  176.     std::cout << std::endl;
  177.     // check accessor functions
  178.     d = d2.get_day();
  179.     m = d2.get_month();
  180.     y = d2.get_year();
  181.     std::cout << std::endl << d << " "
  182.     << m << " "
  183.     << y << std::endl;
  184.     // check next day is correct
  185.     d2.next_day();
  186.     std::cout << "Next day is ";
  187.     d2.print();
  188.     std::cout << std::endl;
  189.     // check setting date by assignment
  190.     // and another check on next day.
  191.     d1 = Date(31,12,1999);
  192.     d1.next_day();
  193.     std::cout << "Day after 31/12/1999 is ";
  194.     d1.print();
  195.     std::cout << std::endl;
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement