This C++ date class is intended to illustrate how to write a non- trivial class in C++.
Even though this class is non-trivial, it is still simple enough for a new C++ programmer to follow all of the details of the C++ program. Initially the constructor sets the date to current date and format to 1. This class also makes use of #ifndef directive and DOS System calls to get the date.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | #ifndef DATE_H #define DATE_H class date { protected: int month; // 1 through 12 int day; // 1 through max_days int year; // 1500 through 2200 static char out_string[25]; // format output area static char format; // format to use for output // Calculate how many days are in any given month // Note - This is a private method which can be called only // from within the class itself int days_this_month(void); public: // Constructor - Set the date to the current date and set // the format to 1 date(void); // Set the date to these input parameters // if return = 0 ---> All data is valid // if return = 1 ---> Something out of range int set_date(int in_month, int in_day, int in_year); // Get the month, day, or year of the stored date int get_month(void) { return month; }; int get_day(void) { return day; }; int get_year(void) { return year; }; // Select the desired string output format for use when the // get_date_string is called void set_date_format(int format_in) { format = format_in; }; // return an ASCII-Z string depending on the stored format // format = 1 Aug 29, 1991 // format = 2 8/29/91 // format = 3 8/29/1991 // format = 4 29 Aug 1991 Military time // format = ? Anything else defaults to format 1 char *get_date_string(void); // return Jan Feb Mar Apr etc. char *get_month_string(void); }; #endif //end of the date.h //date.cpp file implementation // This file contains the implementation for the date class. #include <sprintf.h> // Prototype for sprintf #include <date.h> // Prototypes for the current date #include "date.h" char date::format; // This defines the static data member char date::out_string[25]; // This defines the static string // Constructor - Set date to current date, and // set format to the default of 1 date::date(void) { time_t time_date; struct tm *current_date; time_date = time(NULL); // DOS system call current_date = localtime(&time_date); // DOS system call month = current_date->tm_mon + 1; day = current_date->tm_mday; year = current_date->tm_year + 1900; format = 1; } // Set the date to these input parameters // if return = 0 ---> All data is valid // if return = 1 ---> Something out of range int date::set_date(int in_month, int in_day, int in_year) { int temp = 0; int max_days; // The limits on the year are purely arbitrary if (in_year < 1500) { // Check that the year is between year = 1500; // 1500 and 2200 temp = 1; } else { if (in_year > 2200) { year = 2200; temp = 1; } else year = in_year; } if(in_month < 1) { // Check that the month is between month = temp = 1; // 1 and 12 } else { if (in_month > 12) { month = 12; temp = 1; } else month = in_month; } max_days = days_this_month(); if (in_day < 1) { // Check that the day is between day = temp = 1; // 1 and max_days } else { if (in_day > max_days) { day = max_days; temp = 1; } else day = in_day; } return temp; } static char *month_string[13] = {" ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; // return Jan Feb Mar Apr etc. char *date::get_month_string(void) { return month_string[month]; } // return an ASCII-Z string depending on the stored format // format = 1 Aug 29, 1991 // format = 2 8/29/91 // format = 3 8/29/1991 // format = 4 29 Aug 1991 Military time // format = ? Anything else defaults to format 1 char *date::get_date_string(void) { switch (format) { // This printout assumes that the year will be // between 1900 and 1999 case 2 : sprintf(out_string, "%02d/%02d/%02d", month, day, year - 1900); break; case 3 : sprintf(out_string, "%02d/%02d/%04d", month, day, year); break; case 4 : sprintf(out_string, "%d %s %04d", day, month_string[month], year); break; case 1 : // Fall through to the default case default : sprintf(out_string, "%s %d, %04d", month_string[month], day, year); break; } return out_string; } int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Since this is declared in the private part of the class // header is is only available for use within the class. // It is hidden from use outside of the class. int date::days_this_month(void) { if (month != 2) return days[month]; if (year % 4) // Not leap year return 28; if (year % 100) // It is leap year return 29; if (year % 400) // Not leap year return 28; return 29; // It is leap year } |