Friend function

as we know that a member defined in 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.

Friend function

A function, using which we can access a private member of a class while the friend function is not a member of a class.

Let’s see with the help of Diagram, below-

friend-function

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 use the friend function anywhere (private or public) ) can declare.

The friend keyword is used to make a function a friend.

SYNTEX:-

friend return-type function-name(class-name);

In the syntax given above, an object of class is declared with class-name, in a way we can say that the parameter in the friend function contains a class object declaration.


Declaration of a friend function

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 class, such as-

class student
 { 
     .........
   public:
     ..........
     void friend show_record(student);
 };

while definition of a friend function anywhere (inside class and outside class).

 

Defining friend function definition

The definition of friend function 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.

Here is a syntex 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 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 syntex-

friend_function_name(class-object);

Example:-

void main() 
  {
    class-name obj1;
    friend-function-name(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, but their scope is different.

Let’s try with in 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 obj1)  //outside definition of friend function without scope resolution operator(::)
{
    cout<<"\nRoll no: "<<obj1.roll_no;   // accessing first private member
    cout<<"\nName : "<<obj1.name;      // accessing second private member
}

void main()     // main function start here
{
   clrscr();

   student obj2;

   obj2.get_record();      //normal member calling
   show_record(obj2);     //friend function calling

   getch();
}
OUTPUT:-
Ente 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++