Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File Date.cpp
- // Implementation file for the Date class
- // IEA February 1997
- #include <iostream>
- #include "Date.h"
- using namespace std;
- // Constructor functions
- Date::Date(int dd,int mm,int yy)
- {
- day = dd;
- month = mm;
- year = yy;
- }
- Date::Date()
- {
- day = 1;
- month = 1;
- year = 1900;
- }
- // Accessor functions
- int Date::get_day()
- {
- return day;
- }
- int Date::get_month()
- {
- return month;
- }
- int Date::get_year()
- {
- return year;
- }
- // Other functions
- void Date::print()
- {
- cout << day << "/" << month << "/" << year;
- }
- void Date::next_day()
- {
- int days_in_month;
- day++;
- switch (month)
- {
- case 1 :
- case 3 :
- case 5 :
- case 7 :
- case 8 :
- case 10:
- case 12: days_in_month = 31;
- break;
- case 4 :
- case 6 :
- case 9 :
- case 11: days_in_month = 30;
- break;
- case 2 : if (leap_year())
- days_in_month = 29;
- else days_in_month = 28;
- break;
- }
- if (day > days_in_month)
- {
- day = 1;
- month++;
- if (month > 12)
- {
- month = 1;
- year++;
- }
- }
- }
- void Date::week_later()
- // This function returns the date a week
- // later than date.
- {
- Date temp;
- int days_in_month;
- int day,month,year;
- day = date.get_day();
- month = date.get_month();
- year = date.get_year();
- day+= 7;
- switch (month)
- {
- case 1 :
- case 3 :
- case 5 :
- case 7 :
- case 8 :
- case 10:
- case 12: days_in_month = 31;
- break;
- case 4 :
- case 6 :
- case 9 :
- case 11: days_in_month = 30;
- break;
- case 2 : if (leap_year(year))
- days_in_month = 29;
- else days_in_month = 28;
- break;
- }
- if (day > days_in_month) {
- day = day-days_in_month;
- month++;
- if (month > 12) {
- month = 1;
- year++;
- }
- }
- temp = Date(day,month,year);
- return temp;
- }
- // Private function
- int Date::leap_year()
- {
- if (year % 400 == 0)
- return 1;
- else if (year % 100 == 0)
- return 0;
- else if (year % 4 == 0)
- return 1;
- else return 0;
- }
- *****************************
- // File Date.h
- // Definition file for the class Date
- // which represents a simple date class
- // IEA February 1997
- #ifndef DATE_H
- #define DATE_H
- class Date
- {
- public:
- // Constructors
- // initialise to 1/1/1900
- Date();
- // initialise to dd/mm/yy
- Date(int dd,int mm,int yy);
- // member functions
- void print(); // prints date
- void next_day(); // sets date to next day
- void week_later(); //+7days
- // Accessor functions
- int get_day(); // returns day
- int get_month(); // returns month
- int get_year(); // returns year
- private:
- int day;
- int month;
- int year;
- int leap_year(); // returns true if year is a leap
- // year. Required in next_day()
- };
- #endif
- ****************************
- // A test program for the class Date
- // IEA February 1997
- #include <iostream>
- #include "Date.h"
- int main()
- {
- Date d1, d2(21,8,34);
- int d,m,y;
- // check initialisation is correct
- std::cout << "First date is ";
- d1.print();
- std::cout << std::endl;
- std::cout << "Second date is ";
- d2.print();
- std::cout << std::endl;
- // check accessor functions
- d = d2.get_day();
- m = d2.get_month();
- y = d2.get_year();
- std::cout << std::endl << d << " "
- << m << " "
- << y << std::endl;
- // check next day is correct
- d2.next_day();
- std::cout << "Next day is ";
- d2.print();
- std::cout << std::endl;
- // check setting date by assignment
- // and another check on next day.
- d1 = Date(31,12,1999);
- d1.next_day();
- std::cout << "Day after 31/12/1999 is ";
- d1.print();
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement