Thread: Comparing Dates?

  1. #1
    Registered User
    Join Date
    Sep 2002

    Comparing Dates?

    First of all, I would like to ask if anyone knows a decent site that explains operator overloading pretty well. It's a very confusing topic.. I can't seem to understand. And there seems to be plenty of arguments that overloading makes the program more complicated to read, which is the reason that Java doesn't have it.

    Okay, for the actual program..

    I was asked to make a program that compares two dates using operator overloading. I'm a mess right now, but I'll post the code anyway.

       class Date
          Date(int m, int d, int y); 
          void get();
          void show(); 
          bool isAfter(Date date1, Date date2); //checks if the date1 is after date2.
          bool operator == (const Date &date2); //I have no idea how to use this.. I'm suppose to see if the dates are equal to each other.
          int day,month,year;
       Date::Date() //contains default date
       Date::Date(int m, int d, int y)
       void  Date::get()
       void Date::show()
       bool Date::isAfter(Date date1, Date date2)
          if (date2.year > date1.year)
             return true;
          else if (date2.year < date1.year)
             return false;
             if (date2.month > date1.month)
                return true;
             else if (date2.month < date1.month)
                return false;
                if ( >
                   return true;
                   return false;
       bool Date::operator == (const Date &date2) //no idea.
       int main()
          Date date1, date2;
          cout<<"Enter the first date"<<endl;
          cout<<"Enter the second date"<<endl;
       return 0;
    Its very messy.. and sloppy. It doesn't even compile yet. Please, can anyone give me some helpful hints?

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Look at Date::Show(). It calls IsAfter(date1, date2), but the parameters (date1, date2) aren't defined anywhere (well, in main() but that doesn't count.). This is because the rest of it is sort of jumbled But don't worry about that; it's not too hard to fix. Here are my suggestions:

    -make IsAfter() take only 1 Date. So it should be IsAfter(Date otherDate). Then change 'date2.year' (and all the other date2) to just 'year' (or whatever the .* was). Then, change all the date1's to otherDate's. This should work, because IsAfter() is a member function of Date, so if you go date1.IsAfter(someotherdate), IsAfter will be able to access date1's member variables.

    -make Date::Show() take 1 Date as a parameter (i.e. Date otherdate - the date to compare against). Then when you call IsAfter(), pass it otherdate.

    -in main(), instead of go - do the same with

    As for the operator==, what does the == operator do normally? It compares to see if the two are equal, and if they are, then it returns true So basically, in the code you would do this:
    if((year == date2.year) && (month == date2.month) && (day ==
       return true;
    Hope this helps

    P.S. Please don't just copy and paste the code like so many people do (Don't get me wrong, I'm not saying you do). Read through it and learn from it - that's the purpose of the C board

    P.P.S. I didn't try the code; it might not work It should though, if I'm as perfect as I like to think I am
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting days between 2 dates
    By nynicue in forum C Programming
    Replies: 12
    Last Post: 02-19-2010, 10:50 AM
  2. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  3. bit vs bytes when comparing a file
    By Overworked_PhD in forum C Programming
    Replies: 6
    Last Post: 05-19-2007, 11:22 PM
  4. Using dates in Pro*C
    By clancyPC in forum C Programming
    Replies: 0
    Last Post: 08-17-2006, 06:37 AM
  5. comparing problem
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-05-2002, 06:19 AM