Operator Overloading

First let's start with an example. Remember how + works with strings? '+' is called an operator. Honestly, this is pretty much same thing as a function. It accepts two parameters (string1 and string2), and returns a string. We're going to learn how to write our own operator functions. The syntax will be different than normal functions, but the background idea of inputting parameters and returning something is the same.

To see a full list of operators and find out which ones are overloadable, check out this Wikipedia link. There are quite a few! This link is also pretty helpful, because it tells you what the prototype of your operator function looks like. In particular, the prototype is different depending on if your operator is a member function of a class, or just a regular function.

A regular operator function (not a member of a class)

We're going to start out with a straightforward example. We'll write a '+' operator that works for vector<int>. The idea will be the same as for strings. It will input two vectors, and return the concatenation. Below is the finished code.


Adding some operators to the Date class

Now we're going to add some operators to our Date class that we just built.

First example: Overloading the '==' operator

We're going to overload the '==' operator so that it works with Date objects. In other words, we're going to write some code that will handle the expression

      date1 == date2 

where date1 and date2 are both of type Date. Below is the finished version.



Here's how this function works. If I write

      firstDate == secondDate

It will look inside the firstDate object, and will run the operator== function where the input variable date2 is the same as secondDate. This is because firstDate is on the left-hand side of the == sign, and secondDate is on the right.

Some tricky questions:

  1. When defining the operator== function, how do I refer to the 'day' variable from the secondDate (the Date object on the right hand side of the ==)?
  2. How do I refer to the 'day' variable from the firstDate (the Date of the left hand side)?

Some more examples: < and >= for the Date class

Next we're going to overload the < operator. Our code will handle expressions of the form

    date1 < date2 

Let's say that this is true when date2 is further in the future than date1. For example, 5/8/1990 < 4/2/2000.

For the >= operator, we should be able to use our previous code to quickly get a solution. We already can calculate (date1 == date2) and (date1 < date2), so we should be able to combine these to get one-line definition for >=

However, to do this we're going to have to learn one more bit of notation. Let's say my two date objects are called date1 and date2. If I run the code

    date1 >= date2

then I know that date2 is the input parameter to the function. But how do I refer to date1? When working inside of main, this object is called date1. But my >= function runs from inside of date1, and this is a different scope. Calling it date1 here would be an error.

The answer is *this. In member functions of classes, if you want to refer to the object that you are currently working inside, use *this.

Finished Code:

If we have time: Overloading the '++' operator

We'll define the ++ operator for our Date class. It'll have the same effect as the incrementDate() function that talked about above.

The ++ operator is a little more complicated than the operators we dealt with above. One problem is that i++ (called the postfix increment operator) and ++i (called the prefix increment operator) represent different things. This means they must have different prototypes (they can't both be operator++()). c++ fixes this issue by adding a "dummy variable" to the prototype for i++. This is an extra integer in the list of parameters that is completely unused. It's kind of an ugly solution, but that's the way it is.

So in order to write a member function of Date that handles


We are required to use the prototype

Date operator++(int x)

See the table of overloadable operators again to check out the difference between the prefix and postfix increments.

Once we're all okay with the prototype, we can write the function. But be careful: this operator has return type Date. So at the end of your function you must return a Date object (it should be the edited Date).


Here is the final version of the Date class. This has all the above stuff.