Thread: class date problem.....

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    5

    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<iostream.h>
    #include<string.h>
    #include<stdio.h>

    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:ate(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()
    {


    }

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    i would recomed you overload the operators such as +,- etc to add, subtract dates etc etc.. thoug this reply is not specific to your question...

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    7

    Re: class date problem.....

    Please use the code tags.

    Originally posted by Forever82
    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
    1) I'm not sure what you're trying to ask. It seems like you've got the idea of validating the month and date data already.

    2) Be more specific. Do you want to know the date of the next Christmas? Or how many days until the next one?

    3) I'll be general with my answer. To find the difference between two dates, I know of two methods. Compute the number of days from an "absolute zero" of both dates, and subtract. Compute just the amount between the two dates. I prefer the first because in most cases, the numbers won't over run an int. And it's just so much simpler to code. To compute the difference by finding the gaps, you take a sum of a bunch of "pieces". For example, find the number of days until the end of date1's month (assuming date2 is in a different month), then find the number of days until the end of date1's year (assuming date 2 is in a different year), find the number of days in the years between date1 and 2 (if any), find the number of days up to the start of date2's month, add the the date of the month of date2.

    There may be other ways, but I've never had any real problems with the first method so I never looked into it much. I also find using the first method a much easier way to take into account the weird rules about leap years.

    ~SK

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    5

    reply to unsolved problem

    1) what i am trying to say is, with my function earlier..... yes.... i can differentiate the two dates before and after...but just by putting the comparison depending on the variable of year<Eyear... like this i can't check if the date is in same year and it can't verify the month and day....i have try by putting
    if(year>=year && month<Emonth && day<Eday)
    cout<<"before";
    else
    cout<<"after";
    but cannot work.....

    2)what should i declare inside the Christmas function to count a e.g date1 is 12 26 1970 (count howmany days until the next christmas )

    3)is it a must that i use overloaded operators to count in class by inheritance..??? By using my method....can i still count the different and add ...... if can......pls show me how......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Membership Problem
    By josephjah in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2007, 01:48 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Simple class problem.
    By Great Satchmo in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2004, 10:29 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM