Calendar Date Class

Calendar Date Class - Calendar Date Class As a means of...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Calendar Date Class As a means of illustrating what an actual large and complete C++ class looks like, we will present a class for managing calendar dates. Commentary on this class is given below the source. First of all, the header: // Date class header file #ifndef __DATE_H__ #define __DATE_H__ typedef unsigned short Drep; // internal storage format const int MIN_YEAR = 1875; const int MAX_YEAR = 2025; const Drep MAX_DAY = 55152; const int DOW_MIN = 6; class Date { Drep d; // actual date static int init_flag; // init flag static int isleap(int); // leap year? static Drep cdays[MAX_YEAR-MIN_YEAR+1]; // cumul days per yr static void init_date(); // initialize date static Drep mdy_to_d(int, int, int); // m/d/y --> day static void d_to_mdy(Drep,int&,int&,int&);// day --> m/d/y public: Date(Drep); // constructor from internal Date(const Date&); // copy constructor Date(int, int, int); // constructor from m/d/y Date(const char*); // constructor from char* operator Drep(); // conversion to Drep void print(char* = (char*)0); // print void get_mdy(int&, int&, int&); // get m/d/y long operator-(const Date&); // difference of dates int dow(); // day of week long wdays(const Date&); // work days between dates }; #endif and then the source itself, along with a driver program: // Date class and driver program #include <string.h> #include <stdlib.h> #include <stdio.h> #include <ctype.h> #include <assert.h> #include "date.h" // days in the various months const char days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Drep Date::cdays[MAX_YEAR - MIN_YEAR + 1]; int Date::init_flag = 0; // initialize date structures void Date::init_date() { int i; Drep cumul = 0; init_flag = 1; for (i = MIN_YEAR; i <= MAX_YEAR; i++) { cumul += 365 + isleap(i); cdays[i - MIN_YEAR] = cumul; } } // a leap year?
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 8

Calendar Date Class - Calendar Date Class As a means of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online