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++?
A 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();
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
Post a Comment