Answer to Question #163600 in C++ for Eeshal

Question #163600

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.

1
Expert's answer
2021-02-15T00:01:55-0500

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.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment