1 / 5

Friend Class

Friend Class. Friend Class. Like friend function a class can also be declared as friend of other class. When a class is created as friend class then all the member functions of the friend class also become the friend of the other class .

atalo
Download Presentation

Friend Class

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 Class

  2. Friend Class Like friend function a class can also be declared as friend of other class. When a class is created as friend class then all the member functions of the friend class also become the friend of the other class. This requires the condition that the friend becoming class must be first declared or defined (forward declaration). Similarly like, friend function. A class can be made a friend of another class using keyword friend.

  3. Friend Class For example: class A { friend class B; // class B is a friend class ..... ..... ..... }; class B{ ..... ..... ..... }; When a class is made as friend class, all the member functions of that class becomes friend function. In this program, all member functions of class B will be friend function of class A. Thus, any member function of class B can access the private and protected data of class A.

  4. Friend Class If B is declared friend class of A then, all member functions of class B can access private data and protected data of class A.

  5. Friend Class class A { int a; public: friend class B; void changea() {a=10;cout<<"value in A"<<a;} }; class B { public: void changeb(A x) {x.a=100;cout<<"\nValue in B"<<x.a;} }; void main() { A obj1; obj1.changea(); B obj2; obj2.changeb(obj1); }

More Related