1 / 7

Friend Functions and Friend Classes

Friend Functions and Friend Classes. Friend to a Class. In some instances the condition of information hiding needs to be relaxed. Using friend mechanism nonmembers of the class can access nonpublic members of class.

milla
Download Presentation

Friend Functions and Friend Classes

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 andFriend Classes

  2. Friend to a Class • In some instances the condition of information hiding needs to be relaxed. • Using friend mechanism nonmembers of the class canaccess nonpublic members of class. • Declaring friend inside the class definition “who can access my private implementation?” • class controls which code has access to its members. • Can declare a global function as a friend, and also a member function of another structure, or even an entire structure.

  3. friendFunctions and friend Classes • friend function • Defined outside class’s scope • Right to access non-public members • Declaring friends • Function • Precede function prototype with keyword friend • All member functions of class ClassTwo as friends of class ClassOne • Place declaration of form friend class ClassTwo; in ClassOne definition

  4. friendFunctions and friend Classes • Properties of friendship • Friendship granted, not taken • Class Bfriend of class A • Class A must explicitly declare class Bfriend • Not symmetric • Class Bfriend of class A • Class A not necessarily friend of class B • Not transitive • Class Afriend of class B • Class Bfriend of class C • Class A not necessarily friend of Class C

  5. Note: Because a friend function is not a member function: • definition not qualified with class name and scope operator (::) • receives the object on which it can operate. • uses the dot operator to access the data members.

  6. Precede function prototype with keyword friend. Example class Count { friend void SetX(Count, int); public: Count() { x = 0 } void print(void) { cout<<x; } private: int x; }; void SetX(Count c, int val) { c.x = val; } void main(void) { Count cnt; SetX(cnt, 20); cnt.print(); }

  7. Another Example class classA { friend class classB; private: int data; }; class classB { public: void displaydata_of_classA(classA aObj) { cout<<aObj.data; } void setdata_of_classA(classA aObj, int d) { aObj.data=d; } }; void main(void) { classA a; classB b; b.setdata_of_classA(a,25); b.displaydata_of_classA(a); }

More Related