You are on page 1of 61

March 22,2011

Abstract Data Types


ABSTRACT DATA TYPES
CONTENTS :
→ Introduction to Abstraction
→ Introduction to Encapsulation
→ Introduction to Data Abstraction
→ Design Issues
→ Language Examples
Abstraction

a) Definition :
An abstraction is a view of representation of an entity that
includes the attributes of significance in a particular context.

Purpose of an abstraction :
Simplify the programming process.
b) Types of Abstraction :

Abstraction

Process Data
Abstraction Abstraction
b) Types of Abstraction :

• Process Abstraction
Definition :
It is among the oldest in programming language design.
All the subprograms are process abstractions because they provide
a way for a program to specify that some process is to be done,
without providing the details that how it to be done.
Examples :
Concurrent subprograms and Exception handlers.
Concurrent subprograms : sub functions in programs
Exception handlers : detection of an exception(Exception
Handling) is done by some code unit.
b) Types of Abstraction :

• Data Abstraction
Definition :
 Any representation of data in which the implementation
details are hidden (abstracted) is known as Data Abstraction.
(or)
Data Abstraction is a process of representing the
essential features without including implementation details.
Primary Forms of Data Abstraction :
Abstract data types and objects
b) Types of Abstraction :
• Data Abstraction
Examples :
1. Index of text book.
2. class result
{
int marks;
float percentage;
char name[20];
void input();
void output();
}
contd...
b) Types of Abstraction :

• Data Abstraction
Examples :
2. main()
{
bank b1;
b1.input();
b1.output();
}
in the above example, b1 is an object calling input and output
member functions, but that code is invisible to the object b1.
Encapsulation

a) Definition :
An Encapsulation is a grouping of subprograms and the
data they manipulate and which is either separately or
independently compilable, provides an abstract system and
a logical organization for a collection of related
computations.
Encapsulation
b) Practical Problems :
I. Modularization :
Definition :
To organize the programs into syntactic containers
that include groups of logically related subprograms and
data. These syntactic containers are often called modules,
and the process of designing them is called modularization.
Encapsulation
b) Practical Problems :
II. Compilation Unit :
Definition :
By organizing programs into collections of
subprograms and data, each of which can be compiled
without recompilation of the rest of the program. Such a
collection is called a compilation unit.
So, this encapsulation solves both the practical
problems.
Encapsulation
c) Encapsulations are often placed in libraries and made
available for reuse in programs other than those for which
they are written.
d) In ALGOL, programs can be organized by nesting
subprogram definitions inside the logically larger
subprograms that use them.
e) In FORTRAN, subprograms can be collected into files and
be independently compiled and placed in libraries.
f) In C, a collection of related functions and data definitions
can be placed in a file, which can be independently
compiled.
Introduction to Data Abstraction
a) Definition :
An abstract data type is an encapsulation that includes
only the data representation of one specific data type and
the subprograms that provide the operations for that type.
An instance of an abstract data type is called an object.
Program units that uses a specific abstract data type are
called clients of that type.
Introduction to Data Abstraction
a) Floating Point as an Abstract Data Type :
Most languages include at least one of these, which
provides a means of creating variables for floating point
data and also provides a set of arithmetic operations for
manipulating objects of the type.
The user is not allowed to create new operations on
data of the type, except those that can be constructed
using the built-in-operations.
Introduction to Data Abstraction
b) User Defined Abstract Data Types :
A user defined abstract data type provides the same
characteristics provided by floating points types :
1) Type definition that allows program units to declare
variables of the type but hides the representation of
these variables.
2) Set of operations for manipulating objects of the
type.
Introduction to Data Abstraction
An Abstract Data Type is a data type that
satisfies the following conditions :
 The representation or definition, of the type and the
operations on objects of the type are contained in a
single syntactic unit. Also, other program units may be
allowed to create variables of the defined type.
 The representation of objects of the type is hidden
from the program units that use the type, so the only
direct operations possible on those objects are those
provided in the type’s definition.
Introduction to Data Abstraction
c) Example :
Suppose an abstract data type is to be
constructed for a stack that has the following
abstract operations :
create(stack) : Creates and possibly
initializes a stack object
destroy(stack) : Deallocates the storage
for the stack

contd...
Introduction to Data Abstraction
c) Example :
empty(stack) : A predicate function that
returns true if specified stack is empty
and false if otherwise
push(stack , element) : Pushes the specified element on
the specified stack
pop(stack) : Removes the top element from
the specified stack
top(stack) : Returns a copy of the top
element from the specified stack

contd...
Introduction to Data Abstraction
contd…
c) Example :
Some of the implementation designs of abstract data types
do not require the create and destroy operations.
A client of the stack type could have a code sequence
...
create(STK);
push(STK1,COLOR1);
if(not empty(STK1)) then TEMP := top(STK1);
...
Design Issues

contd...
Language Examples
a) SIMULA 67
b) ADA
c) C++

contd...
Language Examples
a) SIMULA 67 :
I. Encapsulation :
i. Instances or objects of a class are created
dynamically on the heap at the request of the user
program and can be referenced only with pointer
variables. Class objects are therefore head-dynamic.
ii. The code section of a class definition is executed
only once, at object create time.

contd...
Language Examples
a) SIMULA 67 :
I. Encapsulation :
iii. It serves as the constructor for a class and used for
initialization of the variables defined in the class.
iv. SIMULA 67’S contribution to Data Abstraction is the
encapsulation capability of the class construct.
Language Examples
a) Simula 67 :
II. Information Hiding :
i. Variables are not hidden from the clients.
ii. Variables can be accessed through class
subprograms or directly through their names.
iii. Violates the information hiding requirement.
iv. SIMULA 67 class is far less reliable than true abstract
data type.
Language Examples
a) Simula 67 :
III. Example :
Classic Hello World program in SIMULA 67:
Begin
OutText ("Hello World!");
Outimage;
End;
OUTPUT : Hello World!
Language Examples
a) Simula 67 :
IV. Evaluation :
i. SIMULA 67 class construct provides encapsulation,
but it does not provide information hiding.
ii. Revolutionary
iii. Contemporary languages :
Ada and C++
Language Examples
b) Ada :
I. Encapsulation :
I. Packages. Package
II. Packages have two parts :
Specification Body
package package

Provides interface of the Provides


encapsulation implementation of
entities named in
specification
Language Examples
b) Ada :
II. Information Hiding :
i. The user can choose to make an entity entirely
visible to clients or provide only the interface
information.
ii. This is done by two sections of specification
package:
a. Entities are visible to clients
b. Hides its contents

contd...
Language Examples
b) Ada :
II. Information Hiding :
iii. Representation of type appears in a part of
specification called private part, with reserved word
private
iv. The private clause is always at the end of the
specification.

contd...
Language Examples
b) Ada :
II. Information Hiding :
Suppose a type named NODE_TYPE is to be
exported by a package but its representation is to be
hidden. NODE_TYPE is declared in the visible part of
the specification package without its representation
details, as in
type NODE_TYPE is private;

contd...
Language Examples
b) Ada :
II. Information Hiding :
In the private clause, the declaration of NODE_TYPE is
repeated. (complete definition of NODE_TYPE)
package LINKED_LIST_TYPE is
type NODE_TYPE is private;
...
private
type NODE_TYPE;
type PTR is access NODE_TYPE;

contd...
Language Examples
b) Ada :
II. Information Hiding :
type NODE_TYPE is
record
INFO : INTEGER;
LINK : PTR;
end record;
end LINKED_LIST_TYPE;

contd...
Language Examples
b) Ada :
II. Information Hiding :
If none of the entities in a package are to be hidden,
there is no purpose or need for the private part of the
specification.
Types that are declared to be private are called
private types.
Private data types have built in operations for
assignment and comparisons(which are rarely used for
stacks) for equality and inequality.
contd...
Language Examples
b) Ada :
III. Example :
The following is the specification package for a
stack abstract data type :
package STACKPACK is
-- The visible entities, or public interface
type STACKTYPE is limited private;
MAX_SIZE : constant := 100;
function EMPTY(STK : in STACKTYPE) return BOOLEAN;

contd...
Language Examples
b) Ada :
III. Example :
procedure PUSH(STK : in out STACKTYPE; ELEMENT : in
INTEGER);
procedure POP(STK : in out STACKTYPE);
function TOP(STK : in STACKTYPE) return INTEGER;
-- The part that is hidden from clients
private
type LIST_TYPE is array (1 . . MAX_SIZE) of INTEGER;

contd...
Language Examples
b) Ada :
III. Example :
type STACKTYPE is
record
LIST : LIST_TYPE;
TOPSUB : INTEGER range 0 . . MAX_SIZE := 0;
end record;
end STACKPACK;

contd...
Language Examples
b) Ada :
III. Example :
The following is the body package for a
stack abstract data type :
with TEXT_IO; use TEXT_IO;
package body STACKPACK is
function EMPTY(STK: in STACKTYPE) return BOOLEAN is
begin
return STK,TOPSUB = 0
end EMPTY;

contd...
Language Examples
b) Ada :
III. Example :
procedure PUSH(STK : in out STACKTYPE; ELEMENT :
in INTEGER) is
begin
if STK.TOPSUB >= MAX_SIZE then
PUT_LINE(“ERROR – Stack Overflow”);
else
STK.TOPSUB := STK.TOPSUB + 1;
STK.LIST(TOPSUB) := ELEMENT;
end if;

contd...
Language Examples
b) Ada :
III. Example :
end PUSH;
procedure POP(STK : in out STACKTYPE) is
begin
if STK.TOPSUB = 0
then PUT_LINE(“ERROR – Stack Overflow”);
else STK.TOPSUB := STK.TOPSUB – 1;
end if;
end POP;

contd...
Language Examples
b) Ada :
III. Example :
function TOP(STK : in STACKTYPE) return INTEGER is
begin
if STK.TOPSUB = 0
then PUT_LINE(“ERROR – Stack is Empty”);
end if;
end TOP;
end STACKPACK;

contd...
Language Examples
b) Ada :
III. Example :
USE_STACKS, is a client of package STACKPACK. It
illustrates how the package might be used.
with STACKPACK, TEXT_IO;
use STACKPACK, TEXT_IO;
procedure USE_stacks IS
TOPONE : INTEGER;
STACK : STACKTYPE; - - Creates a STACKTYPE object

contd...
Language Examples
b) Ada :
III. Example :
begin
PUSH(STACK,42);
PUSH(STACK,17);
TOPONE := TOP(STACK);
POP(STACK);
...
end USE_STACKS;
Language Examples
b) Ada :
IV. Related Language :
The related language for Ada language is Modula-2.
Main Difference Between Ada & Modula-2 :
In Modula-2, all types whose representations are
hidden in modules must be pointers. This restriction
provides an alternative to the two-part specification
packages of Ada.
Language Examples
c) C++ :
I. Encapsulation :
i. Data Members
ii. Member Functions
iii. The instances of static dynamic classes are always
created by elaboration of an object declaration.
iv. The lifetime of such a class instance ends when
the scope of its declaration is reached.

contd...
Language Examples
c) C++ :
i. Encapsulation :
v. Classes can have heap-dynamic data members,
so that even though a class instance is stack-
dynamic, it can include data members that are
heap-dynamic and allocated from the heap.
vi. C++ provides the new and delete operators to
manage the heap.

contd...
Language Examples
c) C++ :
i. Encapsulation :
vii. When both the header and the body of a
member function appear in the class definition,
the member function is implicitly inlined then the
complete definition can appear in the class.
viii. If only the header of a member function appears
in the class definition, its complete definition
appears outside the class and is separately
compiled.
Language Examples
c) C++ :
II. Information Hiding :
i. Private clause
ii. Public clause
iii. Protected clause
iv. Constructor
v. Destructor
vi. Neither constructors nor destructors have return types
and neither use return statements.
vii. Both constructors and destructors can be explicitly called.
Language Examples
c) C++ :
III. Example :
#include<iostream.h>
class stack
{
private :
int *stack_ptr;
int max_len;
int top_ptr;

contd...
Language Examples
c) C++ :
III. Example :
public :
stack() {
stack_ptr = new int[100];
max_len = 99; top_ptr = -1;
}
~stack() { delete [] stack_ptr; }

contd...
Language Examples
c) C++ :
III. Example :
void push(int number) {
if(top_ptr == max_len)
cout<<“ERROR IN PUSH STACK IS FULL.”;
else stack_ptr[++top_ptr] = number;
}
void pop() { if(top_ptr == -1)
cout<<“ERROR IN POP STACK IS EMPTY”;
else top_ptr--; }
contd...
Language Examples
c) C++ :
III. Example :
int top() { return (stack_ptr[top_ptr]); }
int empty() { return (top_ptr == -1); }
}
void main() {
int top_one;
stack stk;
stk.push(42); stk.push(17); top_one = stk.top(); stk.pop(); . .
....
}
Language Examples
c) C++ :
IV. Evaluation :
For the matrix/vector multiplication operation, one C++
solution is to define the operation outside both the matrix
and vector classes, but define it to be a friend of both. The
following skeletal code illustrates this scenario :
class matrix;
class vector {
friend vector multiply(const matrix&,const vector&);
. . . . . };
contd...
Language Examples
c) C++ :
IV. Evaluation :
class matrix{
friend vector multiply(const matrix&,const vector&);
......
};
vector multiply(const matrix& m1,const vector& v1) {
......
}
Language Examples
c) C++ :
V. Related Language :
The related language for Ada language is Java.
Main Difference Between C++ & Java :
i. All user defined data types in Java are classes and
all objects are allocated from the heap and
accessed through reference variables.
ii. Subprograms in Java can only be defined in
classes.

contd...
Language Examples
c) C++ :
iii. The following is Java class definition for stacks :
import java.io.*;
class stack_class
{
private int []stack_ref;
private int max_len, top_index;
public stack_class() {
stack_ref = new int[100];
max_len = 99; top_index = -1;
}

contd...
Language Examples
c) C++ :
public void push(int number) {
if(top_index == max_len)
System.out.println(“Error in Push Stack is full.”);
else stack_ref[++top_index] = number;
}
public void pop() {
if(top_index == -1)
System.out.println(“Error in Pop-stack is empty”);
else - -top_index; }

contd...
Language Examples
c) C++ :
public int top() {
return (stack_ref[top_index]);
}
public boolean empty() {
return (top_index == -1);
}
}
public class stack {
public static void main(String args[]) {

contd...
Language Examples
c) C++ :
stck_class o = new stack_class();
o.push(35); o.push(63);
System.out.println(“63 is : ”+o.top());
o.pop();
System.out.println(“35 is : ”+o.top());
o.pop(); o.pop();
}
}

contd...
Language Examples
c) C++ :
iv. Lack of a destructor in the java version, obviated
by Java’s implicit garbage collection.
v. Another significant difference is the use of a
reference variable, rather than a pointer, to refer
to stack objects.
Any Queries ????
Thank You . . .

By
K.R.K.Sidhartha
Krishna Chaitanya
Ramesh
Swaroop

You might also like