Operator Overloading - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Operator Overloading

Description:

Recall our class QuadraticEq. We will try to add two QuadraticEq objects ... Class Quadratic Equation. Consider overloading addition operator for two QE objects ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 37
Provided by: akhmad2
Category:

less

Transcript and Presenter's Notes

Title: Operator Overloading


1
Operator Overloading
  • Principles of Operator overloading
  • Operator overloading implementations

2
Todays agenda
  • Do we need operator overloading?
  • Operators unary, binary and ternary
  • What operators can be overloaded?
  • What operators can not be overloaded?
  • Implementation
  • Member function
  • Non-member friend function
  • Non-member non-friend function

3
About operators
  • Every operator in C is basically a function
  • The prototype is
  • return_type operatorX(arguments)
  • X is the operator symbol (e.g., ,-,/,ltlt,new)
  • MyEq1 MyEq2
  • QuadraticEq operator(const QuadraticEq)

Operand1
Operand2
addition operator
4
Do we need operator overloading?
  • Assuming two integers a and b, we have been
    familiar with
  • ab
  • Rather than
  • a.add(b) or add(a,b)
  • What about objects of user-defined classes?
  • E.g., two QuadraticEq objects x,y
  • Can we write xy then?

5
Recall our class QuadraticEq
  • We will try to add two QuadraticEq objects x and y

6
Class declaration
7
(No Transcript)
8
What happens?
9
Operator addition does not work!
  • We will try to work this out!
  • We overload addition operator for class
    QuadraticEq

10
Unary, binary and ternary Operators
  • Unary operator operator with one operand
  • , (dereference), , --
  • E.g. x, y, x, z
  • Binary operator operator with two operands
  • , (multiplication), /,
  • E.g. ab, cd, x/y
  • Ternary operator operator with three operands
  • ? (the only ternary operator)
  • Condition? if true if false
  • (xgt0) ? x 0

11
Fundamentals
  • Operator overloading means create a function of
    existing operators for user-defined class
  • Function name convention
  • Keyword operator followed by the symbol
  • E.g., operator addition? the symbol
  • Function name operator

12
Fundamentals
  • Function prototype
  • Return_type operator( arguments )
  • The number of arguments depends on
  • whether we implement as member function or
    non-member (friend) function
  • Whether it is unary, binary or ternary operator
  • To be discussed later

13
Fundamentals
  • Operators must be overloaded explicitly
  • Overloading does not overload
  • Using operators on a class object
  • It must be overloaded for that class
  • Exceptions
  • Assignment operator,
  • Memberwise assignment between objects
  • Address operator,
  • Returns address of object
  • Both can be overloaded
  • Overloading provides concise notation
  • object2 object1.add(object2)
  • object2 object2 object1

14
Restrictions
  • Can not create new operator
  • E.g., we can not create operator or operator
  • Cannot change
  • How operators act on built-in data types
  • I.e., cannot change integer addition
  • Precedence of operator (order of evaluation)
  • Use parentheses to force order-of-operations
  • Associativity (left-to-right or right-to-left)
  • Number of operands
  • is unitary, only acts on one operand

15
Example
  • Class Fraction
  • Representing fractional number
  • E.g. 3/5, 4/7, etc
  • Overload addition operator

16
Class Fraction
17
  • Compilation ?Unsuccessful
  • Operator is not yet defined for class Fraction

18
How do we overload operator
  • Possibilities
  • Fraction Fraction
  • Fraction Integer
  • Integer Fraction
  • Operator must be overloaded for every
    possibility
  • Consider FractionInteger case
  • Implement as member function
  • Prototype
  • Fraction operator ( const int )

19
(No Transcript)
20
(No Transcript)
21
Implementation
  • We can implement operator overloading as
  • Member function
  • Non-member friend function
  • Non-member non-friend function (just like
    function in general)

22
Invoking overloaded operator
  • Adding two class objects ab
  • Member function implementation
  • a.operator(b)
  • Non-member function implementation
  • operator(a,b)
  • Both have EXACTLY the same effect

23
Implement unary operator
  • Operator with one operand, the object itself
  • Member function without argument
  • Return_type operatorX()
  • Non-member function with one argument
  • Return_type operatorX( ClassName )

  • as data type

24
Overload operator!
25
Execution Output
26
Implement Binary operator
  • Operator with two arguments
  • Possibilities
  • Class object class object
  • Member or non-member
  • Class object other object
  • Member or non-member
  • Other object class object
  • Member or non-member

27
Class object class object
  • Member function prototype
  • Return_type operatorX( Classname )
  • Non-member friend function prototype
  • friend Return_type operatorX( Classname,
    Classname)

28
(No Transcript)
29
(No Transcript)
30
(No Transcript)
31
(No Transcript)
32
Class object other object
  • Member function prototype
  • Return_type operatorX( Othertype )
  • Non-member friend function prototype
  • friend Return_type operatorX( Classname,
    Othertype)

33
Other object class object
  • Non-member friend function
  • friend Return_type operatorX( Classname,
    Classname)
  • Can not be member function
  • YHW?

34
Class Quadratic Equation
  • Consider overloading addition operator for two QE
    objects
  • Prototype as member function
  • QuadraticEq operator( const QuadraticEq )
  • Prototype as non-member friend function
  • Friend QuadraticEq operator( const
    QuadraticEq, const QuadraticEq )

Will you try it out??
35
Class Quadratic Equation
  • Consider overloading ltlt operator
  • X is the operator symbol (e.g., ,-,/,ltlt,new)
  • cout ltlt MyEq
  • ostream operatorltlt(ostream, const QuadraticEq
    )

Operand1
Operand2
Stream insertion operator
Will you try it out??
36
SuMMaRY
  • Operator overloading is allowed to make
    operations with user-defined classes concise and
    clear
  • Operator overloading can be implemented as class
    member function or non-member function
  • Both choices do not have effect on the operation
Write a Comment
User Comments (0)
About PowerShow.com