
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

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
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?
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
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
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 }