C++ friend function

C++ Friend Function


In this article ,we will learn about the friend function,how to declare, define, and why we need friend function.


The function of access to all the private and protected data of their classes is called friend function.


Why a friend function is needed?


In the cpp programming, classes introduce for hiding the data .we can not access the data 

Of the private part of the other class. The programmer writes complex code to use the different class data for the program.C++ to introduce the friend function to solve this problem. By creating just friend function


How to use the friend function?


Following is the syntax of the friend function.



Important points during using of the friend function.


Declaration of the friend function in c++


Friend keyword is used during the declaration of the friend function.

Like a normal function, the friend function also declares inside the class may be private or public part of the class.


Defining the friend function in c++


During the defining of the friend function, a keyword of a friend is not needed. like, Define a normal function.


Take an example with one class for clarification.

#include<iostream>

using namespace std;

class myclass

{

private:

int number;

public:

myclass(int num)

{

number = num;

}

void display()

{

cout<<" number ="<<number<<endl;

}

friend int func(myclass object ); // declare  the friend function

};


int func(myclass object ) //defining the friend function

{

object.number+=10; // access the private member of the class

return object.number; // return the object

}


int main()

{

myclass obj(5); //create the object

obj.display();

cout<<func(obj); //call the friend function

}

output

number =5

15

Here, friend function is used to access the private member of the class myclass , and increment in the number by 10 then returns the object of myclass.

you noticed that friend function is useless.we can also access the member of private by the member function of the class. But if friends use for the two different classes then it important to come.

Take the example of two different classes.

#include<iostream>

using namespace std;


class myclass;

class yourclass

{

private:

int number;

public:

yourclass(int num)

{

number = num;

}

void display()

{

cout<<" number ="<<number<<endl;

}

friend int sum(myclass object1, yourclass object2 ); // declare  the friend function

};



class myclass

{

private:

int number1;

public:

myclass(int num)

{

number1 = num;

}

void display()

{

cout<<" number ="<<number1<<endl;

}

friend int sum(myclass object1, yourclass object2 ); // declare  the friend function

};



int sum(myclass object1 , yourclass object2 ) //defining the friend function

{

int fun;

fun = object1.number1 + object2.number; // access the private member of the class

return fun; // return the object

}



int main()

{

myclass obj(5); //create the object

obj.display();

yourclass obj2(10);

obj2.display();

cout<< " sum of object =  "<< sum(obj,obj2); //call the friend function

}


Output


 number =5 number =10 sum of object = 15


In this example,friend is used to accessing the private member of the two different classes myclass and yourclass.The friend function adds the private number and number1 variables of the classes.


In this case,friend function is very useful to access the private member of the class. If we access the private member of the other class then it is difficult to code.


Please write the comment,if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.

Comments

Popular posts from this blog

how to cout in cpp

input by cin in c++

Flutter layout