×

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

Related Topics

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

C++ Call by Reference

Call by Reference is a C++ method for passing arguments to a function that enables us to pass the actual memory address of the parameter rather than a copy of...

4 minutes read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

C++ Program to move all zeros to the end of the array

Write a program to move all the zeros in the arr[] to the end. The order of the non-zero elements should not be altered and all the zeros should be...

3 minutes read.

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.

Structure Sorting (By Multiple Rules) in C++

To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty...

3 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

Hierarchical Inheritance

C++ Hierarchical Inheritance Hierarchical inheritance inherits the property of one base class in more than one derived class.   C++ Hierarchical Inheritance Example #include <iostream>   using namespace std;   class Person {       char gender[10];       int age;   public:       void getPerson()       {           cout << "Age: "; cin >> age;           cout << "Gender: "; cin >> gender;       }       void dispPerson()       {           cout << "Age: " << age << endl;           cout << "Gender: " << gender << endl;       }   };   class Employee : public Person {       float salary;   public:       void getEmployee()       {           Person::getPerson();           cout << "Salary: Rs."; cin >> salary;       }       void dispEmployee()       {           Person::dispPerson();           cout << "Salary: Rs." << salary << endl;       }   };   class Student : public Person {       char level[20];   public:       void getStudent()       {           Person::getPerson();           cout << "Class: "; cin >> level;       }       void dispStudent()       {           Person::dispPerson();           cout << "Level: " << level << endl;       }   };   int main()   {       Person per;       Employee emp;       Student stu;       cout << "Student data" << endl;       cout << "Enter data" << endl;       stu.getStudent();       cout << endl << "Displaying data" << endl;       stu.dispPerson();       cout << endl << "Staff Data" << endl;       cout << "Enter data" << endl;       emp.getEmployee();       cout << endl << "Displaying data" << endl;       emp.dispPerson();   } Output: Student data Enter data Age: 10 Gender: f Class: 5 Displaying data Age: 10 Gender: f Employee data Enter data Age:...

1 minute read.

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

10 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

3 minutes read.

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Print Table Using While Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.