DATA Abstraction

Data Abstraction and Information Hiding

Examples: Array Abstract Data Type

Example: String Abstract Data Type

Example:Queue Abstract Data type

Classes normally hide their implementation details from the clients of the classes. This is called information hiding. As an example of information hiding, let us consider a data structure called a stack.

Think of a stack in terms of a pile of dishes. When a dish is placed on the pile, it is always placed at the top(referred to as pushing onto the stack): when a dish is removed from the pile, it is always removed from the top(referred to as popping off the stack). Stacks are known as last-in, first-out(LIFO) data structures the last item pushed(inserted) on the stack is the first item popped(removed) from the stack.

The programmer may create a stack class and hide from its clients the implementation of the stack. Stacks can easily be implemented with arrays. A client of the stack class need not know how the stack is implemented. The client simply requires that when data items are placed in the stack, the data items will be recalled in the last-in, first-out order. Describing the functionality of a class independent of its implementation is called data abstraction, and C++ classes define so-called abstract data types(ADTs). Although users may happen to know the details of how a class is implemented, users should not write code that depends on these details. This means that the implementation of a particular class(such as one that implements a stack and its operations of push and pop) can be altered or replaced without affecting the rest of the system, as long as the interface to that class does not change.

The job of a high-level language is to create a view convenient for programmers to use. There is no single accepted standard view that is no reason why there are so many programming languages. Object-oriented programming in C++ presents yet another view.

Most programming languages emphasize actions. In these languages, data exists in support of the action programs need to take. Data is viewed as being “less interesting” than actions, anyway. Data is “crude”.There are only a few built-in data types, and it is difficult for programmers to create their own new data types

This view changes with C++ and the object-oriented style of programming. C++ elevates the importance of data and its behaviors. The primary activity in C++ is creating new types(i.e. classes) and expressing the interactions among objects of those types.

1.Example for Data Abstraction

#include<iostream> 
using namespace std; 
class implementAbstraction 
{ 
     private: 
         int a, b; 
 public:
      // method to set values of   
   // private members      
void set(int x, int y)     
 {          
a = x;          
b = y;     
}      
void display()
{ 
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
}; 
 int main() 
 { 
     implementAbstraction obj; 
     obj.set(10, 20); 
     obj.display(); 
     return 0; 
 }

OUTPUT:

a=10
b=20

An abstract data type actually captures two notations, namely, data representation and the operations that are allowed on those data. For example, the notion of int defines addition, subtraction, multiplication, division and modulus operations(among others) in C++, but division by zero is undefined; and these allowed operations perform in a manner sensitive to machine parameters such as the fixed word size of the underlying computer system

Examples: Array Abstract Data Type

This primitive capability is acceptable for performing array operations if the programmer is cautious and undemanding. There are many operations that would be nice to perform with arrays, but that is not C++. With C++ classes, the programmer can develop an array of ADT that is preferable to “raw” arrays. The arrays class can provide many helpful new capabilities such as

  • subscript range checking
  • an arbitrary range of subscripts instead of having to start with 0
  • array assignment
  • array comparison
  • array input/output
  • an array that knows their size
  • arrays that expand dynamically to accommodate more elements.

New classes created in C++ environments can be proprietary to an individual, to small groups or to companies. Classes can also be placed in standard class libraries intended for wide distribution.ANSI(The American National Standards Institute) and ISO(The International Organization for Standardization) have developed a standard version of C++ that includes a standard class library. The reader who learns C++ and object-oriented programming will be ready to take advantage of the new kinds of rapid, component-oriented software development made possible with increasingly abundant and rich libraries

Example: String Abstract Data Type

C++ is an intentional sparse language that provides programmers with only the raw capabilities needed to build a board range of systems(Consider it a tool for making tools). The language is designed to minimize performance burdens.C++ is appropriate for both applications programming and systems programming the latter places extraordinary performance demands on programs. Certainly, it would have been possible to include a string data types among C++’s built-in data types. Instead, the language was designed to include a mechanism for creating and implementing string abstract data types through classes.

Example:Queue Abstract Data type

Each of us stands in line from time to time. A waiting line is also called a queue. We wait in line at the supermarket checkout counter, we wait in line to get gasoline, we wait in line to board a bus, we wait in line to pay a highway toll and students know all too well about waiting for line internally, so we need to write programs that simulate what queues are and do. A queue is a good example of an abstract data type. A queue offers well-understood behavior to its clients. Clients put things in a queue one at a time using an enqueue operation and the clients get those things back one at a time on demand using a dequeue operation. Conceptually, a queue can become infinitely long. A real queue, of course, is inserted in the queue is the first item removed from the queue.

The queue hides an internal data representation that somehow keeps track of the items currently waiting in line, and it offers a set of operations to its clients, namely, enqueue and dequeue. The clients are not concerned about the implementation of the queue. Clients merely want the queue to operate “as advertised.”When a client enqueues a new item, the queue should accept that item and place it internally in some kind of first-in, first-out data structure. When the client wants the next item from the front of the queue, the queue should remove the item from its internal representation and should deliver the item to the outside world(i.e. to the client of the queue) in FIFO order(i.e. the item that has been in the queue the longest should be the next one returned by the next dequeue operation).

The queue ADT guarantees the integrity of its internal data structure. Clients may not manipulate this data structure directly. Only the queue member functions have access to its internal data. Clients may cause only allowable operations to be performed on the data representation; operation not provided in the ADT’s public interface is rejected in some appropriate manner. This could mean issuing an error message, terminating execution or simply ignoring the operation request.

Translate »