From the course: Complete Guide to C++ Programming Foundations

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Operator overload

Operator overload

- [Instructor] I have mentioned that in C++, not only can you overload functions, but also operators. In this exercise, we will take some of our Inventory class functions and upgrade them to operators that make sense. In the header file, let's look at the prototypes of addItem in line 23, removeItem in line 26 and getItem in line 29. Let's change these functions to operators. First of all, operators can be urinary, binary, or ternary, meaning that they operate on one, two, or three operands respectively. These three operations in our class are binary because they operate on the object they are members of and the only argument they take. Second, all operators must return a value, as opposed to functions which may return void. So let's go to line 23 and upgrade the addItem function. This one will be replaced by the compound assignment operator +=. The syntax to overload an operator goes like this. First, we specify the return type. This cannot be void because this operator works with a…

Contents