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
|
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;
#define highestDate {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
#define highestMonth 12;
#define defaultMonth 1;
namespace JuanGilbertoSanchez
{
//CLass declaration of class date
class date
{
//Variable declaration accesible only by the function of the class
private:
int dateMonth;
int dateDay;
int dateYear;
//Public class function accesible
public:
date();
date(int, int, int);
int getMonth() {return dateMonth;}
int getDay() {return dateDay;}
int getYear() {return dateYear;}
void display();
};
//2 functions Used by C++ programming
void static ErrorAndExit()
{
cerr << "Data is Under/over the allowed Value";
exit(EXIT_FAILURE);
}
//display the date
void date::display()
{
cout << "The date is " << dateMonth << '/' << dateDay << '/' << dateYear <<'\n';
}
//default constructor sets the month, day, and year to the current date
date::date()
{
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime( &rawtime);
dateMonth = timeinfo->tm_mon;
dateDay = timeinfo->tm_mday;
dateYear = timeinfo->tm_year;
}
// class date constructor, the array represents the maximumdate of each month,
// leap year is not considered here.
date::date(int month, int day, int year)
{
// Variable declaration
int maximumDate[] = highestDate;
int maximunMonth = highestMonth;
// Check for valid value of month
if (month > 0 && month <= maximunMonth)
dateMonth = month; //load the private variable dateyMonth
else
{
dateMonth = defaultMonth;
ErrorAndExit();
}
//check for valid value of day
if (day > 0 && day <= maximumDate[dateMonth - 1])
dateDay = day; //load the private variable dateDay
else
ErrorAndExit();
//check for valid value of year
if (year >= 0 )
dateYear = year; //load the private variable dateYear
else
ErrorAndExit();
}
}
|