1 / 9

Friend Functions

Friend Functions. Recall Private Access Woes. // fraction.h ... class Fraction { ... private: int m_Numerator ; int m_Denominator ; };. //fraction.cpp ... Fraction mult_fracs (const Fraction & lhs, const Fraction & rhs ) { Fraction temp;

marcie
Télécharger la présentation

Friend Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Friend Functions

  2. Recall Private Access Woes //fraction.h ... class Fraction { ... private: intm_Numerator; intm_Denominator; }; //fraction.cpp ... Fractionmult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator; ...

  3. Recall Private Access Woes //fraction.h ... class Fraction { ... private: intm_Numerator; intm_Denominator; }; //fraction.cpp ... Fractionmult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator; ...

  4. Friend Functions A friend function is a nonmember function to a class that has been given direct access rights to the private section of the class. The keyword friend is always used inside the class definition of the class granting the access rights. Never use the word friend outside of a class definition.

  5. Friends! //fraction.h ... class Fraction { ... • friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); private: intm_Numerator; intm_Denominator; }; //fraction.cpp ... Fractionmult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator; ...

  6. Friends! //fraction.h ... class Fraction { ... • friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); private: intm_Numerator; intm_Denominator; }; //fraction.cpp ... Fractionmult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator; ...

  7. Full Function //fraction.h ... class Fraction { ... • friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); • ... }; //fraction.cpp ... Fractionmult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator; temp.m_Denominator = lhs.m_Denominator * rhs.m_Denominator; return temp; }

  8. Don’t Do This //fraction.h ... class Fraction { ... • friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); • ... }; //fraction.cpp ... friend Fractionmult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator; temp.m_Denominator = lhs.m_Denominator * rhs.m_Denominator; return temp; }

  9. End of Session

More Related