What is Data Hiding In C++


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) so that it will be safe from accidental manipulation.
Example: We go 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

Example of Data hiding in C++

#include<iostream> using namespace std; class MyRank{ int a; //default accessibility is private public: void read(); 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; k.read(); k.print(); 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.