Posts

Showing posts with the label friend function.friend function in oops

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 functi...