Join Bill Weinman for an in-depth discussion in this video Overloading operators with nonmember functions, part of C++ Essential Training.
- Operator overloading is fundamental to C++.…In the last lesson we discussed…operator overloading with member functions.…In this movie we'll look at why…and how you may sometimes use…non-member functions for operator overloading.…Here I have a working copy for rational.cpp…from chapter 4 of the exercise files.…And in the previous movie on operator overloading…we discussed overloading…before basic arithmetic operators with member functions.…There is an important circumstance…where this just doesn't work, and let's take a look at it.…
If we come down here to our main function,…and I'm just going to add a line here.…I'm just going to say a plus 14.…Now when I build and run,…you'll notice that this works fine.…I need to change this, so we can see what it is here.…See, seven over one plus 14 equals 21 over one.…And that is the result that we would expect.…If we turn this around and we say 14 plus a instead,…you notice that I get an error here.…
And I'm just going to go ahead…and I am going to change this, too,…because we are going to make this work.…
Author
Released
12/16/2014- What is C++?
- Anatomy of a C++ program
- Writing statements and expressions
- Declaring variables
- Using loops
- Defining functions
- Getting the most out of the preprocessor
- Creating classes and objects
- Undertanding data types
- Overloading operators
- Understanding inheritance
- C++ template programming and the STL
- Handling exceptions
Skill Level Intermediate
Duration
Views
Q: How can I follow the exercises on Windows without paying for Microsoft Visual Studio?
Q: I stuck in the section on setting up Visual Studio 2015 for use with the exercise files. Where is the "C/C++" section in the Property dialog?
A: There's a bug in Visual Studio 2015 which prevents the C/C++ section
from appearing in an Empty project. In order to get to this section you
will need to first add a C++ file to the project. Here's how to do this:
1. Press Shift-Alt-A to "Add an existing file" to the project.
2. Select the "hello.cpp" file from the Chap01 folder of the Exercise Files and press the "Add" button.
3. Confirm that "hello.cpp" is listed under Source Files, then
right-click on the "Working" project name and select "Properties".
4. Now you should be able to select the C/C++ section and proceed with the installation video instructions.
Related Courses
-
C Essential Training
with Isac Artzi7h 22m Beginner -
Learning C
with Dan Gookin3h 28m Beginner -
Learning C++
with Peggy Fisher2h 36m Beginner
-
Introduction
-
Welcome53s
-
-
1. Basic Syntax
-
What is C++?5m 20s
-
Introduction to basic syntax1m 57s
-
Anatomy of a C++ program9m 5s
-
Identifiers2m 14s
-
Defining variables3m 4s
-
Pointers4m 57s
-
References3m 48s
-
Arrays and strings7m 11s
-
Conditionals5m 32s
-
Looping with while and do2m 29s
-
Using stdout7m 1s
-
-
2. Defining Functions
-
Overview of C++ functions3m 15s
-
Defining a function4m 8s
-
Passing values to a function7m 52s
-
Using function pointers8m 7s
-
Overloading function names2m 39s
-
Using recursion2m 28s
-
-
3. The Preprocessor
-
About the preprocessor2m 29s
-
Using macros as constants3m 30s
-
Including files2m 31s
-
Conditional compilation1m 52s
-
Defining macros4m 2s
-
Macro caveats3m 12s
-
-
4. Classes and Objects
-
Defining a class5m 5s
-
Data members5m 15s
-
Function members6m 30s
-
Namespaces4m 8s
-
Conversion operators5m 8s
-
Creating function objects1m 55s
-
Converting numbers to words13m 45s
-
5. Data Types
-
Overview of data types2m 49s
-
Integral types9m 50s
-
Floating-point types3m 57s
-
Characters and strings2m 47s
-
Character-escape sequences2m 10s
-
Qualifiers5m 32s
-
The C++ reference type4m 59s
-
Structured data1m 56s
-
Bit fields2m 34s
-
Enumerations3m 50s
-
Unions2m 15s
-
The void type2m 26s
-
The auto type4m 20s
-
-
6. Operators
-
Common operators3m 29s
-
Logical operators2m 48s
-
Bitwise operators3m 51s
-
Operator precedence2m 1s
-
-
7. Object Inheritance
-
Simple inheritance3m 48s
-
Accessing the base class2m 26s
-
Friendship3m 23s
-
Multiple inheritance3m 33s
-
8. Templates
-
Understanding templates2m 18s
-
Template functions4m 20s
-
Template classes4m 13s
-
Type inference6m 20s
-
-
9. The Standard Library
-
File management with stdio2m 35s
-
Handling system errors2m 8s
-
Time and date functions4m 6s
-
10. The Standard Template Library (STL)
-
Overview of the STL2m 30s
-
Vectors8m 41s
-
Strings4m 38s
-
I/O streams8m 10s
-
Iterators5m 33s
-
Lists4m
-
Pairs1m 58s
-
Sets4m 29s
-
Maps3m 56s
-
Stacks and queues6m 20s
-
Handling exceptions4m 58s
-
-
Conclusion
-
Goodbye57s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Overloading operators with nonmember functions