Data Abstraction in C++

Data abstraction can be explained as the process of providing only the bare minimum essential information to the end user and hiding their background details. In other words, Abstraction can be defined as the process to represent only the necessary information in program without displaying the details.

Data abstraction is a programming and design technique that relies on the separation of interface and implementation.

 

The above diagram gives us a clear idea about how the concept of data abstraction actually works. The outer rounded rectangular box represents the software in its entire form. The innermost rectangle represents the Implementation, which is the background process, which is hidden from the user. The outer rectangle represents the Interface, which is the process where the user interacts with the software.

Now we will look at a real-world example to understand the concept of data abstraction even better. Let us take the example of the internet itself. For most of us, internet is the answer to all questions. Whenever we need to find out information about anything, all we need to do is open the browser, access a search engine, and type in the topic we want information about.

We, as end users are not bothered about how the information is displayed to us via the internet. We do not have to understand how the entire process is being carried out. We just require the end result. This process is known as Data Abstraction.

In C++, classes provides great level of data abstraction. C++ has provision for many public methods which allow the developers the freedom to customise the functionality of the object and to manipulate object data.

In C++, we use classes to define our own abstract data types (ADT). You can use the cout object of class ostream to stream data to standard output like this

 

#include <iostream>
using namespace std;

int main() {
   cout << "Hello C++" <<endl;
   return 0;
}

Access Labels Enforce Abstraction

In C++, access labels are used to define the abstract interface to the class. A class may contain zero or more access labels. They are as follows:

  • Members defined with a public label can be accessed from all parts of the program.
  • Members defined with a private label cannot be accessed by the code that uses the class.

Benefits:

  • Class members and objects are protected from unpredicted user-level errors, which might corrupt the state of the object.
  • The class implementation may adapt to changing requirements or bug reports without requiring change in user-level code.

Example:

#include <iostream>
using namespace std;

class Sample {
   public:
      // This is a constructor
      Sample(int a = 0) {
         tot = a;
      }
      
      // This is the interface 
      void add(int num) {
         tot += num;
      }
      
      // This is another interface 
      int Total() {
         return tot;
      };
      
   private:
      // hidden data from outside world
      int tot;
};

int main() {
   Sample x;
   
   x.add(20);
   x.add(10);
   x.add(40);

   cout << "Total " << x.Total() <<endl;
   return 0;
}

 

Output:

Total 70