Data abstraction in C++.


Data abstraction and it's uses

  • Data abstraction is one of the most essential and important feature of object oriented programming in C++. 
  • Abstraction means displaying only essential information and hiding the details. 
  • Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.
  • 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.
www.mskuthar.blogspot.com
  • Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation.
  • Let's take one real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen.
  • Thus, we can say a television clearly separates its internal implementation from its external interface and you can play with its interfaces like the power button, channel changer, and volume control without having zero knowledge of its internals.
  • Now, if we talk in terms of C++ Programming, 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.
  • For example, your program can make a call to the sort() function 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, your function call will still work.
  • In C++, we use classes to define our own abstract data types (ADT). You can use the cout object of class iostream to stream data to standard output like this:


#include <iostream.h>
#include <conio.h>
int main( )
{
   cout << "Hello C++" <<endl;
   return 0;
}

// program to calculate the power of a number.

#include <iostream.h>
#include <conio.h>  
#include<math.h>   
int main()  
{    
 int n = 4;  
   int power = 3;  
   int result = pow(n,power);         // pow(n,power) is the  power function  
   std::cout << "Cube of n is : " <<result<< std::endl;  
   return 0;  
}  
Expected Output:-

Cube of n is : 64
  • Here, you don't need to understand how cout displays the text on the user's screen. 
  • You need to only know the public interface and the underlying implementation of cout is free to change.

//Program to show use of Data Abstraction.
#include <iostream.h>
#include <conio.h>  
class sample {
public:
    int gl, g2;

public:
    void val()
    {
        cout << "Enter Two values : "; cin >> gl >> g2;
    }
    void display()
    {
        cout << gl << " " << g2;
        cout << endl;
    }
};
int main()
{
    sample S;
    S.val();
    S.display();
}

Expected Output:-
Enter Two values : 20
50
20 50

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:
  • 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.
  • Members defined with a private 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.

Benefits of Data Abstraction:

  • Data abstraction provides two important advantages:
  1. Class internals are protected from inadvertent user-level errors, which might corrupt the state of the object.
  2. The class implementation may evolve over time in response to changing requirements or bug reports without requiring change in user-level code.
  3. By defining data members only in the private section of the class, the class author is free to make changes in the data. 
  4. If the implementation changes, only the class code needs to be examined to see what affect the change may have. 
  5. If data are public, then any function that directly accesses the data members of the old representation might be broken.

Data Abstraction Example:


  • Any C++ program where you implement a class with public and private members is an example of data abstraction. 
  • Consider the following example:


#include <iostream.h>
#include <conio.h>
class Adder{
  public:
      // constructor
      Adder(int i = 0)
      {
        total = i;
      }
      // interface to outside world
      void addNum(int number)
      {
          total += number;
     }
      // interface to outside world
      int getTotal()
      {
          return total;
      };
   private:
      // hidden data from outside world
      int total;
};
int main( )
{
  Adder a;
   a.addNum(10);
   a.addNum(20);
   a.addNum(30);
  cout << "Total " << a.getTotal() <<endl;
   return 0;
}

Expected Output:-

Total 60
  • Above class adds numbers together, and returns the sum. The public members addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. 
  • The private member total is something that the user doesn't need to know about, but is needed for the class to operate properly.

//Simple illustration of abstract class and pure virtual function in C++

#include <iostream.h>#include <conio.h>
class base_class{ public:  virtual void display() = 0;};class derived_class : public base_class{ public: void display() {  cout<<"This is simple illustration of abstract class and pure virtual function"; }};
int main(){  derived_class obj;
  obj.display();
 return 0;}
Expected Output:-
This is simple illustration of abstract class and pure virtual function
//Let's see a simple example of data abstraction using classes.
#include <iostream.h>
#include <conio.h>   
 class Sum    
{    
private: int x, y, z; // private variables  
public:    
void add()    
{    
cout<<"Enter two numbers: ";    
cin>>x>>y;    
z= x+y;    
cout<<"Sum of two number is: "<<z<<endl;    
}    
};    
int main()    
{    
Sum sm;    
sm.add();    
return 0;    
}    
Expected Output:-
Enter two numbers:
3
6
Sum of two number is: 9

Post a Comment

Previous Post Next Post