Definition Of Friend Function In Oops
Even though the prototypes for friend functions appear in the class definition friends are not member functions a friend can be a function function template or member function or a class or class template in which case the entire class and all.
Definition of friend function in oops. A friend function of a class is defined outside that class scope but it has the right to access all private and protected members of the class. Following are some important points about friend functions and classes. A friend function provides privileges to access all the private and protected members of the class. 1 friends should be used only for limited purpose.
In object oriented programming a friend function that is a friend of a given class is a function that is given the same access as methods to private and protected data. An ordinary function that is not the member function of a class has no privilege to access the private data members but the friend function does have the capability to access any private data members. To declare a function as a friend of a class precede the function prototype in the class definition with keyword friend as follows. But that friend function has rights to access to all private and protected members variables and functions.
If a function is friend function of a class that friend function is not the actual member of the class but which function has rights to access to all private and protected members variables and functions. A friend can be a function function template or member function or a class or class template in which case the entire class and all of its members are friends. Too many functions or external classes are declared as friends of a class with protected or private data it lessens the value of encapsulation of separate classes in object oriented programming. Friend function allows us to access private class members from the outer class.
With that being said let s get down to specific. In this tutorial we will learn to create friend functions and friend classes in c with the help of examples. However other oop languages have their own ways to implement the functionality of a friend function. Friend functions allow alternative syntax to use objects for instance f x instead of.
For better understanding understanding friend function and characteristics in c. A friend function is declared by the class that is granting access so friend functions are part of the class interface like methods. Introduction of classes oops simple program for. The keyword friend in the class prototype inside the class definition precedes it.