Data Hiding in C++: What is Encapsulation and Abstraction?

Last updated on Nov 25,2020 42.7K Views

Data Hiding in C++: What is Encapsulation and Abstraction?

edureka.co

In simple words, data hiding is an object-oriented programming technique of hiding internal object details i.e. data members. Data hiding guarantees restricted data access to class members & maintain object integrity. In this blog, we will understand how data hiding works in C++. Following topics are covered in this tutorial:

Encapsulation, abstraction & data hiding is closely related to each other. When we talk about any C++ program, it consists of two fundamental elements:

Encapsulation

Encapsulation binds the data & functions together which keeps both safe from outside interference. Data encapsulation led to data hiding.

Let’s look at an example of encapsulation. Here, we are specifying the getter & setter function to get & set the value of variable num without accessing it directly.

Example:

#include<iostream> 
using namespace std; 
  
class Encapsulation 
{ 
    private: 
        // data hidden from outside world 
        int num; 
          
    public: 
        // function to set value of  
        // variable x 
        void set(int a) 
        { 
            num =a; 
        } 
          
        // function to return value of 
        // variable x 
        int get() 
        { 
            return num; 
        } 
}; 
  
// main function 
int main() 
{ 
    Encapsulation obj; 
      
    obj.set(5); 
      
    cout<<obj.get(); 
    return 0; 
}

Output:

 

 

Data Abstraction

Data Abstraction is a mechanism of hiding the implementation from the user & exposing the interface.

Example:

#include <iostream> 
using namespace std; 
  
class Abstraction 
{ 
    private: 
        int num1, num2; 
  
    public: 
      
        void set(int a, int b) 
        { 
            num1 = a; 
            num2 = b; 
        } 
          
        void display() 
        { 
            cout<<"num1 = " <<num1 << endl; 
            cout<<"num2 = " << num2 << endl; 
        } 
}; 
  
int main()  
{ 
    Abstraction obj; 
    obj.set(50, 100); 
    obj.display(); 
    return 0; 
}

Output:

Data Hiding in C++ 

Data hiding is a process of combining data and functions into a single unit. The ideology behind data hiding is to conceal data within a class, to prevent its direct access from outside the class. It helps programmers to create classes with unique data sets and functions, avoiding unnecessary penetration from other program classes.

Discussing data hiding & data encapsulation, data hiding only hides class data components, whereas data encapsulation hides class data parts and private methods.

Now you also need to know access specifier for understanding data hiding.

private, public & protected are three types of protection/ access specifiers available within a class. Usually, the data within a class is private & the functions are public. The data is hidden, so that it will be safe from accidental manipulation.

Now let’s look at a data hiding example.

Example: Data Hiding in C++

#include<iostream>
using namespace std;
class Base{
    
    int num;  //by default private
    public:
    
    void read();
    void print();
    
};
 
void Base :: read(){
    cout<<"Enter any Integer value"<<endl; cin>>num;
    
}
 
void Base :: print(){
    cout<<"The value is "<<num<<endl;
}
 
int main(){
    Base obj;
    
    obj.read();
    obj.print();
    
    return 0;
}

Output:

Now after going through the above C++ programs, you would have understood what is data hiding & how to implement it in C++. I hope this blog is informative and added value to you.

Thus we have come to an end of this article on ‘data hiding in C++’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

BROWSE COURSES