Data Hiding in C++
C++ :
High-performance apps can be made using the cross-platform language C++. Bjarne Stroustrup created C++ as an addition to the C language. Programmers have extensive control over memory and system resources thanks to C++.
We can say that C++ language is an object oriented programming language. Programming in C++, an object-oriented language, offers applications a distinct structure and encourages code reuse, which reduces development costs.
Applications that can be converted to different platforms can be created using C++ because it is portable.
Fun and simple to learn, C++. Because C++ is close to C# and Java, programmers can easily move from one to the other. We can also say that c++ programming language is an extension of c programming language.
Data hiding in C++ programming language :
In C++, the process of hiding of data or data hiding from object members is known as data hiding. In the event that an object member tries to access hidden data, the programmer will return an error.
The programmer cannot connect to data that has been buried since it is inaccurate because of this safety feature. To know about the internal object details, object-oriented programming (OOP) language uses the software development method known as data hiding (data members).
Data hiding ensures that class members have exclusive access to data and safeguards object integrity by preventing unintentional or intentional alterations. Let us see a small practical example for data hiding in c++ programmimg language.
Example: In this illustration, a class is present along with two functions and a variable. The variable "num" in this instance is private, meaning that only other members of the same class may access it. This prevents it from being accessed outside of the class, a process known as data hiding.
Applications of Data Hiding :
Let's look at the following example to better understand data hiding. Assuming you are the programmer, let's say you create a class called "CheckAccount" and construct a data member called "Balance" that represents a user's bank account balance.
The data member "Balance" in this case contains sensitive data. Although you might grant access to an external programmer to examine this sensitive data, it's highly unlikely that you will permit this external application to change the properties of the data held in the member's Balance. Data hiding, and more especially the usage of the private access specifier, can be used to achieve this result.
Data that is duplicated and delicate are frequently subject to data hiding. The efficient and speedy operation of a software depends on this kind of data. Due to unauthorised access, the ensuing data changes are irreversible and necessitate a complete rewrite on the part of the programmer before future use.
Data hiding shields students against unintended oversights. Typically, a class consists of several linked fields that must all be in a reliable state. Let's say a programmer is given direct access to any of these fields. In that circumstance, there's a chance that you might make changes to one area without making changes to crucial linked fields, which would put your class in a conflicting situation.
Advantages of Data Hiding:
There are many advantages and benefits of data hiding. Few of them are as shown below:
- It is used to decrease the complexity and unpredictable nature of data.
- It increases the program's capacity for reuse.
- Reduces system complexity for improved robustness by minimizing interdependencies between software components.
- It offers protection for data against tampering and unauthorised access.
The data hiding in C++ programming language consists of private , public and also protected methods as well as members.
Now let us see an example of data hiding in C++ programming language using the following code as shown below:
Example :
#include < iostream >
using namespace std;
class Parent
{
int n;
public :
void read ( );
void print ( );
};
void Parent : : read ( )
{
cout < < “ Enter the value of integer : “ < < endl ; cin > > n;
}
void Parent : : print ( )
{
cout < < “ The integer value is “ < < num < < endl ;
}
int main ( )
{
Parent obj1;
obj1.read ( );
obj1.print ( );
return 0;
}
Output :
Enter the value of integer :
15
The integer value is 15