Friend Function in C++ With Example Programs

By
Advertisement
The private members cannot be access from outside the class. That is, a non-member function cannot  have access to the private data of a class. It is possible to grant a nonmember function access to the private members of a class by using a friend. A friend function has access to all private and protected members of the class for which it is a friend. To declare a friend function, include its prototype within the class, preceding it with the keyword friend.



For example consider the following program
using namespace std;
class myclass 
{
int a, b;

public:

friend int sum(myclass x);

void setab(int i, int j);

};

void myclass::setab(int i, int j)
{
a = i;
b = j;
}
// Note: sum() is not a member function of any class.

int sum(myclass x)
{
/* Because sum() is a friend of myclass, it can directly access a and b. */
return x.a + x.b;
}
int main()
{
myclass n;
n.setab(3, 4);
cout << sum(n);
return 0;
}


In this example, the sum( ) function is not a member of myclass. However, it still has full access to its private members. Also, notice that sum( ) is called without the use of the dot operator. Because it is not a member function, it does not need to be (indeed, it may not be) qualified with an object's name.
Characteristics of a Friend function:
  • The function is not in the scope of the class to which it has been declared as a friend.
  • It cannot be called using the object as it is not in the scope of that class.
  • It can be invoked like a normal function without using the object.
  • It cannot access the member names directly and has to use an object name and dot membership operator with the member name.
  • It can be declared either in the private or the public part.
There are some situations in which friend functions are quite valuable.
  1. friends can be useful when you are overloading certain types of operators
  2. friend functions make the creation of some types of I/O functions easier
  3. friend functions may be desirable is that in some cases, two or more classes may contain members that are interrelated relative to other parts of your program.
Let's see a simple example when the function is friendly to two classes.

     
    using namespace std;  
    class B;          // forward declarartion.  
    class A  
    {  
        int x;  
        public:  
        void setdata(int i)  
        {  
            x=i;  
        }  
        friend void min(A,B);         // friend function.  
    };  
    class B  
    {  
        int y;  
        public:  
        void setdata(int i)  
        {  
            y=i;  
        }  
        friend void min(A,B);                    // friend function  
    };  
    void min(A a,B b)  
    {  
        if(a.x<=b.y)  
        std::cout << a.x << std::endl;  
        else  
        std::cout << b.y << std::endl;  
    }  
       int main()  
    {  
       A a;  
       B b;  
       a.setdata(10);  
       b.setdata(20);  
       min(a,b);  
        return 0;  
     }  

References:
  1.  C++ Complete References 
  2. www.javatpoint.com/cpp-friend-function

0 comments:

Post a Comment