Lecture 16 Operator Overloading-Stream Insertion Extraction_+ Operator
Lecture 16 Operator Overloading-Stream Insertion Extraction_+ Operator
Programming
(SE1143)
Lecture 16
2
Overloading as a nonmember
function
When we overload a binary operator as a member function, one of
the operands needs to be the host object. This is fine when each
operand has a different role in the operation.
However, in some operators, such as (a + b), the two operands play
the same role and neither of them is related to the result. In these
cases, it is better to use a nonmember function.
C++ allows functions to be declared as friend functions of the class.
A friend function has no host object, but it is granted friendship so
that it can access the private data members and member functions of
the class without calling the public member functions.
We use friend functions to overload selected operators.
3
Binary Arithmetic Operators
4
5
6
Extraction and Insertion Operators
7
8
9
Example for Practice
10
This is all for Week 16
11