Docsity

Operator Overloading - Lecture Slides | CS 23021, Study notes of Computer Science

Toggle sidebar
bg1
Operator overloading
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 fu nctions:
bool equal(const Date& date1, const Date& date2){
return(
date1.gety ear() == dat e2.getyear() &&
date1.getm onth() == da te2.getmonth () &&
date1.getd ay() == date2.ge tday()
);
}
looks like accessors a re not helping
Class time
Can we write somethi ng 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 y ear, month and day are private variables of
objects and equ al is not a class mem ber.
Can we make it a clas s member?
We cannot: if such fun ction is called as follows
date1.equal(date2) then it can access th e private vars of
date1 but not d ate2
what shall we do?
Class time (cont.)
To allow non-membe r function to access private class m embers it should
be decared as friend function:
class Date {
public:
// compares t wo dates
friend bo ol equal(const D ate&, const Date &);
private:
int month;
int day;
int year;
};
note that a friend-func tion is not a class member - it’ s definition should not
be preceded with clas s qualifier (Date::)
no separate prototype (outside class definition) is neces sary for friend
equal is invoked as n on-member function - without dot operator:
if(equal(date1, date2)) { ...
Note, that friend-funct ions circumvent information hiding and should be
used as rare as possi ble and with great care
use member function if the task to be done involves onl y one object
only use friend-functio n if the task requires access to pr ivate members
of more than one obje ct and the objects are used symm etrically
you can almost alway s use accessors instead of friends
Friend functions
We wish we could wri te:
if(date1 == date 2)) { ...
Would this code work ?
It will not by default be cause equality comparison is def ined only for basic
data types not for obje cts
one could define equa lity comparison for objects using o perator
overloading - a princip le where an operator is defined to accept
arguments that differ from ones defined previously.
Friend functions can b e used for operator overloading:
friend bool operator==(const Date&, const Date&);
overloaded operator c an do arbitrary actions. Stick to in tuitive meaning of
operator
most operators can b e overloaded; no new operators ca n be created
cannot change the nu mber of arguments operator takes
assignment operator can be overloaded but the principl e differs; will
discuss later
Operator overloading
Assume we need a cl ass to store US currency:
class Money {
public:
???
private:
int all_cents;
};
Binary arithmetic ope rators can be overloaded:
friend Money operator -(const Money&, const Money&);
Unary operators can a lso be overloaded:
friend Money operator -(const Money&);
note that overloading binary “+ “does no t automatically overload “+=“
I/O operators (<< and >>) can be overloaded
to be able to stack the I/O operators they need to return a reference (&) to
stream (more on refer ences in later lectures)
friend ostre am& operator<<(o stream& os, c onst Money&){
os << al l_cents // shoul d output dollars
return os; // note the r eturn value
}
Overloading arithmetic, I/O

Partial preview of the text

Download Operator Overloading - Lecture Slides | CS 23021 and more Study notes Computer Science in PDF only on Docsity!

Operator overloading

 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

Class time

 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 date  what shall we do?

Class time (cont.)

 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 - it be preceded with class qualifier (Date::) ’s definition should not  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

Friend functions

 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

Operator overloading

 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 (<< and >>) 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& operator<<(ostream& os, const Money&){ os << all_cents // should output dollars return os; // note the return value }

Overloading arithmetic, I/O