Abstract data types - PowerPoint PPT Presentation

About This Presentation
Title:

Abstract data types

Description:

Title: Slide 1 Author: tyhuang Last modified by: kun-yuan Created Date: 11/5/2001 6:02:33 PM Document presentation format: Company – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 28
Provided by: tyh1
Category:

less

Transcript and Presenter's Notes

Title: Abstract data types


1
Abstract data types object-oriented paradigm
2
Abstraction
  • Abstraction a view of an entity that includes
    only the attributes of significance in a
    particular context.
  • Two fundamental abstractions process abstraction
    data abstraction
  • Process abstraction sortInt(list, list_len)
  • sorting algorithms is not revealed
  • user code remains same even implementation of
    sorting algorithm is changed
  • used for a long time

3
Abstract data type
  • Abstract Data Type an encapsulation that
    includes only the data representation and the
    subprograms that provides operations for that
    type.
  • reliability user cannot directly access objects
  • readability without implementation details

4
Introduction to Data Abstraction
  • Built-in types are abstract data types
  • e.g. int type in Java
  • The representation is hidden
  • Operations are all built-in
  • User programs can define objects of int type
  • User-defined abstract data types must have the
    same characteristics as built-in abstract data
    types
  • To declare variables of that type
  • A set of ops

5
Abstract data type example
interfaces create(stack) destroy(stack) empty(sta
ck) push(stack, item) pop(stack) top(stack)
code example create(stk1) push(stk1,
34) push(stk1, 20) if ( !empty(stk1) ) temp
top(stk1)
  • stack implementation can be changed from array to
    list w/o affecting the code

6
Encapsulation
  • Encapsulation a grouping of subprograms and the
    data they manipulate
  • Hiding the implementation details of the abstract
    interface
  • Protect internal data
  • Reliability, maintenance

7
Language Examples-C
  • Based on C struct type and Simula 67 classes
  • The class is the encapsulation device
  • All of the class instances of a class share a
    single copy of the member functions
  • Each instance of a class has its own copy of the
    class data members
  • Instances can be static, stack dynamic, or heap
    dynamic

8
At first glance
  • include ltiostream.hgt
  • class stack
  • private
  • int stackPtr
  • int maxLen
  • int topPtr
  • public
  • stack()
  • stackPtr new int 100
  • maxLen 99
  • topPtr -1
  • stack()
  • delete stackPtr

void push(int item) if (topPtr maxLen)
cerr ltlt Stack full\n else
stackPtrtopPtr item void pop()
if (topPtr -1) cerr ltlt Stack
empty\n else topPtr-- int top()
return ( stackPtrtopPtr ) int empty()
return ( topPtr -1) // end class
stack
Data members
Member functions
9
C Class
  • data members data defined in a class
  • stack_ptr, max_len, top_ptr
  • member functions functions defined in a class
  • also called access interfaces
  • push( ), pop( ), top( ), empty( )
  • private entities (data or member functions) that
    are to be hidden from outside the class
  • public entities (data or member functions) that
    are visible outside the class

10
C class (cont)
  • constructor initialize the data members of newly
    created objects
  • stack( )
  • implicitly called when an object of the class
    type is created
  • can have one or more constructor for a class
  • destructor implicitly called when the lifetime
    of an instance of the class ends
  • stack( )

11
Example in C
  • include ltiostream.hgt
  • class stack
  • private
  • int stackPtr
  • int maxLen
  • int topPtr
  • public
  • stack()
  • stackPtr new int 100
  • maxLen 99
  • topPtr -1
  • stack()
  • delete stackPtr

void push(int item) if (topPtr maxLen)
cerr ltlt Stack full\n else
stackPtrtopPtr item void pop()
if (topPtr -1) cerr ltlt Stack
empty\n else topPtr-- int top()
return ( stackPtrtopPtr ) int empty()
return ( topPtr -1) // end class
stack
12
Class usage in C
  • stack stk ? constructor is called to initialize
    the instance
  • at the end of the main, stks destructor is called
  • Code example in C
  • void main()
  • int topOne
  • stack stk
  • // stack-dynamic
  • stk.push(42)
  • stk.push(20)
  • topOne stk.top()
  • stk.pop()
  • // stk being freed

13
Language Examples
  • Constructors
  • Functions to initialize the data members of
    instances
  • May also allocate storage if part of the object
    is heap-dynamic
  • Can include parameters to provide
    parameterization of the objects
  • Implicitly called when an instance is created
  • Can be explicitly called
  • Name is the same as the class name

14
Language Examples
  • Destructors
  • Functions to cleanup after an instance is
    destroyed usually just to reclaim heap storage
  • Implicitly called when the objects lifetime ends
  • Can be explicitly called
  • Name is the class name, preceded by a tilde ()

15
Examples in Java
  • Java uses implicit garbage collection (no
    destructor) and reference variable (not pointer)

Code example in Java public class TestStack
public static void main() INTEGER
topOne StackClass myStack new
StackClass() myStack.push(42)
myStack.push(20) topOne myStack.top()
myStack.pop()
  • Code example in C
  • void main()
  • int topOne
  • stack stk // stack-dynamic
  • stk.push(42)
  • stk.push(20)
  • topOne stk.top()
  • stk.pop()

16
Java features
  • Stack_class does not have destructor
  • implicit garbage collector
  • In main( ), myStack does not get freed
  • implicit garbage collector
  • The use of reference variables
  • Java does not support pointer

17
ExampleStack in Java
  • 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
  • public void push (int number)
  • if (top_index max_len)
  • System.out.println(
  • Error stack full)
  • else
  • stack_reftop_index
  • number

public void pop( ) if (top_index -1)
System.out.println(
Error Stack empty) else
top_index public int top( )
return (stack_reftop_index)
public boolean empty( ) return
(top_index -1) // end class stack
18
Parameterized Abstract Data Types- C
  • Templated Classes
  • Classes can be somewhat generic by writing
    parameterized constructor functions, e.g.
  • stack (int size)
  • stk_ptr new int size
  • max_len size - 1
  • top -1
  • stack stk(100)

19
Parameterized Abstract Data Types
  • The stack element type can be parameterized by
    making the class a templated class
  • Java does not support generic abstract data types

20
Parameterized Abstract Data in C
  • include ltiostream.hgt
  • class stack
  • private
  • int stackPtr
  • int maxLen
  • int topPtr
  • public
  • stack(int size)
  • stackPtr new int size
  • maxLen size -1
  • topPtr -1
  • stack()
  • delete stackPtr

void push(int item) if (topPtr maxLen)
cerr ltlt Stack full\n else
stackPtrtopPtr item void pop()
if (topPtr -1) cerr ltlt Stack
empty\n else topPtr-- int top()
return ( stacktopPtr ) int empty()
return ( topPtr -1) // end class stack
Usage stack myStack(50)
21
Template abstract data type in C
  • include ltiostream.hgt
  • template ltclass Typegt
  • class stack
  • private
  • Type stackPtr
  • public
  • stack()
  • stackPtr (new Type 100),
  • maxLen (99), topPtr(-1)
  • stack(int size)
  • stackPtr new Type size
  • maxLen size 1
  • topPtr -1
  • C example in left
  • stackltintgt stk
  • stackltintgt stk(150)
  • C template classes are instantiated at compile
    time
  • code for new type is created when not existed
  • Java does not support generic abstract data type
    as in left

22
Encapsulation Constructs
  • Original motivation
  • Large programs have two special needs
  • 1. Some means of organization, other than simply
    division into subprograms
  • 2. Some means of partial compilation (compilation
    units that are smaller than the whole program)
  • Obvious solution a grouping of subprograms that
    are logically related into a unit that can be
    separately compiled (compilation units)
  • These are called encapsulations

23
Encapsulation Constructs
  • Nested subprograms in Ada and Fortran 95
  • Encapsulation in C
  • Files containing one or more subprograms can be
    independently compiled
  • The interface is placed in a header file
  • Problem the linker does not check types between
    a header and associated implementation
  • Encapsulation in C
  • Similar to C
  • Addition of friend functions that have access to
    private members of the friend class

24
Naming Encapsulations
  • Large programs define many global names need a
    way to divide into logical groupings
  • A naming encapsulation is used to create a new
    scope for names
  • C Namespaces
  • Can place each library in its own namespace and
    qualify names used outside with the namespace
  • C also includes namespaces

25
OO Key feature
  • Abstraction
  • Well-defined interface
  • Hierarchy
  • Composition
  • Derivation
  • inheritance
  • Encapsulation
  • Hiding the detail
  • Polymorphism

26
From C to C
  • Without abstraction
  • main()
  • int stack_itemsSTACKSIZE, stack_top 0, x
  • /push x into stack/
  • stack_itemstack_top x
  • /pop stack to x/
  • x stack_item--stack_top

27
Abstraction
  • Abstraction
  • void init(stack s
  • viod push(stack s, int i)
  • int pop(stack s)
  • void cleanup(stack s)
  • typedef struct
  • int itemsstacksize
  • Int top
  • stack
  • Void init(stack s) s-gttop 0
  • Void push (stack s, int i) s-gt itemss-gttop
    I
  • Int pop(stack s)return s-gtitems--s-gttop
  • main()
  • int x
  • stack stack1
  • init(stack1)
  • push(stack1, x)
Write a Comment
User Comments (0)
About PowerShow.com