Being a C++ developer, analyze the whole scenario, what do you think “Does a friend function contradict the rules of Encapsulation?


Being a C++ developer, analyze the whole scenario, what do you think “Does a friend function contradict the rules of Encapsulation?

Question:

 Scenario: 

ATM (Automated Teller Machine) machines are electronic
 outlets that allow customers to do their basic transactions
 without interaction of the bank’s representative. While
 programming this ATM in C++, We have created a class
 named CUSTOMER which stores customer data (i.e. name, NIC,
 age, address, account balance, and transaction history, etc.).
 There is a friend function that wants to access some private
 information of the class.

Solution:

“Friend Functions don't violate” the concept of Encapsulation.
Friend functions increase the encapsulation instead of reducing it because friend functions are written “explicitly” by the designer or programmer.
Furthermore, friend functions restrict the private information as private within their class. Friend functions never disclose the information to other class members or throughout the system.
 The above argument corroborates the fact that the control of the interface is still with the programmer.
Therefore, friend functions are treated as part of the interface. For that reason, friend functions work the same as public members since both have exactly the same functionality. Moreover, they only access the same as any other public function of the class.
 Thus we can say that friend functions do not violate the concept of encapsulation.




Thank You

Post a Comment

0 Comments