C++ Friend Function

« Previous Tutorial Next Tutorial »

In this tutorial, you will learn all about friend function in C++ with its example program and step by step explanation of the code.

What is Friend Function ?

A friend function is a function of the class that has the same access as member functions of the same class. But friend function is not a member function. If a keyword friend is placed before any function declared inside the class, then that function can be called as friend function.

Where Friend Function is Declared ?

Friend function is declared inside the class where member function of the same class gets declared. But friend function is not a member function of the class to which it is a friend (or where it is declared).

Use friend keyword to declare a function as friend function. Or precede the function name with this keyword. For example,

friend void myFun();

Friend function is defined outside the class (where it is declared). To define a friend function, friend keyword is not required. This keyword is required only while declaring the function inside the class.

Why friend Keyword is used ?

It is used to tell the compiler, that it is not a member function of the class. Rather it is a friend function. Because if you remove the keyword friend, then this function gets processed by compiler as member function of the class.

How to call Friend Function ?

Friend function has no object to call it. Or friend function has no any caller object. Because it is not a member function of the class where it is declared. And to define it, there is no any membership lebel gets attach while defining the function. Therefore, to call this function from main() function, just call it as normal function call.

Why to use Friend Function

In a big project, there are a lot of programmers who work for the same project. Therefore, using friend function, one programmer can access to the private data members of the class defined by another programmer.

For example, let's suppose a programmer named James who want to access the private data member of the class that is defined by other programmer named Ricky. So, Ricky declares a friend function inside the class defined by him and tells the name of this function to his friend James who work for the same project. Now James can use this function to access private data member of the class defined by Ricky.

Step by Step Explanation of Friend Function

Now let's take an example that multiplies any two number entered by user. There is a use of friend function in this program. Let's have a look at the program, later I'll explain each and everything goes inside the program in step by step manner.

using namespace std;

class CodesCracker
        int m, n, res;
        void getData()
            cout<<"Enter Two Numbers: ";
        void showResult()
            cout<<"\nResult = "<<res;
        friend void mulFun(CodesCracker &c);
int main()
    CodesCracker c;
    return 0;
void mulFun(CodesCracker &c)
    c.res = (c.m) * (c.n);

This program was build and run under Code::Blocks IDE. The snapshot given below shows the sample run of the above program:

cpp friend function

Now supply two numbers say 5 and 6. Press ENTER key to see the output as shown in the following snapshot:

friend function cpp

Here is step by step explanation of previous program:

  • Program flow always starts from main() function
  • Therefore, in main() function, there is a statement
    CodesCracker c;
  • It tells the compiler, to create an object named c of the class CodesCracker
  • But before creating an object, compiler checks for the class CodesCracker. That is, whether it is defined or not.
  • Because it is defined, then an object named c gets created
  • Using this object c, a member function getData() gets called
  • Because c is the object declared for the class CodesCracker, therefore function getData() from this class only gets called
  • This function asks from user to enter any two number and stores it in m and n variables
  • This variable is declared as private data member of the same class
  • Now a friend function mulFun() gets called with c as its parameter
  • Here c is the object of class CodesCracker
  • So Program checked whether this function is declared or not, inside the class of that object
  • Because it is declared inside the same class with friend keyword
  • So this function is a friend to that class
  • Friend to the same class means, it can access private data of that class
  • Because, it is already declared in the same class of that object passed as its parameter
  • So program goes to function definition part
  • In function definition, the statement c.res = (c.m) * (c.n); defines that, the value of m and n variable of the class to which the object c is declared, gets multiplied and stored in a variable res of the same class
  • Now program flow again goes back to main() function
  • And the statement c.showResult(); gets executed, where the value of res variable gets printed on output screen. Thats it.

« Previous Tutorial Next Tutorial »