View Single Post
Old 03-21-2004, 07:45 AM   #2 (permalink)
ten_d
Registered User
 
Join Date: Mar 2004
Posts: 2
ten_d is on a distinguished road
This may help a bit. Here's my class so far.

//Date Class definition
#ifndef DATE1_H
#define DATE1_H
//#include <iostream>

class Date {

public:
Date( int = 1, int = 1, int = 1990); //defult constructor
void print() const; //print date in month/day/year format
void printJulian() const; //print in julian format ddd/yyyy
~Date();

private:
int month;
int day;
int year;

int checkDay(int) const;

}; //end of class Date
#endif

Date::Date(int mn, int dy, int yr)
{
cout << "Enter the month: ";
cin >> mn;
cout << "Enter the day: ";
cin >> dy;
cout << "Enter the year: ";
cin >> yr;

if (mn > 0 && mn <= 12)
month = mn;

else {
month = 1;
cout << "Month " << mn << " is invalid. Month is set to 1.\n";
}

year = yr;
day = checkDay(dy);

} //end Date constructor



//print Date object in form month/day/year
void Date::print() const
{
cout << "The day is " << month << '/' << day << '/' << year << endl;
} //end of function print

//output Date object to show when its destructor is called
Date::~Date()
{

} //end destructor ~Date



//utility function to confirm proper day value based on month and year, handles leap years too
int Date::checkDay(int testDay) const
{
static const int daysPerMonth[13] =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//determine whether testDay is valid for specified month
if (testDay > 0 && testDay <= daysPerMonth [month])
return testDay;

//Feb 29 check
if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
return testDay;

cout << "Day " << testDay << " is invalid. Date set to 1.\n";

return 1;
} //end of function checkDay



void Date::printJulian() const
{
static const int daysPerMonth[13] =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int julDate = day;

for (int counter =0; counter < month; counter++)
{
julDate = julDate + daysPerMonth[counter];
}

if ((year % 400 == 0 || (year % 4 ==0 && year % 100 != 0)) && (month >= 2 && day > 28))
julDate++;

cout << "The date in Julian style ddd/yyyy is " << julDate << '/' << year << endl;
} //end of printJulian function
ten_d is offline   Reply With Quote