课程: Complete Guide to C++ Programming Foundations
免费学习该课程!
今天就开通帐号,24,100 门业界名师课程任您挑!
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…
内容
-
-
-
-
-
-
-
-
-
(已锁定)
Overview of classes and objects5 分钟 13 秒
-
(已锁定)
Data members3 分钟 1 秒
-
(已锁定)
Function members8 分钟 8 秒
-
(已锁定)
Constructors and destructors5 分钟 15 秒
-
(已锁定)
Using classes2 分钟 26 秒
-
(已锁定)
Header and implementation files4 分钟 23 秒
-
(已锁定)
Working with several source files7 分钟 8 秒
-
(已锁定)
Operator overload4 分钟 46 秒
-
(已锁定)
Solution: Create a person class2 分钟 22 秒
-
(已锁定)
-
-
-