Operator overloading - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Operator overloading

Description:

Class time. Can we write something like this? bool equal(const Date& date1,const Date ... Can we make it a class member? ... what shall we do? Class time (cont. ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 7
Provided by: denebC
Category:

less

Transcript and Presenter's Notes

Title: Operator overloading


1
Operator overloading
2
Class time
  • Class date represents the date of year
  • class Date
  • public
  • ???
  • private
  • int month
  • int day
  • int year
  • what would you write if you need to compare two
    dates for equality?
  • Use accessor functions
  • bool equal(const Date date1, const Date date2)
  • return(
  • date1.getyear() date2.getyear()
  • date1.getmonth() date2.getmonth()
  • date1.getday() date2.getday()
  • )
  • looks like accessors are not helping

3
Class time (cont.)
  • Can we write something like this?
  • bool equal(const Date date1,const Date date2)
  • return(
  • date1.year date2.year
  • date1.month date2.year
  • date1.day date2.day
  • )
  • we cannot because year, month and day are private
    variables of objects and equal is not a class
    member.
  • Can we make it a class member?
  • We cannot if such function is called as follows
    date1.equal(date2) then it can access the private
    vars of date1 but not date2
  • what shall we do?

4
Friend functions
  • To allow non-member function to access private
    class members it should be decared as friend
    function
  • class Date
  • public
  • // compares two dates
  • friend bool equal(const Date, const Date)
  • private
  • int month
  • int day
  • int year
  • note that a friend-function is not a class member
    - its definition should not be preceded with
    class qualifier (Date)
  • no separate prototype (outside class definition)
    is necessary for friend
  • equal is invoked as non-member function - without
    dot operator
  • if(equal(date1, date2)) ...
  • Note, that friend-functions circumvent
    information hiding and should be used as rare as
    possible and with great care
  • use member function if the task to be done
    involves only one object
  • only use friend-function if the task requires
    access to private members of more than one object
    and the objects are used symmetrically
  • you can almost always use accessors instead of
    friends

5
Operator overloading
  • We wish we could write
  • if(date1 date2)) ...
  • Would this code work?
  • It will not by default because equality
    comparison is defined only for basic data types
    not for objects
  • one could define equality comparison for objects
    using operator overloading - a principle where an
    operator is defined to accept arguments that
    differ from ones defined previously.
  • Friend functions can be used for operator
    overloading
  • friend bool operator(const Date, const Date)
  • overloaded operator can do arbitrary actions.
    Stick to intuitive meaning of operator
  • most operators can be overloaded no new
    operators can be created
  • cannot change the number of arguments operator
    takes
  • assignment operator can be overloaded but the
    principle differs will discuss later

6
Overloading arithmetic, I/O
  • Assume we need a class to store US currency
  • class Money
  • public
  • ???
  • private
  • int all_cents
  • Binary arithmetic operators can be overloaded
  • friend Money operator -(const Money, const
    Money)
  • Unary operators can also be overloaded
  • friend Money operator -(const Money)
  • note that overloading binary does not
    automatically overload
  • I/O operators (ltlt and gtgt) can be overloaded
  • to be able to stack the I/O operators they need
    to return a reference () to stream (more on
    references in later lectures)
  • friend ostream operatorltlt(ostream os, const
    Money)
  • os ltlt all_cents // should output dollars
  • return os // note the return value
Write a Comment
User Comments (0)
About PowerShow.com