Hour 15. Using Operator Overloading
Operator Overloading
The built-in types in C++ work with operators such as addition (+
) and multiplication (*
), making it easy to use these types in expressions:
int x = 17, y = 12, z;z = x * (y + 5);
The C++ compiler knows to multiply and add integers when the *
and +
operators appear in an expression. The preceding code adds 5 to y
, and then multiplies the result by x
. The z
integer is assigned the value 289.
A class could provide the same functionality with multiply()
and add()
member functions, but the ...
Get Sams Teach Yourself C++ in 24 Hours, fifth edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.