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

Last updated on Nov 25,2020 42.6K Views

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

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:

  • Program statements – Part of the program (functions) that performs actions.
  • Program dataData in the program which is manipulated using functions.

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:

Output - Data Hiding in C++ - Edureka

 

 

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:

Output - Data Hiding in C++ - EdurekaData 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.

  • Private members/methods can only be accessed by methods defined as part of the class. Data is most often defined as private to prevent direct outside access from other classes. Private members can be accessed by members of the class.

  • Public members/methods can be accessed from anywhere in the program. Class methods are usually public which is used to manipulate the data present in the class. As a general rule, data should not be declared public. Public members can be accessed by members and objects of the class.

  • Protected member/methods are private within a class and are available for private access in the derived class. 

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:

Output - Data Hiding in C++ - EdurekaNow 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.

Comments
0 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.