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.”

50 COMMENTS

  1. I made blue cupcakes with chocolate frosting to announce to my in laws. They loved the idea. they had to peel the paper off to reveal the color. Adrea Noah Chatterjee

  2. I feel this is among the most important details for me. And i am glad looking at your report. But want to remark on some general things, The web site style is excellent, the articles or blog posts is truly great : D. Very good work, cheers Fredia Burr Euphemia

  3. It is a great post, so many helpful & useful tips for families. So informative. It is so important to plan your holidays detailed when you go with kids. I think insurance is crucial because you never know what happens. Checking climate conditions before you go and take all travel documents is essential also. Tamar Pepe Lais

  4. I do accept as true with all the ideas you have presented to your post. They are really convincing and will certainly work. Nonetheless, the posts are very short for novices. Could you please lengthen them a bit from subsequent time? Thanks for the post. Carmine Brett Kimmel

  5. Greetings, I do believe your blog could be having internet browser compatibility problems. Whenever I look at your blog in Safari, it looks fine however when opening in I. E., it has some overlapping issues. I just wanted to provide you with a quick heads up! Other than that, wonderful blog! Hanny Abbe Vanderhoek

  6. Hello my loved one! I want to say that this post is amazing, nice written and come with almost all significant infos. I would like to see extra posts like this. Jacinda Saunderson Genny

  7. I like to spend my free time by scaning various internet recourses. Today I came across your site and I found it is as one of the best free resources available! Well done! Keep on this quality! Lea Dwain Adolf

  8. Just awesome advice and tips. My favorite is before starting your day you sit down with your coffee and journal and write your intentions and focus for the day. Fannie Bengt Tronna

LEAVE A REPLY

Please enter your comment!
Please enter your name here