class date problem

1) how can i validate the months , days and year together with the subclass earlier, if the year same...nothing provided.,. no result.pls show example...
2)i really have no idea on counting the next christmas day by providing a specified date on my christmas subclass.pls show me some examples..
3)how to count the different between the two dates....by using the below class... any clues...will be appreciate

#include
#include
#include

class Date
{ //data
public:
Date(int =1,int=1,int=1900);//default constructor



~Date();

protected:
int month;
int day;
int year;
int checkDay(int);


//data

};

///////////////////////////////////////////////////////////////////////////

// Date constructor with range checking
Date::Date(int m, int d, int y)

{
month= (m > 0 && m<=12)? m:1;
day= checkDay(d);
year=(y>=1900 && y <2100)? y:1900;
}

//utility function to confirm proper day value
//based on month and year
int Date::checkDay(int testDay)
{
static int daysPerMonth[13]={0,31,28,31,30,
31,30,31,31,30,
31,30,31};
if(month !=2)
{
if(testDay > 0 && testDay <= daysPerMonth[month])
return testDay;
}

else //February: Check for possible leap year
{
int days= (year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0) ? 29:28);

if(testDay > 0 && testDay <= days)
return testDay;
}


return 1;
}



Date::~Date()
{


}















//A method that takes two dates as arguments
//and return true(or 1)provided the first date
//occurs before the second date else return false(or 0)

//class earlier:public Date

class Earlier:public Date
{
public:

Earlier(int month=1,int year=1900,int day=1,int mm=1,int dd=1,int yy=1900);//default constructor
compare() ;
void Eprint() const;


protected:
int Emonth;
int Eday;
int Eyear;
int EcheckDay(int EtestDay);

};

Earlier::Earlier(int mm, int dd, int yy, int month, int day,int year):Date(month,day,year)
{
Emonth= (mm > 0 && mm<=12)? mm:1;
Eday=EcheckDay(dd);
Eyear=(yy>=1900 && yy <2500)? yy:1900;
}

int Earlier::EcheckDay(int EtestDay)
{
static int EdaysPerMonth[13]={0,31,28,31,30,
31,30,31,31,30,
31,30,31};
if(Emonth !=2)
{
if(EtestDay > 0 && EtestDay <= EdaysPerMonth[Emonth])
return EtestDay;
}

else //February: Check for possible leap year
{
int Edays= (Eyear % 400 == 0 ||
(Eyear % 4 == 0 && Eyear % 100 != 0) ? 29:28);

if(EtestDay > 0 && EtestDay <= Edays)
return EtestDay;
}


return 1;
}


Earlier::compare()
{

if(year<Eyear )
cout<<"First date is before second date.";
else if(year>Eyear )
cout<<"First date is after second date.";



}


//Display print function
void Earlier::Eprint() const
{
cout<<"Date 1: "<<month<<" "<<day<<" "<<year<<"
Date 2: "<<Emonth<<" "<< Eday<<" " << Eyear<<"

";
}













//A method Christmas which given a date, should return
//the number of days until the next Christmas day(December,25)


//class christmas:public Date


class Christmas:public Earlier
{
public:
Christmas(int mm, int dd, int yy, int month, int day,int year):Date(month,day,year);

adddays();
void Cprint() const;

private:
void Ccount();
};

void Christmas::Ccount()
{
if(month==2&& day<29&&year % 4==0&&
(year % 400 ==0 || year % 100 !=0))
++day;
else if (day< Date::daysPerMonth[month])
++day;
else if(month<12) // not last day of month
{
++month;
day=1;
}

else //December
{
month=1;
day=1;
++year;
}
}




void Christmas::Cprint()const
{
}











//Given two date,return no. of days between the given two dates.
//For e.g. daysBetween(Date(12,4,1970),Date(22,4,1970) is 10.
//The program should cope with dates having different months.

//class Compare:public Date
//////////////////////////////////////////////////////////////////
main()
{


Earlier d1(8,1,1986,7,27,1982);
d1.Eprint();
d1.compare();

return 0;
}


Comments

  • use the functions in time.h and your life will get a lot simpler. Here is just one example of to calculate the number of days from today to next Christmas.

    [code]
    #incude

    int main()
    {
    // get today's date as an integer
    time_t now = time(0);
    // convert the date to a structure
    struct tm* tm;
    tm = localtim(&now);
    // set the structure to Dec 25th of the current year
    // or next year if today is between Dec 26th and Jan 1st
    tm->tm_mday = 25;
    tm->tm_mon = 11;
    if(tm-tm_yday > 360)
    {
    tm->tm_year++; // next year
    }

    time_t xmas = mktime(tm);

    time_t diff = xmax - now;
    cout << "Number of seconds until next Christmas: " << diff << endl;
    // since there are (60*60*24) (seconds * minutes * hours) seconds in
    // one day, the number of days is:
    cout << "Number of days: " << diff/(60*60*24) << endl;
    }

    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion