Friend Function In C++ | What Is Friend Function | Why We Use Friend Function In C++ ?

 Friend Function In C++



What is the friend function in c++?

A friend function is a simple function outside a class but it has access to all private + protected members of a class. 
But you might think that we have to tell the class that this function is your friend. You are right you have to define the friend function inside the class by the friend keyword.

To get a better understanding of the friend function let's see an example.

Let suppose a bank in a bank there is a lot of data about user i.e. name, age, address, bank balance, etc. so if RBI should have to investigate that bank then RBI should have to access all of its detail. So here bank is a class and RBI should have a friend function to investigate then without interrupting in the original class.

What is the advantage of the friend function in C++?

friend function is used to access the non-public members of a class. It allows generating more efficient code. It provides additional functionality which is not normally used by the class. It allows sharing of private class information by a non-member function.

How to create a friend function in c++?

To declare a friend function in c++, you should remember the following rules - 

1. Can be defined anywhere. 

2. NOT a member of the class. 

3. Should be declared inside a class--- use friend keyword. 

4. object you need to pass as an argument. 

5. call as normal function--- fun(); 


#include <iostream>
#include <string>
using namespace std;

class bank
{
    //Data Memebers Of A Bank Class
    private:
    float total_bank_balance;
    float loan_on_bank;
    
    
    public:
    char name_of_bank[200];
    bool loan;
    float intrest_rate;

    public:
    void get_detail()
    {
        cout<<"Enter The Details Of The Bank"<<endl;
        cout<<endl;
        cout<<"FULL NAME OF THE BANK (WITH NO SPACES)"<<endl;
        cin>>name_of_bank;
        cout<<endl;
        cout<<"TOTAL BANK BALANCE IN BANK (in Crores):"<<endl;
        cin>>total_bank_balance;
        cout<<endl;
        cout<<"TOTAL LOAN ON BANK (in Crores)"<<endl;
        cin>>loan_on_bank;
        cout<<endl;
        cout<<"LOAN AVAILABLE ON (YES : 1 , NO : 0)"<<endl;
        cin>>loan;
        cout<<"INTEREST RATE OF LOAN"<<endl;
        cin>>intrest_rate;
        cout<<"THANK YOU FOR GIVING DETAILS"<<endl;
        cout<<endl;
        cout<<endl;

    }

    //Defining Friend Function
    friend void investigate(bank);

};



class bank_user
{
    protected:
    char user_name[200];
    int user_age;
    float user_bb;

    public:
    void get_userdetail()
    {
        cout<<"ENTER YOUR BANK ACCOUNT DETAILS"<<endl;
        cout<<endl;
        cout<<"ACCOUNT HOLDER NAME (WITHOUT SPACES)"<<endl;
        cin>>user_name;
        cout<<endl;
        cout<<"ACCOUNT HOLDER AGE"<<endl;
        cin>>user_age;
        cout<<endl;
        cout<<"TOTAL BANK BALANCE IN ACCOUNT (IN RS.)"<<endl;
        cin>>user_bb;
        cout<<"THANK U FOR GIVING DETAILS"<<endl;
        cout<<endl;
        cout<<endl;
    }
    //Defining Friend Function
    friend void investigate_user(bank_user);
};



void investigate(bank bank_obj){
    bank_obj.get_detail();
    //loan_on_bank is a private data member
    if(bank_obj.loan_on_bank >= 100) //100 Crore
    cout<<bank_obj.name_of_bank<<" AAO KABHI RBI KI HAWELI PE"<<endl; //bank in in under loan ; 
    //false mean this bank is not passed in investigation
    else
    cout<<"NO ACTION NEDDED ON "<<bank_obj.name_of_bank<<endl;

}

void investigate_user(bank_user user_obj)
{
    user_obj.get_userdetail();
    //user_bb is a protected memeber in bank class
    if(user_obj.user_bb>=10000000) //1 Crore
    cout<<user_obj.user_name<<" AAO KABHI CBI KI HAWELI PE"<<endl;
    else
    cout<<"NO ACTION NEEDED ON "<<user_obj.user_name<<endl;
    //false mean this user is not passed in investigation
}
int main()
{
   
    bank SBI;
    investigate(SBI);
    
    bank_user harshad_mehta;
    investigate_user(harshad_mehta);

    return 0;
}

Output - 

Enter The Details Of The Bank


FULL NAME OF THE BANK (WITH NO SPACES)

StateBankOfIndia


TOTAL BANK BALANCE IN BANK (in Crores):

50000


TOTAL LOAN ON BANK (in Crores)

500


LOAN AVAILABLE ON (YES : 1 , NO : 0)

1

INTEREST RATE OF LOAN

12

THANK YOU FOR GIVING DETAILS



StateBankOfIndia AAO KABHI RBI KI HAWELI PE

ENTER YOUR BANK ACCOUNT DETAILS


ACCOUNT HOLDER NAME (WITHOUT SPACES)

HarshadMehta


ACCOUNT HOLDER AGE

31


TOTAL BANK BALANCE IN ACCOUNT (IN RS.)

50000000

THANK U FOR GIVING DETAILS



HarshadMehta AAO KABHI CBI KI HAWELI PE




Comments

Popular posts from this blog

Constructor In C++ | Constructor Explained With Real Life Example

Destructor In C++ | Destructor Explained With Real Life Examples

Array Of Objects In C++ | Array Of Objects Explained With Real Life Example