0% found this document useful (0 votes)
0 views46 pages

Lecture12 13 CPP

The document introduces operator overloading in C++, explaining that it allows operators to gain additional meaning relative to a class while retaining their original meaning. It details how to create operator functions, the restrictions on operator overloading, and the differences between overloading binary and unary operators, including the use of friend functions. Additionally, it highlights examples of overloading various operators and provides guidance for homework assignments related to operator overloading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views46 pages

Lecture12 13 CPP

The document introduces operator overloading in C++, explaining that it allows operators to gain additional meaning relative to a class while retaining their original meaning. It details how to create operator functions, the restrictions on operator overloading, and the differences between overloading binary and unary operators, including the use of friend functions. Additionally, it highlights examples of overloading various operators and provides guidance for homework assignments related to operator overloading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Introducing Operator Overloading

The basics of operator overloading


• Operator overloading resembles function
overloading.
• When an operator is overloaded, that operator loses
none of its original meaning.
• Instead, it gains additional meaning relative to the
class for which it is defined.
• To overload an operator, create an operator function.
• Most often an operator function is a member function
or a friend of the class for which it is defined.
The basics of Operator Overloading
• The general form of a member operator function is
shown here:

• The return type of an operator function is often the


class for which it is defined.
• However, an operator function is free to return any
type.
The basics of Operator Overloading
• The operator being overloaded is substituted for the
#
• For example, if the + is being overloaded, the
function name would be operator +
• The contents of arg-list vary depending upon how
the operator function is implemented and the type
of operator being overloaded.
The basics of Operator Overloading
• There are two important restrictions to remember
when you are overloading an operator.
• First, the precedence of the operator cannot be
changed.
• Second, the number of operands that an operator
takes cannot be altered.
• For example, we cannot overload the / operator so
that it takes only one operand.
The basics of Operator Overloading
• Most C++ operators can be overloaded. The only
operators that you cannot overload are: . :: .* ?
• One final point: operator functions cannot have
default arguments.
Overloading Binary Operators
• When a member operator function overloads a
binary operator, the function will have only one
parameter.
• This parameter will receive the object that is on the
right side of the operator.
• The object on the left side is the object that
generates the call to the operator function and is
passed implicitly by this.
• It is important to understand that operator function
can be written with many variations.
Overloading Binary Operators
• Example – the following program overloads the +
operator relative to the coord class. This class is
used to maintain X, Y coordinates.
Overloading Binary Operators
Overloading Binary Operators

• The following statement is also valid


Overloading Binary Operators

• The following statement is also valid


Overloading Binary Operators
• The following version of the preceding program
overloads the – and the = operators relative to the
coord class.
Overloading Binary Operators
Overloading Binary Operators
Overloading Binary Operators
Overloading Binary Operators
Overloading Binary Operators
• When the operator +( ) function was created, it did
not matter which order the operands were in.
• That is A+B is the same as B+A
• However, subtraction operation is order dependent.
• Therefore, to subtract the operand on the right from
the operand on the left.
• Because it is the left operand that generates the call
to operator –( ), the subtraction must be in this
order:
Overloading Binary Operators
• It is possible to overload an operator relative to a
class so that the operand on the right side is an
object of a built-in type, such as an integer.
Overloading Binary Operators
Overloading Binary Operators
Overloading Binary Operators
Overloading Binary Operators
• What happens when the compiler sees the
following statement?

• The overloaded operator + ( int i) function works


only when the object is on the left.
• Therefore, this statement generates compile-time
error.
Overloading the Relational and Logical Operators
• It is possible to overload the relational and logical
operators.
• They will return an integer that indicates either true
or false.
Overloading the Relational and Logical Operators
• In the following program, the == and && operators
are overloaded.
Overloading the Relational and Logical Operators
Overloading the Relational and Logical Operators
Overloading the Relational and Logical Operators
Overloading a Unary Operator
• Overloading a unary operator is similar to
overloading binary operator except that there is
only one operand to deal with.
• When we overload a unary operator using a
member function, the function has no parameter.
• Since there is only one operand, it is this operand
that generates the call to the operator function.
• There is no need for another parameter.
Overloading a Unary Operator
• Example- the following program overloads the
increment operator (++) relative to the coord class.
Overloading a Unary Operator
Overloading a Unary Operator
Overloading a Unary Operator
• Assuming the preceding program, these two
statements would have been identical

• Modern specification for C++, create two version


of the operator ++( ) functions.
• First is shown in the preceding example.
• The second is declared like this:
Overloading a Unary Operator
• The second is declared like this:

• In this case, notused will always be passed the


value 0.
Using Friend Operator Functions
• It is possible to overload an operator relative to a
class by using a friend rather than a member
function.
• As we know, a friend function does not have a this
pointer.
• In this case of a binary operator, this means that a
friend operator function is passed both operands
explicitly.
• For unary operator, the single operand is passed.
Using Friend Operator Functions
• Here operator + ( ) is overloaded for the coord class
using a friend function.
Using Friend Operator Functions
Using Friend Operator Functions
Using Friend Operator Functions
• As We know, an overloaded member operator
function,

• The solution to this problem is to make the


overloaded operator functions friends and define
both possible situations.
Using Friend Operator Functions
Using Friend Operator Functions
Using Friend Operator Functions
Using Friend Operator Functions
Using Friend Operator Functions
• If we want to use a friend operator function to
overload either the ++ or - - unary operator, we
must pass the operand to the function as a reference
parameter.
Using Friend Operator Functions
Using Friend Operator Functions
Using Friend Operator Functions
Home work:
 A CLOSER LOOK AT THE ASSIGNMENT OPERATOR
 OVERLOADING THE [ ] SUBSCRIPT OPERATOR

You might also like