Difference between friend function and member function in C++

12 Jun, 2022
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();    }}
C++
// Example: Find the largest of two numbers using Friend Function #include<iostream>using namespace std; class Largest{    int a,b,m;    public:        void set_data();        friend void find_max(Largest);       }; void Largest::set_data(){    cout<<"Enter the First No:";    cin>>a;    cout<<"Enter the Second No:";    cin>>b;} void find_max(Largest t){    if(t.a>t.b)        t.m=t.a;    else        t.m=t.b;                 cout<<"Maximum Number is\t"<<t.m;} main(){    Largest l;    l.set_data();    find_max(l);    return 0;}
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:
C++
class freetrial {private:    {    public:        {            void check();        }        trial::void check();    }}
C++
// Example: defining member function without argument inside  class #include<iostream>using namespace std; class Item{    int itemId,quantity;    double price;    public:        void setValue()        {            cout<<"Enter the Item Id:";            cin>>itemId;            cout<<"Enter the Item Price:";            cin>>price;            cout<<"Enter the Quantity:";            cin>>quantity;        }        void display()        {            cout<<endl<<itemId<<"\t"<<price<<"\t"<<quantity;        }         }; main(){    Item _item;    _item.setValue();    _item.display();    return 0;}
Output
Enter the Item Id: 123
123
Enter the Item Price:25
Enter the Quantity:3
123    25    3
C++
// Example: defining member function without argument outside  class #include<iostream>using namespace std; class Item{    int itemId,quantity;    double price;    public:        void setValue();        void display();}; void Item::setValue(){    cout<<"Enter the Item Id:";    cin>>itemId;    cout<<"Enter the Item Price:";    cin>>price;    cout<<"Enter the Quantity:";    cin>>quantity;} void Item::display(){    cout<<endl<<itemId<<"\t"<<price<<"\t"<<quantity;}         main(){    Item _item;    _item.setValue();    _item.display();    return 0;}
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
chinmaychougaonkar
aditiyadav20102001
Friend function and class
Difference Between
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between Synchronous and Asynchronous Transmission
Difference Between Method Overloading and Method Overriding in Java
Difference Between Go-Back-N and Selective Repeat Protocol
Time complexities of different data structures
Difference between Compile-time and Run-time Polymorphism in Java
Differences and Applications of List, Tuple, Set and Dictionary in Python
Difference between HTTP GET and POST Methods
Difference between Top down parsing and Bottom up parsing
Difference between Prim's and Kruskal's algorithm for MST
Difference Between Paging and Segmentation