n-2r7
n-2r7

Reputation: 331

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

I am writing a "Date" class for an assignment and I am having trouble doing one the 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 am trying to make 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 chronologically.

I have tried doing a simple if statement with the == operator but I get errors.

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

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

Thank you in advance!

Upvotes: 10

Views: 120508

Answers (8)

27P
27P

Reputation: 1475

class Temp {

public:
       Temp(const char* str) {
         this->str = str;
         printf("Temp [%s] is created\n", this->str);
       }

       ~Temp() {
         printf("Temp [%s] is deleted\n", this->str);
       }

       bool operator == (const Temp& a) const {
         return this->str == a.str;
       }

private:
       const char* str = new char[10];
}

int main() {

   Temp test1{"Hello1"};
   Temp test2{"Hello2"};
   (test1 == test2) ? printf("Equals") : printf("Non equals");

   return 0;
}

Upvotes: 0

Greg Bacon
Greg Bacon

Reputation: 139531

The semantics of C++'s || make this a little cluttered:

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;
}

Upvotes: 4

Steve Jessop
Steve Jessop

Reputation: 279265

Here's how I might implement your Compare function, although the format takes a moment to get used to:

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;
}

Or perhaps:

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;
}

I wouldn't use operator== in Compare, although the answers telling you how to implement operator== are fine if you want that as well. The reason is that operator== is clearly going to have to look at the same fields compare does, and if it returns false then Compare will do very similar work again. Efficiency probably isn't an issue, but it duplicates the logic.

And for what it's worth, idiomatic C++ is to implement operator< and possibly also a consistent operator== and operator>, rather than an all-in-one Compare function. The operators are what the standard algorithms use for searching and sorting, and everything else follows. Java chose to do things differently.

Upvotes: 8

Eli Bendersky
Eli Bendersky

Reputation: 273536

Compare object by contents, i.e. in your case the dates are equal of the day, month and year are equal (and perhaps format - depending on your semantics).

Also, C++ already includes a great facility for object comparison: operator == which allows writing clearer code than calling a Compare method.

By the way, take care with this:

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

If the condition is true, the cout line will be executed. The return will be executed even if the condition is false.

Upvotes: 4

Alexander Gessler
Alexander Gessler

Reputation: 46617

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;
}

Usually, you'lll also want to provide overloaded comparison operators, for example (also within the class definition):

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

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

... // consider using boost::operators

PS: There are smarter implementations of Compare() - just check the other answers. This one is pretty straightforward and readable, but conforms exactly to your specification.

Upvotes: 15

John Dibling
John Dibling

Reputation: 101456

In order to use operator== for user-defined types, you must implement it. In addition, your Compare function should be marked as a const member function:

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

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

Upvotes: 3

Vivin Paliath
Vivin Paliath

Reputation: 95528

You can't do d1 === d2, because I believe it compares the memory addresses (haven't done C++ in a while).

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

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();
      }
   }
}

Upvotes: 0

Notinlist
Notinlist

Reputation: 16640

into the class's public area

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

Upvotes: 8

Related Questions