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 - C++ Tutorial
From the course: Complete Guide to C++ Programming Foundations
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
-
-
-
-
-
-
-
-
-
(Locked)
Overview of classes and objects5m 13s
-
(Locked)
Data members3m 1s
-
(Locked)
Function members8m 8s
-
(Locked)
Constructors and destructors5m 15s
-
(Locked)
Using classes2m 26s
-
(Locked)
Header and implementation files4m 23s
-
(Locked)
Working with several source files7m 8s
-
(Locked)
Operator overload4m 46s
-
(Locked)
Solution: Create a person class2m 22s
-
(Locked)
-
-
-