Friend Functions


  • We have seen that the private members cannot be accessed from outside the class. That is, a non member function can not have an access to the private data of the class.
  • But, there could be a situation where we would like 2 classes to share a particular function.
    For ex:
    Consider a case where 2 classes manager and scientist have been defined, and we would like to use a function income-tax() to operate on the objects of both these classes.
  • In such situations, C++ allows the common function to be made friendly with both the classes, there by allowing the function to have access to the private data of these classes.
  • Such functions need not be a member of any of these classes.
  • To make an outside function "friendly" to a class, we have to declare function as a friend.
  • Ex:
            
             class ABC
              {
                 …….
                 ……..
            public:
                   ……..
                   ………
                   friend void xyz();       //declaration
                };
  • The function declaration should be preceded by the keyword friend.
  • The function definition does not use either the keyword friend or the scope operator ::.
  • The functions that are declared with the keyword friend are known as "friend functions".

A friend function processes certain special characteristics

  • It is not in the scope of the class to which it has been declared as friend.
  • Since it is not in the scope of the class, it cannot be called using the object of that class.
  • It can be invoked like a normal function without the help of any object.
  • Unlike member functions, it cannot access the member names directly and has to use an object name and dot operator with each member name.
  • It can be declared either in the public or the private part of a class without affecting its meaning.
  • Usually, it has the objects as arguments.
Example

  • The friend function accesses the class variables a and b by using the dot operator and the object passed to it.
  • The function call mean(X) passes the object X by value to the friend function.
  • Member functions of one class can be friend functions of another class.
  • In such cases they are defined using the scope resolution operator as shown below:
  • class X
          { 
            …..
            …….
           int fun1();  //member fun of x
          };
    
         class Y
          { 
            ……
            ……
          friend int X :: fun1();      //fun1() of X
          ……….
          };
    
  • The function fun1() is a member of class X and a friend of class Y.
  • We can also declare all the member functions of one class as the friend functions of another class.
  • In such cases, the class is called a friend class.
  • Ex:
                class z
                {
                 ………..
                 friend class x;    //all member functions of x are
                                        //friends to z
                };


Static data members << Previous

Next >> Returning Objects

Our aim is to provide information to the knowledge seekers.


comments powered by Disqus
Footer1