This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Monday, February 27, 2023

How data hiding is accomplished?

 Data hiding is accomplished in object-oriented programming (OOP) through the use of access specifiers, which determine the visibility and accessibility of class members. In C++, access specifiers are implemented using the keywords "public," "private," and "protected."

"Public" members are accessible by any code that has a reference to the object. These members can be accessed from outside the class, and they define the interface through which the object can be manipulated.

"Private" members are accessible only by the class itself and its friend functions. These members cannot be accessed from outside the class and are hidden from the user.

"Protected" members are accessible by the class and its derived classes. These members cannot be accessed from outside the class hierarchy and are used to implement inheritance.

By using the "private" access specifier, the data is effectively hidden from the user, making it inaccessible to the outside world. This means that any data contained within the class cannot be accessed, modified or deleted directly by code outside of the class. Instead, it can only be accessed through public member functions that have been specifically designed to allow users to interact with the data in a safe and controlled way.

The ability to hide data in this way provides a number of benefits, including increased security, more efficient code, and easier maintenance. It also allows programmers to make changes to the internal workings of the class without affecting the rest of the program, making it easier to modify and extend the code over time.




No comments:

Post a Comment

If you have any doubts, please let me know

Slider Widget