C++ Programming & Tutorials for Beginners

Friend Function

Home  »  C++ Programming  »  Friend Function


     Data hiding and encapsulation, two main concept of object oriented programming states that members functions outside a class cannot access private data member of another class. There are very few situations when non-public data of a class is to be acessed by the member which do not belong to the class. Under these circumstances, friend functions are needed. Friend functions helps in accessing non-public members and data hiding, by permitting unrestricted access to private and protected data. Use of friend function should be done only in unavoidable circumstances and not as a general practice.



Contents




Declaration of Friend Function

     A friend function to a class may be declared or defined within the scope of a class definition. The general syntax of friend function declaration is given below:

friend return_type user_define_name (arguments list)


In above given syntax, friend is keyword which informs the compiler that it is not member function of the class.
return_type is the type of the value that the function returns.
user_define_name is the name of the function defined by the user arguments list is the set of parameters, defined inside the parenthesis. The following given formats shows the friend function's definition inside and outside of class:

friend function defined inside the class:
class demo
{
  private:
    int a;
    _____________________
  public:
    _____________________
    _____________________

  friend void sample (demo abc)
  {
    cout<< abc.a;
  }
};

The above format shows that the friend function is defined inside the class under public variables section. A friend function can also be declared under private section. This does not effects the operations of friend function.

friend function defined outside the class:
class demo
{
  private:
    int b;
    _____________________
  public:
    _____________________
    _____________________

  friend void sample (demo abc);
  {
    cout<< abc.b;
  }
};

In above given format, the friend function is declared defined outside the class. Here, demo is the class name sample is the name of the friend function and abc is the temporary object declared in the friend function definition.




Output









Inline Friend Function

     The friend functions, may also be declared as inline functions. If any friend function is declared inside the class or within the scope of the class definition, then the inline code substitution is automatically assigned to it. But if the friend function id defined outside the scope of class definition, then it is required to assign a keyword inline before the return type. The following example shows the assignment of inline to a friend function.





Output





Common Friend Function For Two Classes

     Common friend function for two classes means a function to operate on objects of two different classes. Therefore in this case, the function will take objects of two classes as arguments. In this case, the friend classes should have forward declaration so that the friend function can operate on their private data members. The below program show Common Friend Function for two classes.



Output
Enter the First Number. 5
Enter the Second Number. 5
Multiplication= 25


Note :- The keyword friend should not be repeated in both the function declaration and definition when friend function is defined outside the class.








Previous
Next Post »