Senerio:
ATM (Automated Teller Machine) machines are electronic outlets that allow customers to do their basic transactions without interaction of 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.
Question:
Being a C++ developer, analyze the whole scenario, what do you think, does a friend function contradict the rules of Encapsulation?
Note:
Give your opinion in yes or no with strong reasons.
Yes.
Encapsulation is an OOP concept that binds data and methods that manipulates the data together and prevent both the methods and data from interference or modification from outside. In other words, encapsulation promotes data hiding. A friend function however, is defined outside the specific encapsulated data scope, in this case CUSTOMER class. A friend function that accesses the data violates encapsulation as it accesses the data inside CUSTOMER class while it is not defined inside the CUSTOMER class scope.
Comments
Leave a comment