Skip to content
Related Articles

Related Articles

Improve Article
Difference between friend function and member function in C++
  • Last Updated : 28 Apr, 2021

Friend Function: It is basically a function that is used to access all private and protected members of classes. It is considered as a non-member function of class and is declared by the class that is granting access. This function is prefixed using the friend keyword in the declaration as shown below:

Class definition using friend function:

C++




class freetrial {
private:
    {
    public:
        {
            friend void check();
        }
        void check();
    }
}

Member Function: It is basically a function that can be declared as members of a class. It is usually declared inside the class definition and works on data members of the same class. It can have access to private, public, and protected data members of the same class. This function is declared as shown below:

Class definition using member function:



C++




class freetrial {
private:
    {
    public:
        {
            void check();
        }
        trial::void check();
    }
}

Tabular Difference between the friend function and member function:

Friend Function

Member Function

It can be declared in any number of classes using the keyword friend.It can be declared only in the private, public, or protected scope of a particular class.
This function has access to all private and protected members of classes.This function has access to private and protected members of the same class.
One can call the friend function in the main function without any need to object.One has to create an object of the same class to call the member function of the class.
The Friend keyword is generally used to declare a function as a friend function.In these, there is no such keyword required.
It cannot be called using, can be invoked like a normal function without using an object, defined outside the class scope, etc.It has its own prototype within the class definition, operates on any object of the class, has access to all members of the class, etc.
This method provides access to provide and protected data members.This method provides modularity to a program.
It is generally used to modify or change private and protected data members of the class.It is generally used to improve code reusability and to make code maintainable.
It also provides additional functionality that is kept outside class, provides functions that need data, allows sharing private class information by non-member function, etc.It allows access to internal private data, can be used a general protocol or interface, use for internal purpose only, and non-publishable operations such as initialization and intermediate results of computation.
In this, binary operations usually take only one explicit parameter.In this, the binary operation usually takes two explicit parameters.
The unary operator does not take any explicit parameter.The unary operator takes at least one explicit parameter.
It is not a part of the class.It is a part of the class definition and is invoked by a particular object.



My Personal Notes arrow_drop_up
Recommended Articles
Page :