VOOZH about

URL: https://www.geeksforgeeks.org/cpp/difference-between-friend-function-and-member-function-in-cpp/

⇱ Difference between friend function and member function in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference between friend function and member function in C++

Last Updated : 23 Jul, 2025

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:

Output

Enter the First No:25
Enter the Second No:96
Maximum Number is 96

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:

Output

Enter the Item Id: 123
123
Enter the Item Price:25
Enter the Quantity:3
123 25 3

Output

Enter the Item Id:1234
Enter the Item Price:25
Enter the Quantity:4

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 private 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, the binary operation usually takes two explicit parameters.In this, binary operations usually take only one explicit parameter.
The unary operator takes at least one explicit parameter.The unary operator does not take any 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.
Comment
Article Tags: