OOP Compare Member Function

I have an assignment in my OOP c++ class and I had to create a class called date and one of the member functions is a compare function that compares two dates that are taken in. It is suppose to be something like this:

Date d1(12,25,2003); // Dec 25, 2003
Date d2(5,18,2002); // May 18, 2002

d1.Compare(d2); // returns 1 (since d2 comes first)
d2.Compare(d1); // returns -1 (calling object is d2, comes first)

Then if d1 and d2 are equal then it returns 0.

This is what he gave us to start with the function:

int Date::Compare(const Date& d)
{

}
I am just confused on where to start and how exactly I do this. The rest of my class is completely correct, I am just not sure where to start with this. Any help would be awesome. Thank you


This is a pretty straightforward problem. In fact I'm sure you already know how to do it, you just have to figure out how to translate your thoughts to code.

1
2
3
4
Date d1(12,25,2003); // Dec 25, 2003
Date d2(5,18,2002); // May 18, 2002

d1.Compare(d2); // returns 1 (since d2 comes first) 


You say that call should return 1 since d2 comes first... but how do you know that d2 comes first?

What about the dates 12,25,2003 and 5,18,2002 indicates that the latter one comes first?
Well I know you have to compare the years first. Then if the years are the same you compare the months, then if the months and years are the same then compare the days. I am just confused because I dont know where to start, and I haven't had much practice passing by reference
Well I know you have to compare the years first. Then if the years are the same you compare the months, then if the months and years are the same then compare the days.


Yup. You just outlined all the steps necessary to write this function. Good job.

So start with step 1: How do you compare 2 years? What is the code for that?

I haven't had much practice passing by reference


Passing by reference has little to do with it. That part has already been written for you.

Inside your Compare function you have 2 objects: the 'this' object and the 'd' object:

1
2
3
4
5
int Date::Compare(const Date& d)
{
    cout << year;  // <- prints 'this' year
    cout << d.year;  // <- prints 'd's year
}


These are the two objects you are comparing.
Thank you!

Inside your Compare function you have 2 objects: the 'this' object and the 'd' object:


where did the "this" object come from? I don't really understand that part.
and so i compare year to d.year and month to d.month and day to d.day?
where did the "this" object come from? I don't really understand that part.


Every non-static member function has a hidden 'this' object. It is the object on which the member function was called:

1
2
3
4
5
6
7
8
9
10
11
// two dates
Date foo;
Date bar;

// compare the two dates with the Compare function:
foo.Compare(bar);

// bar is passed as the parameter, so it will become 'd' inside the Compare function

// the Compare function was called on the 'foo' object (ie, 'foo' is to the left of the dot)
//   so 'foo' will become the 'this' object inside of the Compare function. 


and so i compare year to d.year and month to d.month and day to d.day?


Yes.
Ok I am starting to see what you are saying a little bit!

So my code will basically be like:

1
2
3
4
5
if(year < d.year)
    return 1;
else if (year == d.year && month < d.month)
    return 1; 
//etc, etc. 


So now my final question would be how how do I know which one is comparing what? Like how do I know to return 1 for the greater one and like -1 for the other one. Does it matter what order I put them in as long as I stay consistent? So my next piece of code could be like:

1
2
3
4
5
if(year > d.year)
    return -1;
else if(year == d.year && month > d.month)
    return -1;
//etc, etc. 

??
well actually I wouldn't even write my return -1 cases because I would do all my return 1 statements then my return 0 statement then just say:
else
return -1;

but same general idea, right?
So now my final question would be how how do I know which one is comparing what? Like how do I know to return 1 for the greater one and like -1 for the other one. Does it matter what order I put them in as long as I stay consistent?


It does matter. Take another look at the example the assignment gives you:

d1.Compare(d2); // returns 1 (since d2 comes first)

Remember that in this call, d2==d and d1==this. Because d2 is in the parenthesis, and d1 is left of the dot.

This means you'd want to return 1 if the param comes before 'this'
And -1 if 'this' comes before the param.


So my next piece of code could be like:


Your final code will look something like that. Just be sure to thoroughly test it and make sure you get the proper results for all the cases you can think of.

but same general idea, right?


Hint: return 0 should probably be your 'else' here, since that is the hardest condition to meet and therefore should be the last one ruled out.
awesome you really helped me out and got me thinking. I remember briefly in class talking about a "this" object, but it was a few lectures after he released this assignment so I figured it did not apply to this one.

But if you could also get me started with last and final function of my class that would be GREAT! it's a function that increments.

He gave it to us like this to start out:

1
2
3
4
void date::increment(int numDays = 1)
{

}


so when you call it you pass in the number of days you would like to increment it then it has to add on those amount of days.

so,

1
2
3
4
5
Date d1(10, 31, 1998);	// Oct 31, 1998
Date d2(6, 29, 1950);		// June 29, 1950

d1.Increment();		// d1 is now Nov 1, 1998
d2.Increment(5);		// d2 is now July 4, 1950 


and if nothing is passed in the parameter then you automatically increment by just 1. I know it has to start out using a for loop, I am just not sure where to go next
Well again... it comes back down to a simple logic problem. Just ask yourself how you would solve this problem in your own brain... and break it down into small, simple steps.

So the question to ask is:
1
2
Date d2(6, 29, 1950);		// June 29, 1950
d2.Increment(5);		// d2 is now July 4, 1950 


How do you know that d2 will be July 4th after incrementing it by 5?



EDIT:

To clarify, "what code do I need to solve this logic problem" is the wrong approach. The approach I'm trying to push on you is: "What are the steps involved to solve this logic problem? Then, what is the code for each of those steps?"

A lot of programming is learning to break large problems into smaller parts. Once the parts are small enough, the code needed to make them happen becomes obvious.
Last edited on
1
2
3
4
5
6
7
int date::compare( const date& that ) const
{
    const int a = year*10000 + month*100 + day ;
    const int b = that.year*10000 + that.month*100 + that.day ;
    
    return (a>b) - (b>a) ;
}
Topic archived. No new replies allowed.