How to compare two objects (calling object and parameter) in a class?

I am writing a Date class for the assignment, and I am having problems performing one of the functions.

This is the header file for the class.

class Date
{
public:
Date();                                  // Constructor without parameters
Date(int m, int d, int y); // Constructor with parameters.

// accessors
int GetMonth();               // returns the size of the diamond
int GetDay();
int GetYear();

// mutators
bool Set(int m, int d, int y);
bool SetFormat(char f);

// standard input and output routines
void Input();             
void Show();              
void Increment(int numDays = 1);                 
int Compare(const Date& d);     

private:
int month,                    // month variables
    day,                 // day variable
    year;               // year variable
char format;
};

The member function I'm trying to do is the int Compare (const Date & d) function. I need this function to compare two Date objects (the calling object and the parameter), and should return: -1 if the calling object comes first chronologically, 0 if the objects are the same date, and 1 if the parameter object comes first in chronological order.

I tried to make a simple if statement with the == operator, but I am getting errors.

  if (d1 == d2)
     cout << "The dates are the same";
     return (0);

After creating the objects, the function should be called like this: d1.Compare (d2)

Thank you in advance!

+5
7
int Date :: Compare (const Date& d) {

   if (year<d.year) {
      return -1;
   }
   else if (year>d.year) {
      return 1;
   }
   else if (month<d.month) {
      return -1;
   }
   else if (month>d.month) {
      return 1;
   }
   // same for day

   return 0;
}

, ( ):

bool operator == (const Date& d) const {
   return !Compare(d);
}

bool operator < (const Date& d) const {
  return Compare(d)<0;   
}

... // consider using boost::operators

PS: Compare() - . , .

+10

, Compare, , :

int Date::Compare(const Date& d) const {
  return
    (year < d.year)   ? -1 :
    (year > d.year)   ?  1 :
    (month < d.month) ? -1 :
    (month > d.month) ?  1 :
    (day < d.day)     ? -1 :
    (day > d.day)     ?  1 :
                         0;
}

, :

template<typename T>
int Compare(T a, T b) {
    if (a < b) return -1;
    if (b < a) return 1;
    return 0;
}

int Date::Compare(const Date& d) const {
    int a = Compare(year, d.year);
    if (a == 0) a = Compare(month, d.month);
    if (a == 0) a = Compare(day, d.day);
    return a;
}

operator== Compare, , , operator==, , . , operator==, , , , false, Compare . , , , .

, , ++ operator< , , operator== operator>, "--". - , , . Java - -.

+7

public

bool operator==(const Date& rhs) const {
    return
       year == rhs.year
       && month == rhs.month
       && day == rhs.day
    ;
}
+5

, .. , (, , format - ).

, ++ : operator ==, , Compare.

, :

  if (d1 == d2)
     cout << "The dates are the same";
     return (0);

, cout . return , .

+4

++ || :

static inline int cmp(int a, int b)
{
  return a < b ? -1 : a == b ? 0 : 1;
}

int Date::Compare(const Date& d)
{
  int result;
  (result = cmp(year, d.year))     ||
    (result = cmp(month, d.month)) ||
      (result = cmp(day, d.day));

  return result;
}
+4

== , . , Compare - const:

class Date
{
...
int Compare(const Date& d) const;     

bool operator==(const Date& rhs) const
{
    return 0 == Compare(rhs);
}
+1
source

You cannot do d1 === d2 because I believe that it compares memory addresses (did not do C ++ at that time).

What you need to do is write a function that will compare each member of your Date class and return a negative number, 0 or a positive number. Negative means less, 0 means the same thing, and positive means more.

For example, in Java:

public int compareTo(Date date) {
  int returnValue = 0;

   returnValue = this.getYear() - date.getYear();

   if(returnValue == 0) {
      returnValue = this.getMonth() - date.getMonth();

      if(returnValue == 0) {
         returnValue = this.getDay() - date.getDay();
      }
   }
}
0
source

All Articles