as we know that a member defined in the private mode of a class can only be accessed by other members of that class i.e. private member cannot be accessed from outside the class.
Let’s see with the help of the Diagram, below-
However, this is a special function that tells the compiler that he is the friend of the class who can access the private member of that class. because he is not a member of the class so we can declare the friend function anywhere (private or public) ) can declare.
The friend keyword is used to make a function with a friend.
syntax
friend return-type function-name(class-name);
In the syntax given above, an object of the class is declared with class-name, in a way we can say that the parameter in the friend function contains a class object declaration.
C++ friend function Declaration
Note, that the class of which is created as a friend, this friend function is declared within that class. In a way, we can say that the declaration of friend function is inside a class, such as-
class student
{
.........
public:
..........
void friend show_record(student);
};
while the definition of a friend function anywhere (inside the class and outside class).
Defining friend function definition
The definition of friend function in C++ can be inside or outside the class like other members,
When we give the definition of a friend function, in it, we declare a new object of the class in the parameters of the friend function, from this object, the friend function accesses both the private and public members of the same class. see the below diagram,
as you can see in the above, object1 is created by friend function(in the parameter) whereas object2 is created by the class. their scope is different.
Here is a syntax to define a friend function definition-
return-type friend-function-name(class-name obj2)
{
private-member.obj2;
public-member.obj2;
}
In the syntax given above, obj2 will be the second object of the class, meaning when we use the friend function, we have to declare two objects of the class. In which the first object is declared as same before(inside void main() function) while the second is declared as a parameter in the Friend function.
How to call friend function in C++?
In order to call a friend-function, we do not use the Dot operator (.), But the object of that class (which is a friend) is in the form of parameters in the friend function.
Here is a calling friend function syntax-
friend_function_name(class-object);
Example
void main()
{
class-name class-obj1;
friend-function-name(class-obj1);
}
Remember, the object obj1 created by the class in the calling Friend function, and the object declared in the parameter in the definition of the friend function, will both be different. although both are objects of the same class, their scope is different.
Let’s try within the program below-
This program is the same as the previous page in which we have stored a student record (roll_no and name). Now it has been created using friend function-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
// public member by default
int roll_no;
char name[20];
public:
void get_record(void); //normal member declaration
void friend show_record(student); //friend declaration in public
};
void student:: get_record() //outside definition using scope resolution operator(::)
{
cout<<"Enter roll no: ";
cin>>roll_no;
cout<<"Enter Name : ";
gets(name);
}
void show_record(student obj2) //outside definition of friend function without scope resolution operator(::)
{
cout<<"\nRoll no: "<<obj2.roll_no; // accessing first private member
cout<<"\nName : "<<obj2.name; // accessing second private member
}
void main() // main function start here
{
clrscr();
student obj1;
obj2.get_record(); //normal member calling
show_record(obj1); //friend function calling
getch();
}
OUTPUT
Enter roll no: 11
Enter Name : Rahul sherma
Roll no: 11
Name : Rahul sherma
more about friend function
Previous- class and object in C++
Next- constructor in C++