0

ok i have this program dealing with date Class. im having trouble with the overloaded operator % which returns a date with smallest component month, day, and year for two given dates. your help is very appreciated.

For Example:

d1 is May 5 , 1942

d2 is August 1, 1970

The small components of d1 and d2 are May 1, 1942

class date
{
  private:
 
    int month, day, year;
    bool leap;  // true if a leap year
    bool is_it_leap ();

  public: 

    date ();  // constructor, creates a base date
    // sets a date to the given parameters
    date (int m, int d, int y);
    void next ();  // moves a date forward one day
    void print (); // prints the date in readable format
    bool new_year (); // true if date is January first
    // equality test for two dates
    bool equal (date a_date);
    // greater than test for two dates
    bool operator> (date a_date);
    // returns smallest components of two dates
    date operator% (date a_date);
    // friend that returns the greatest of three dates
    friend date great3 (date d1, date d2, date d3);
};

How would my date operator% (date a_date) function definition be for it to work???

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

convert both dates to integer then all you have to compare is one simple integer. You can use the mktime() function in time.h to help you out. For example, 1 Jan 2008 is converted like this:

int main()
{
    struct tm tm;
    time_t t1;
    memset(&tm,0,sizeof(struct tm));
    tm.tm_mday = 1;
    tm.tm_mon = 0; // 0 = Jan, 1 = Feb etc.
    tm.tm_year = 2008 - 1900;
    t1 = mktime(&tm);
    cout << t1 << "\n";

}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.