Open In App

Difference between friend function and member function in C++

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads