What is friend function? Explain the use with an example. Friend - TopicsExpress



          

What is friend function? Explain the use with an example. Friend Function: Friend functions are those functions which can access all the functions and variables of a class though it is not a member function of that class. Actually to share a function among two or more classes friend functions are used. If it is declared so, then it will able to access all variables and functions of those classes. Explanation with Example: An explanation can be given using the following program with required comments, 1 //friend function #include class cls2; //this is forward declaration of class cls2 class cls1{ int a; //this ‘a’ is for objects of class cls1 public: void set_a(){ a=4; //setting ‘a’ for objects of class cls1 } friend void sum(cls1, cls2); }; class cls2{ int a; //this ‘a’ is for objects of class cls2 public: void set_a(){ a=4; //setting ‘a’ for objects of class cls2 } friend void sum(cls1, cls2); }; void sum(cls1 x, cls2 y){ cout
Posted on: Wed, 04 Sep 2013 06:03:32 +0000

Trending Topics



Recently Viewed Topics




© 2015