It is a process of combining data and functions into a single unit. This is the central idea of OOP.

Usually, the data within a class is private & the functions are public The data is hidden

(in OOP data is given more importance than function as data is the reason for program’s existence)

Example: We go to a bank & access the staff rather than the computers and files over there.

The bank staff knows how to access those computers and files.
Data hiding:

It means that data is concealed (masked) within a class so that it cannot be accessed by functions outside the class even by mistake.

The mechanism used to hide data is to put it in a class & make it private.

The body of the class has two keywords namely :

  • (i) private
  • (ii) public

In C++, the keywords private and public are called access specifiers. The data
hiding concept in C++ is achieved by using the keyword private.

Private data and functions can only be accessed from within the class itself. Public data and functions are accessible outside the class also.

Data hiding not mean the security technique used for protecting computer databases.

The security measure is used to protect unauthorized users from performing any operation (read/write or modify) on the data.

The data declared under Private section are hidden and safe from accidental
manipulation. Though the user can use the private data but not by accident.

The principle of data hiding helps the programmer to build secure program that
can not be invaded by code in other parts of a programs.

Data and encapsulation is the most striking feature of a class. The data is not accessible to the outside world, and only those functions which are wrapped in the class can access it.

These functions provide the interface between the object’s data and the program.

This insulation of the data from direct access by the program is called data
hiding or information hiding.

Example of Data hiding in C++ program

using namespace std; 
class MyRank{ 
int a;
public: void read(); //default accessibility is private
void print();
void MyRank :: read() { 
cout<<"Enter any Integer value"<<endl;
cin>>a; } 
void MyRank :: print(){ 
cout<<"The value is "<<a<<endl; } 
int main(){ 
MyRank k;;
return 0;

Output:-Enter any Integer value 666
The value is 666

Explanation of the program

We do not manipulate the data directly. We can do so only by using functions and objects.

Hidden from Whom?

Don’t confuse data hiding with the security techniques used to protect computer databases.

To provide a security measure you might, for example, require a user to supply a password before granting access to a database.

The password is meant to keep unauthorized or malevolent users from altering (or often even reading) the data.

Data hiding, on the other hand, means hiding data from parts of the program that don’t need to access it. More specifically, one class’s data is hidden from other classes.

Data hiding is designed to protect well-intentioned programmers from honest mistakes.

Programmers who really want to can figure out a way to access private data, but they will find it hard to do so by accident.

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: What is Data Hiding In C++

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories


Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page


Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
    Strength indicator
    Log In | Lost Password