Friend Function -Program And Its Important Uses

friend function
friend function

Friend Function:-

The function that is declared with the help of keyword ‘friend’ is known as Friend Function.

With the help of Access Specifier ‘Private’ we can make data members private from non member function of that class.So that non member function can not access  all those data members which are private.

Sometimes there is necessary to use the private data members of a class by non member function. To solve this problem Friend Function is used.

Now we can understand that FF is non member function that is fully granted to access  all the private data members of a class.

We know that a member function is accessed through the object of that specific class but FF needs object to be pass by or pass by reference.

 

Syntax,

friend  <returntype> <functionname>(parameters)

{

Fuction Body

}

Program,

#include<iostream.h>

#include<conio.h>

class b;

class a

{

int x;

public:

a(int xx)

{x=xx;}

friend void  sum(a,b);

};

class b

{

int y;

public:

b(int yy)

{y= yy;}

friend void sum(a,b);

};

void sum(a objx, b objy)

{

cout<<” Sum  is:- “<<objx.x+objy.y;

}

int main ()

{

a  ob1 (10);

b  ob2 (5);

sum (ob1,ob2);

}

Output: –

Sum  is :-15

You Also Can Visit For More Knowledge In C++:-

Visit Your YouTube  Channel:-  Think , About It

Visit Your Website :- www.atozlives.com

Thanks,

“Play With All Computer Science , Mathematics  And Technology.”

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here