5 – Abstract Data Types
Department of Computer Engineering
Faculty of Engineering, Prince of Songkla University
1
Overview:
1.
2.
3.
4.
5.
6.
What is Data Abstraction? What is ADT?
Model for an Abstract Data Type
Complex Number ADT Example
How Well are ADTs Supported in C?
C++
ADT vs Object-Oriented Programming
2
1.1 What is Data Abstraction?
Concept of “Abstraction”
Allows us to consider the high-level characteristics
of something without getting bogged down in the
details
For example: Process abstraction in procedural
programming like C, we can use (pre-defined)
functions without concern how they really works
inside.
Data Abstraction
We know what a data type can do
How it is done is hidden
3
1.2 What is an Abstract Data Type?
Abstract Data Type (ADT)
Defines a particular data structure in terms of data
and operations
Offers an interface of the objects (instances of an
ADT)
An ADT consists of
Declaration of data
Declaration of operations
Encapsulation of data and operations : data is hidden
from user and can be manipulated only by means of
operations
4
1.3 ADT Implementation
Implementaion of an Abstract Data Type (ADT)
Hidden from the user
Same ADT may be implemented in different ways in
different languages
Some languages offer built-in ADTs and/or features to
be used to implement ADTs (user-define types)
ADTs support modular design which is very important in
software development
5
2.1 Model for an ADT
Interface - Operations
Data Structure
System design with ADTs
Problem definiton
Specify ADT interactions
Identification of ADTs
(identify data or attributes)
Specify ADT operations
Identify object hierarchy (if using OOP)
Implement ADTs
6
2.2 Clients and Manufacturers
client
implementation
interface
ADT
use
client
manufacturer’s
responsibility
client
7
2.3 Benefits
Manufacturer Benefits:
easy to modify, maintain
profitable
reusable
Client Benefits:
simple to use, understand
familiar
cheap
component–based
8
3. Complex Number ADT Example
A complex number has a real part and an
imaginary part. e.g.: 2+4i
Interface (operations) ?
create a complex number
add, subtract, multiply, divide
print a complex number
test to see if something is complex
etc.
9
Declare a complex number
Interface:
Complex c1, c2, c3;
Possible Implementation (in C language ):
struct complex {
double real,imag;
}
typedef struct complex Complex;
10
Create a complex number
Interface:
c1 = create_complex(2, 3);
/* conceptually, c1 = 2+3i */
11
Implementation:
Complex create_complex(double real,
double imag)
{
Complex c;
c.real = real;
c.imag = imag;
return c;
}
12
Add two complex numbers
Interface:
c3 = add_complex(c1, c2);
/* conceptually, c3 = c1 + c2 */
13
Implementation:
Complex add_complx(Complex c1,
Complex c2)
{ Complex csum;
csum.real = c1.real + c2.real;
csum.imag = c1.imag + c2.imag;
return csum;
}
14
Using the Complex Number ADT
#include <stdio.h>
/* type implementation */
struct complex {
double real,imag;
typedef struct complex Complex;
/* operation interface */
Complex create_complex(double,double);
Complex add_complex(Complex, Complex);
/* other Complex prototypes
print_complex() . . .
*/
continued
15
Using the Complex Number ADT
int main ( )
{ Complex c1, c2, c3;
c1 = create_complex(2,-3);
c2 = create_complex(2,-3);
c3 = add_complex(c1,c2);
print_complex(c3);
return 0;
}
/*Implementation of Complex functions */
:
16
4. How Well are ADTs Supported in C?
Does C enforce the use of the ADTs interface
and the hiding of its implementation?
No
17
5. C++ and OOP
C++ is a superset of C, which has added features to
support object oriented programming (OOP)
C++ offers STL (Standard Templates Library)
providing a set generic data structures which allow
programmers to easily implement standard data
structures like queues, lists, and stacks.
C++ supports classes
things very like ADTs
Other OOP languages such as Java, Smalltalk
18
6. ADT vs Object-Oriented Programming (OOP)
ADTs are not a part of a particular programming
language
Rather they are implemented by a programmer to
solve a particular problem or some class of problems
In OOP, an ADT can be easily modeled as a class
An instance as an object
Data of ADT as properties or fields of a class
Operations as methods
ADT ≠ OOP
Classes in OOP offers more features than ADTs :
Inheritance (Superclass-Subclass), Polymorphisms, etc.
19