Title: Abstract Data Types and Encapsulation Constructs
1 Abstract Data Types andEncapsulation Constructs
Jianhua Yang
Department of Math and Computer Science
Bennett College
2 Outline
The concept of abstraction
Introduction to data abstraction
Design issues for abstraction data types
Language examples
Parameterized abstract data types
Encapsulation constructs
Naming encapsulations
3 11.1 The concept of abstraction
An abstraction is a view or representation of an entity that includes only the most significant attributes.
It allows programmers to focus on essential attributes, while ignoring subordinate attributes.
Two abstractions data, process
4 Purpose
Its purpose is to simplify the programming process
5 Process abstraction
All subprograms are process abstractions
6 Data abstraction
What is it?
7 11.2 Introduction to Data Abstraction
An abstract data type is an enclosure 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.
8 1. Floating-point as an Abstract Data Type
float x, y, z
9 2. User defined Abstract Data Types
Should be the same as the language-defined types
A type definition that allows program units to declare variables of the type but hides the representation of objects of the type
A set of operations for manipulating objects of the type.
10 An abstract data type
Must satisfy the following two conditions
The declarations of the types and the operations on the objects of the type are contained in a single syntactic unit.
The representation of objects of the type is hidden from the program units that use the types.
11 Advantages
1. It provides a method of organizing a program into logical units that can be compiled separately.
2. Keep specifications and implementation separate.
3. Increase reliability.
4. Easy to maintain.
12 3. An Example
An abstract data type stack
Operations
Create(stack)
Destroy(stack)
Empty(stack)
Push(stack, element)
Pop(stack)
Top(stack)
13 11.3 Design issues for ADT
Encapsulation
Information hiding
package class Public Private Protected 14 11.4 Language Examples
ADT in Ada
ADT in C
ADT in Java
ADT in C
15 1. ADT in C
Struct
Class
16 1) Class (Encapsulation)
Data members
Member functions
The data defined in a C class are called data members The functions defined in a class are called member functions 17 Data members
Stack-dynamic
Heap-dynamic
C provides the new and delete operators to manage the heap. 18 Member functions
Can be defined in two distinct ways
1. complete definition can appear in the class
2. only its header is in the class.
inline 19 2) Information hiding
Private
Public
Protected
Information that is to be hidden is treated as private attributes. Information that is to be visible is treated as public attributes. Information that is to be between private and public is treated as protected attributes. 20 3) An Example void main() int topOne stack stk stk.push(42) stk.push(17) topOne stk.top() stk.pop()
class stack
private
int stackPtr
int maxLen
int topPtr
public
stack()
stackPtr new int100
maxLen99
topPtr-1
stack() delete stackPtr
void push (int number)
if (topPtrmaxLen)
cerrltlterror in push-stack
else
stackPtrtopPtrnumber
void pop()
21 2. ADT in Java
import java.io.
class StackClass //beginning of StackClass
private int stackRef
private int maxLen, topIndex
public StackClass() // A constructor
stackRef new int 100
maxLen 99
topIndex -1
22 ADT in Java (Cont.)
public void push (int number)
if (topIndex maxLen)
System.out.println(Error- stack is full)
else stackReftopIndex number
public void pop()
if(topIndex -1)
System.out.println(Error stack is empty)
else --topIndex
23 ADT in Java (Cont.)
public int top() return (stackReftopIndex)
public boolean empty() return (topIndex -1)
//end of StackClass
24 An example to use StackClass
public class TstStack
public static void main(String args)
StackClass myStack new StackCalss()
myStack.push(42)
myStack.push(29)
System.out.println(29 is myStack.top())
myStack.pop()
System.out.println(42 is myStack.top())
myStack.pop()
myStack.pop() //Produces an error message
25 3. ADT in C
public class Weather
public int DegreeDays
get
return degreeDays
set
degreeDays value
prinvate int degreeDays
26 ADT in C using this class
Weather w new Weather()
int degreeDaysToday, oldDegreeDays
w.DegreeDays degreeDaysToday
oldDegreeDays w.DegreeDays
27 11.5 Parameterized ADT
It is to design a stack abstract data type that can store any scalar type elements rather than being required to write a separate stack ADT for every different scalar type.
28 An example with C
include ltiostream.hgt
template ltclass Typegt
class stack //beginning of stack class
private
Type stackPtr
int maxLen
int topPtr
public
//A constructor for 100 element stacks
stack()
stackPtr new Type100
maxLen 99
topPtr -1
29 An example with C (Cont.)
stack() delete stackPtr
void push(Type number)
if(topPtr maxLen) coutltltError stack is fullltltendl
else stackPtr topPtr number
void pop()
if(topPtr -1) cout ltltError- stack is emptyltltendl
else topPtr --
Type top() return (stackPtrtopPtr)
int empty() return (topPtr -1)
// end of stack class
30 11.6 Naming Encapsulations
for constructing a large program
1. work cooperation
2. using Library.
31 Naming Encapsulations
It define name scopes that assist in avoiding these name conflicts.
32 1. C namespaces
C includes a specification, namespace, that helps programs manage the problem of global namespaces.
PowerShow.com is a leading presentation sharing website. It has millions of presentations already uploaded and available with 1,000s more being uploaded by its users every day. Whatever your area of interest, here you’ll be able to find and view presentations you’ll love and possibly download. And, best of all, it is completely free and easy to use.
You might even have a presentation you’d like to share with others. If so, just upload it to PowerShow.com. We’ll convert it to an HTML5 slideshow that includes all the media types you’ve already added: audio, video, music, pictures, animations and transition effects. Then you can share it with your target audience as well as PowerShow.com’s millions of monthly visitors. And, again, it’s all free.
About the Developers
PowerShow.com is brought to you by CrystalGraphics, the award-winning developer and market-leading publisher of rich-media enhancement products for presentations. Our product offerings include millions of PowerPoint templates, diagrams, animated 3D characters and more.