Topic :Introduction To Data Abstraction in C++ Programming Language:

Data Abstraction:

Object Oriented Programming has a special Feature called Data Abstraction. Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e, to represent the needed information in program without presenting the details.

Abstraction shows only important things to the user and hides the internal details. For Example: When we using our Laptop, We only know how to use it, but we don't know Exactly how it's work. on another side, when our Laptop is Locked, the user can't see our personal Information on the laptop. so when he will put the password and unlock the window then he'll be able to access the information. so this window password work like Abstraction.


image: Data-Abstraction

Let's take another real-life example of Data Abstraction to clear the concept. Let's take an example of a Laptop, Which we can Turn on and shutdown, we can use the internet, or watch Movies and listen to the song. We can add external components, like extra keyboard, USB Drive, or Extra Hard disk, We can Increase and decrease the volume while listening music. and most important, we are coding/Programming using our Laptop. But we don't know how exactly the Laptop Works, we don't know it's Internal Phenomenon. we don't know how it connects us to the Internet using wifi or DSL. And you don't know how you actually access infobrother through just Cable or wifi. This is all about Data Abstraction. Laptop Abstract it's Internal Information form user.

In our Array Tutorial we used Sort() Function. (Check Example here... ) Without Knowing what algorithm the function actually uses to sort the given values. In fact, the Underlying implementation of the sorting functionality could change between releases of the library, and as long as the interface stays the same, our function call will still work.



Data Abstraction in C++:

In C++, Classes provides great level of Data Abstraction. They provide Sufficient public methods to the outside world to play with the functionality of the object and to manipulate object data, i.e. state without actually knowing how class has been implemented internally.

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



#include<iostream>
using namespace std;
 
main()
{
    cout<<" Info Brother !"<<endl;
    return 0;
}

Here, we don't need to understand how cout displays the text on the user's screen. We need to know only the public interface and the underlying Implementation of cout is free to change.



Access Labels Enforce Abstraction:

In C++, We use access labels to define the abstract interface to the class. A Class may contain zero or more access labels:


  • » Public: Members defined with a public label are accessible to all parts of the program. The data-abstraction view of a type is defined by its public members.
    » Private / Protected: Members defined with a private label or Protected Label are not accessible to code that uses the class. The private sections hide the implementation from code that uses the type.


There are no restrictions on how often an access label may appear. Each access label specifies the access level of the succeeding member definitions. The specified access level remains in effect until the next access label is encountered or the closing right brace of the class body is seen.



  • Advantages of Abstraction:

    » The Programmer does not have to write the low-level code.
    » FThe Programmer does not have to specify all the register/binary-level steps or care about the hardware of instruction set details.
    » Code duplication is avoided and thus programmer does not have to repeat fairly common tasks every time a similar operation is to be performed.
    » It allows internal implementation details to be changed without affecting the users of the abstraction.





Abstraction separates code into interface and implementation. So while designing your component, you must keep interface independent of the implementation so that if you change underlying implementation then interface would remain intact. In this case whatever programs are using these interfaces, they would not be impacted and would just need a recompilation with the latest implementation.



Let's take an example to show, how abstraction work. and how we can use abstraction concept to hide our data from outside the class.


/* Data Abstraction: Example. InfoBrother*/
 
#include<iostream>
using namespace std;
 
class hide //simple class.
{
    private: //private member. (abstraction)
        int x, y;
 
    public:    //public member.
        hide() //constructor.
        {
            x=10;
            y=20;
        }
 
 
        void show() //simple function to display values.
        {
            cout<<" The value of x is: "<<x<<endl;
            cout<<" The value of y is: "<<y;
        }
 
};
 
main()
{
    hide obj;   //object creation of type hide.
    obj.show(); //public member called using object.
 
    return 0;
}


The first Occurrence of the keyword Public or Private / Protected before a member data or a member function makes the compiler assume the type of accesses of all the members following this to be of the first occurrence type. however, if the types of access of member data and member function are different, then the access type should be explicitly mentioned in each case.

Let's remove the Public keyword from above example and check how the program work.


/* Data Abstraction: Example. InfoBrother*/
 
#include<iostream>
using namespace std;
 
class hide //simple class.
{
    private: //private member. (abstraction)
        int x, y;

    
        hide() //constructor.
        {
            x=10;
            y=20;
        }
 
 
        void show() //simple function to display values.
        {
            cout<<" The value of x is: "<<x<<endl;
            cout<<" The value of y is: "<<y;
        }
 
};
 
main()
{
    hide obj;   //object creation of type hide.
    obj.show(); //public member called using object.
 
    return 0;
}


Oops !! after removing the Keyword Public , we got some errors while execution of program. Because in example, we was trying to access Private members of class from outside the class. as we can't access any private member or function of class from outside. so this error occur and our program didn't execute.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement