Operator Overloading - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Operator Overloading

Description:

C lets you overload all operators with the ... All other operators can be implemented as member, non-member, or ... code doesn't compile because it is ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 16
Provided by: rockcree
Category:

less

Transcript and Presenter's Notes

Title: Operator Overloading


1
Operator Overloading
2
Operator Overloading allows you to define and
overload the behavior of operators for
user-defined data types. C lets you overload
all operators with the exception
of . . ? Operator Overloading can be
accomplished via member or non-member
functions. The following operators must be
overloaded as member functions () -gt All
other operators can be implemented as member,
non-member, or friend functions. Non-member
operator functions must take at least one class
argument. This prevents overloading of built-in
types.
3
Any function that performs an operation on an
object can be a member or non-member
function. class Point public Point(int x,
int y) _x(x), _y(y) int x() const
return _x int y() const return _y
Point add(const Point rhs) const private
int _x, _y Point Pointadd(const Point
rhs) const return Point(_x rhs._x, _y
rhs._y) Point add(const Point lhs, const
Point rhs) return Point(lhs.x() rhs.x(),
lhs.y() rhs.y()) int main () Point
a(3,7), b(18,2) Point r1 a.add(b) Point
r2 add(a, b)
4
We can rewrite the add functions we just wrote as
overloaded operators. Heres what theyd look
like class Point public Point
operator(const Point rhs) const Point
Pointoperator(const Point rhs) const
return Point(_x rhs._x, _y rhs._y) Point
operator(const Point lhs,
const Point rhs) return Point(lhs.x()
rhs.x(), lhs.y()
rhs.y()) int main () Point a(3,7),
b(18,2) Point r a b Which function
gets called in this case?
5
This is a trick! This code doesnt compile
because it is ambiguous. When overloading an
operator, the programmer needs to decide whether
to make that operator a member or a
non-member. For operator, it is preferred that
it be overloaded as a non-memberprimarily
because it doesnt need to be a member. When
overloading a binary operator as a member, the
overload will take a single argument
corresponding to the right hand side(rhs)
operand. When overloading a binary operator as a
non-member, the overload will need to take 2
arguments, corresponding to both the left hand
side(lhs) and right hand side(rhs) operands. Try
to overload binary operator- for Point. Should
it be a member or a non-member?
6
Unary Operators
Unary operators can also be overloaded. If they
are overloaded as a member, they will take zero
arguments. If they are overloaded as
non-members, they will take a single
argument. Lets look at unary operator- which
performs negation. // As a member class Point
public Point operator-()
const Point Pointoperator-() const
return Point(-_x, -_y) // As a
non-member Point operator-(const Point p)
return Point(-p.x(), -p.y())
7
To use this unary operator int main ()
Point a(3,7), b(18,2) Point r -a Lets
look at some other operators that we might want
to overload to perform comparisons bool
operator(const Point lhs,
const Point rhs) return ((lhs.x()
rhs.x()) (lhs.y()
rhs.y())) Notice how the operator for the
whole is defined in terms of the operator for the
components. We could do something similar to
implement other comparison operators, namely !
gt lt gt lt
8
Sometimes we even implement an overloaded
operator in terms another operator we have
overloaded. Heres one possible way to implement
operator! bool operator!(const Point lhs,
const Point rhs) return !(lhs
rhs) This implementation uses operator
that we just overloaded.
9
Mixed-Type Operators
You can use non-matching parameter types when
overloading binary operators. For example, we
may want to overload operator for the Point
class for Points as well as scalars Point
operator(const Point lhs,
const Point rhs) return Point(lhs.x()
rhs.x(), lhs.y()rhs.y()) Point
operator(const Point lhs, int num) return
Point(lhs.x()num, lhs.y()num) int main()
Point a(3,4), b(5,6) Point prod1 a b
Point prod2 b a Point prod3 a 3
Point prod4 3 a // what happens here
10
Rule of Thumb
When determining whether operators should be
implemented as member of non-member functions,
heres a few things to keep in mind Member
operators have access to the protected and
private members of the left operandthis is
necessary for all operators that actually modify
the left operands contents. Non-member
operators only have access to the public
interface of the left operand, unless they are
explicitly declared as friends. In general, use
the following rule-of0thumb as a guideline for
determining whether an operator function should
be a member or not All unary
operators member () -gt member(required)
- / member All other binary
operators non-member
11
Arithmetic Assignment Overloading
How should Arithmetic Assignment Operator
overloading differ from Arithmetic Operator
overloading? For one thing, it must be a member
since the left operand is going to be modified by
the operation. Non-assignment Arithmetic
operators do not modify their left
operand. Heres operator for Point class
Point public Point operator(const Point
rhs) Point Pointoperator(const Point
rhs) _x rhs._x _y rhs._y
return this Why is the return value by
reference? Point a(2,3), b(3,4), c(4,5) a b
c
12
Increment and Decrement Operators
Recall that the Increment (operator) and
Decrement(operator--) operators each come in 2
formsprefix and postfix. Since the
functionality is different, well need to write 2
different functions for each operator. class
Point public Point operator() //
prefix Point operator(int) //
postfix Point Pointoperator() _x
_y return this Point operator(int)
Point temp(this) _x _y return
temp
13
Which do you think is more efficient? Prefix or
Postfix? Why am I returning by reference for
Prefix and by value for Postfix?
14
Overloading ltlt and gtgt
Overloading the stream insertion and extraction
operators makes inputting and outputting
user-defined data types easy to do. These
operators can not be implemented as member
functions because there are not commutativethat
left operand must always be of a stream type.
Most commonly, these operators are overloaded as
non-member friends. class Point public
friend ostream operatorltlt(ostream os,
const Point p) ostream
operatorltlt(ostream os,
const Point p) return os ltlt '(' ltlt p.x() ltlt
',' ltlt p.y() ltlt ')'
15
Once again, why is the return value by
reference? Chaining! int main() Point
a(2,4), b(5,6) Point r1 a b cout ltlt r1
ltlt endl Point r2 a 4 cout ltlt r2 ltlt
endl Point r3 6 a cout ltlt r3
return 0
Write a Comment
User Comments (0)
About PowerShow.com