Following the error messages, and checking the documentation, none of this was too difficult.
#include <iostream>
using namespace std;
class date {
protected:
int month; int day; int year; static char out_string[25]; static int format;
int days_this_month(void);
public:
date(void);
int set_date(int in_month, int in_day, int in_year);
int get_month(void) { return month; };
int get_day(void) { return day; };
int get_year(void) { return year; };
void set_date_format(int format_in) { format = format_in; };
char* get_date_string(void);
const char* get_month_string(void); };
class time_of_day
{
protected:
int hour; int minute; int second; static int format; static char out_string[25];
public:
time_of_day(void);
time_of_day(int H) { hour = H; minute = 0; second = 0; };
time_of_day(int H, int M) { hour = H; minute = M; second = 0; };
time_of_day(int H, int M, int S) {
hour = H;
minute = M; second = S;
};
int set_time(void);
int set_time(int hour_in);
int set_time(int hour_in, int minute_in);
int set_time(int hour_in, int minute_in, int second_in);
void set_time_format(int format_in) { format = format_in; };
char* get_time_string(void);
};
int date::format; char date::out_string[25];
date::date(void)
{
time_t time_date;
struct tm current_date;
time_date = time(NULL); errno_t terror = localtime_s(¤t_date, &time_date); month = current_date.tm_mon + 1;
day = current_date.tm_mday;
year = current_date.tm_year + 1900;
format = 1;
}
int date::set_date(int in_month, int in_day, int in_year)
{
int temp = 0;
int max_days;
if (in_year < 1500) {
year = 1500; temp = 1;
}
else
{
if (in_year > 2200)
{
year = 2200;
temp = 1;
}
else
{
year = in_year;
}
}
if (in_month < 1) { month = temp = 1;
}
else
{
if (in_month > 12)
{
month = 12;
temp = 1;
}
else
{
month = in_month;
}
}
max_days = days_this_month();
if (in_day < 1) { day = temp = 1;
}
else
{
if (in_day > max_days)
{
day = max_days;
temp = 1;
}
else
{
day = in_day;
}
}
return temp;
}
static const char* month_string[13] = { " ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
const char* date::get_month_string(void) {
return month_string[month];
}
char* date::get_date_string(void)
{
switch (format)
{
case 2: sprintf_s(out_string, "%02d/%02d/%02d", month, day, year - 1900);
break;
case 3: sprintf_s(out_string, "%02d/%02d/%04d",
month, day, year);
break;
case 4: sprintf_s(out_string, "%d %s %04d",
day, month_string[month], year);
break;
case 1: default: sprintf_s(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 };
int date::days_this_month(void)
{
if (month != 2)
return days[month];
if (year % 4) return 28;
if (year % 100) return 29;
if (year % 400) return 28;
return 29; }
int main(void)
{
date today, birthday;
birthday.set_date(7, 21, 1960);
cout << "Limited test of the date class\n";
cout << "Today is " << today.get_date_string() << "\n";
cout << "Birthday is " << birthday.get_date_string() << "\n";
today.set_date_format(4);
cout << "Today is " << today.get_date_string() << "\n";
cout << "Birthday is " << birthday.get_date_string() << "\n";
return 0;
}