friend functionsone of the major features of oop


Friend Functions

One of the major features of OOP is information hiding. A class encapsulates data and methods to operate on that data in a single unit. The data from the class can be accessed only by member functions of the class. This restricted access not only hides the execution details of the methods and the data structure, it also saves the data from any possible misuse, accidental or otherwise. Though, the concept of data encapsulation sometimes takes information hiding too far. There are situations where a rigid and controlled access leads to inconvenience and hardships.

For example, consider a case where a function is needed to operate on object of two different classes. This function cannot be made a member of both the classes. What can be done is that a function can be explained outside both the classes and made to operate on both. That is, a function not belonging to either, but able to access both. Such a function is known as a friend function. In other words, a friend function is a non-member function, which has access to a class's private members. It is like allowing access to one's personal belongings to a friend.

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: friend functionsone of the major features of oop
Reference No:- TGS0309473

Expected delivery within 24 Hours