OOAD Functions Q/A #1



Question:What is a friend function? What are the pros and cons of using a friend function?

Answer:

A friend function is a special function which is not a member of a class but can access private and protected members of the class. Consider ing the fact that the private members cannot be accessed from outside the class and a non-member function cannot have an access to the private data of a class. C++ allows to have a common function to be made friend with more than one class, thereby allowing the function to have access to the private data of these classes. Such a function is not required to be a member of these classes. Simply create a function outside the class and declare it as a friend of a class. The function should be preceded by the keyword friend. The function can be defined anywhere in the program like a normal C++ function. For example:

class employee
{
...
...
public:
...
...
friend void salary(employee);
}

The functions that are declared using friend keyword are known as friend function. A function can be declared as a friend in any number of classes. Member function of one class can also be a friend functions of another class. In such cases, they are defined using the scope resolution operator as:

class x
{
...
...
int fun1( );
... 
...
};

class y 
{
...  
... 
friend int x:: fun1 ( );
... 
...
};

Characteristics of friend function

  • friend function is not in the scope of the class in which it has been declared as friend.

  • It cannot be called using the object of the class as it is not in the scope of the class.

  • It can be called similar to a normal function without the help of any object.

  • Unlike member functions, a friend function cannot access the member variables directly and has to use an object name and dot membership operator with each member variable.

  • It can be declared either in the public or private scope area of a class.

  • Usually, it has objects as arguments.

Program example to understand the friend function

#include <iostream>

using namespace std;

class test 
{
   private:
      int a,b ;
   public:
      void init( )
      {
         a=10;
         b=20;
      }
      friend int computer (test t);
};
int computer (test t)
{
   return (t.a+t.b);
}
int main()
{
   test t1;
   t1.init();
   cout <<"the result is: "<<computer(t1);
   return 1;
}

Output

The result is :30

Here the function computer() is not a member function of the class test. In order to make this function have access to the private data a and b of class test , it is created as a friend function for the class test.

Benefits of friend function

  • A friend function is used to access the non-public members of a class.

  • It allows to generate more efficient code.

  • It provides additional functionality which is not normally used by the class.

  • It allows to share private class information by a non member function.

  • It is used when two or more classes may contain members that are interrelated relative to others parts of the program.

Disadvantages of friend function

  • A derived class does not inherit friend function.

  • Friend functions can not have a storage class specifier i.e they can not be declared as static or extern.

Advertisements