Data Hiding In C++

It is also known as data encapsulation and is a fundamental concept of Object oriented programming.By the way, in many instances you may come across the term ‘method’. A method is just another term used for a function.
Now let us assume that you have created a new library that you want to distribute to other programmers. A library is a C++ code written for some specific purpose.
For example, working with images (bmp, gif or jpg) isn’t so easy in C++. You have to write methods (functions) for reading an image, identifying what type it is, displaying etc.
There are many applications where image processing is applicable. The simplest application is inOptical Character Recognition (OCR). In OCR we try to write a program that will be able to recognize hand-written letters or numbers.
For instance, if you write 8, the computer should be able to recognize that an 8 has been written (no matter how bad the handwriting is).
Whatever you write on paper will be scanned into the system and stored in the form of an image file. A programmer developing an OCR application, could either start from scratch or he could focus on his problem (i.e. OCR).
By starting from scratch, We mean that he has to write a lot of coding for reading an image etc. If he can get access to some good Image processing library he could simply use the #include directive and concentrate on the OCR algorithm.
So, let’s say that you have created a library for the purpose of image processing. You will make use of the concept of classes.
You will create a class called ‘Image’ which will contain member functions for reading an image, identifying the type of image etc. You will also have some member data like the height and width of the image.
When you distribute your library to others, you give them the opportunity to create objects (or instances) of your ‘Image’ class.
You will also have provided a little help file describing the various functions that are available for use.
Note: You will only describe the use of the various functions (ex: the function syntax and what it will do). You will not be providing an insight into the implementation aspect of the function.
Now, the OCR person will start coding for his application. There is a chance that he might use the same member data identifier ‘length’ and ‘width’ in his program.
Imagine the consequence if ‘length’ and ‘width’ are not made private within the class.
If they are not made private, there is a good chance that the user will fiddle around with the data (knowingly or unknowingly) and their values will get altered by the user (which is what you don’t want to happen).
The result is that your functions, which operate on these member data, will produce some ambiguous results.
By hiding your data, you prevent such problems from happening because the user will never be able to access or modify the member data.
Thus We would like to summarize the entire theory that We’ve dished out within a few lines to reinforce the concepts. Data encapsulation means hiding the implementation of a class from the user.
We restrict the user in such a way that the user can perform only limited operations on the private data (and that is only through the member functions).
Another advantage of hiding the implementation is that you (the class designer) may decide to change the algorithm used for reading an image. You might have discovered some better and effective method for doing the same purpose.
By hiding the implementation, all you need to do is to change the function definition. The user will never have to change his code (because you have only changed the implementation, which the user has nothing to do with).
The code written by the user will still run even though you have changed the implementation.
Scroll to Top