Data Abstraction in C++

    Data Abstraction is one of the most important features of Object-Oriented Programming language. The concept of Abstraction deal with data and information hiding means in abstraction we try to hide as much as data from the user and only give access to some of its features. For instance, we all know cout<< is an object of iostream header file, but we do not know the inner functionality of this object we only know that it can be used to print values, that’s how abstraction concept works, in abstraction, we try to hide the internal working of our object from the user so the user could not find out the inner working and logic of our code.

    Abstraction Real Word Problem

    For instance, we can send messages through WhatsApp, and WhatsApp gives us many other features to use such as status, delete messages, create group, forward messages, video call, etc. and a user can use all of these features but he/she does not need to know how these features work. The inner implementation of all these features are completely hidden from the user, and this what abstraction is, hiding essential information.

    Abstraction Implementation in C++

    In C++ we can implement or achieve abstraction using two techniques:

    • Using Classes.
    • Using Header files.

    Abstraction using Classes

    The most common technique to implement the concept of Abstraction is by using classes. With class, we can group all the data and function members all together in a single entity and by using the Access Modifies (public, private and protected) we can decide which data and function members of the class need to be hide from the user.

    Abstraction through Access Specifier:

    In Class itself, we use the Access specifier for restriction on the accessibility of data and function member of the class.

    • All the public members of the class can be accessed by the object which is created outside the class.
    • By default, all the class members would be private, if we do no mention any access modifier and all the private members are not accessible by the class object.

    Model:

    #include <iostream>
    using namespace std;
    class Abstraction_Example
    {
    private:
    int x, y;  // using private access modifier we have abstracted the value of x and y from user
    public:
    
       void put(int a, int b)
        {
           x = a;
           y = b;
         }
    
       void show()
         {
            cout<<"Value of x is : " <<x << endl;
            cout<<"Value of y is : " << y << endl;
         }
    };
    
    int main()
    {
    Abstraction_Example a_e;
    a_e.put(200,400) ;
    a_e.show();
    return 0;
    }

    Output:

    Value of x is : 200
    Value of y is : 400
    Behind the code:

    In this example, we have restricted the accessibility of variables x and y to the Abstraction_Example object a_e. Here the object can only access the member functions of the class and these member functions are used to put and show the values in x and y.

    Using Header files

    We can also achieve abstraction using header file, for instance when we want to copy the value of one string variable to another string variable then we use the strcpy() function and to use this function first we include its header file <string.h>. Here we do not need to know how the code of strcpy() has been written, we just used the function to serve our purpose, and the C++ developers have used the header file to abstract the inner working of strcpy() function. Example:

    #include <iostream>
    #include<string.h>
    using namespace std;
    int main() 
    
    {
        char str_1[10]= "Hello", str_2[20];
        strcpy(str_2,str_1);
        cout<<"The value of str_2 is : "<<str_2;
        return 0;
    }

    Output:

    The value of str_2 is : Hello

    Advantages of Abstraction in C++:

    • The essential information of the class can be hidden from the user.
    • We do not need to write the low-level code to hide the essential information.
    • Increase code reusability and decrease code manipulation.
    • Bring more security to the code writing.

    Abstraction Quick Summary :

    • Abstraction deal with hiding the essential information and providing overall features.
    • We can achieve abstraction by using classes and header files.
    • In class, we Access specifier to make the class members private or public.

    People are also reading: