Introduction to Data Abstraction - PowerPoint PPT Presentation

Loading...

PPT – Introduction to Data Abstraction PowerPoint presentation | free to view - id: 26be6c-NjM4M



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Introduction to Data Abstraction

Description:

The name matches the name of the class. ... The top value in a stack is the last one we pushed onto it and is the only one we can see. ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 42
Provided by: fduu8
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Introduction to Data Abstraction


1
Introduction to Data Abstraction
  • A brief history
  • Basic concepts
  • C classes and Data Abstraction

2
Introduction to Data Abstraction
  • Let's start with the idea that Data Abstraction
    is the development and implementation of new
    data types within a programming environment.

3
Introduction to Data Abstraction
  • A brief history
  • Pascal
  • A type section could define a new data type, but
    not any of the operations on it. Operations
    (functions) were placed elsewhere.
  • Programmers could monkey around with parts of
    the data type in ways that were not intended.
  • There was no way to develop a generic data
    type a data type organization containing
    elements regardless of the data type of those
    elements

4
Introduction to Data Abstraction
  • A brief history
  • C
  • typedefs, structs, unions and enums could be used
    to define a new data type, but not any of the
    operations on it. All of these things COULD be
    placed in corresponding source and header files
  • Programmers could monkey around with parts of
    the data type in ways that were not intended.
  • There was no way to develop a generic data
    type a data type organization containing
    elements regardless of the data type of those
    elements

5
Introduction to Data Abstraction
  • A brief history
  • C
  • classes can be used to define data types AND the
    operations on that data type.
  • Done properly, programmers can not monkey
    around with parts of the data type in ways that
    were not intended.
  • templates can be used to define generic data
    types.
  • The language can be extended to allow operators
    (not functions) on the data type

6
Introduction to Data Abstraction
  • Basic Concepts
  • An abstract data type is a representation of
    information with basic operations defined on it.
  • We can develop an approximation of most data
    types in a program by using the languages
    mechanisms to define an implementation.
  • The three basic concepts are
  • encapsulation
  • information hiding
  • genericity

7
Introduction to Data Abstraction
  • Basic Concepts
  • Encapsulation
  • The entire definition of an ADT can be separated
    from the application using it
  • Information hiding
  • The program application using the ADT can not see
    the details of the ADTs implementation and
    therefore can not monkey around
  • Genericity
  • You can define, for example, a stack as an ADT
    regardless of the kind of information in the
    stack stack of integers, stack of records, stack
    of stacks

8
Introduction to Data Abstraction
  • Example the Vector
  • A Vector is a homogeneous collection of
    information of some base data type. There is no
    limitation on the number of elements in the
    collection.
  • We can always determine the number of elements
    (the size).
  • The collection is ordered, starting at the 0th
    element. Any element can be accessed by its
    index (its place within the ordering).
  • We can insert a new element into a Vector at any
    position.
  • We can append an element after the last element.
  • We remove any element from a Vector.
  • We can detach the last element.

9
Introduction to Data Abstraction
  • A Vector is NOT an array, although it seems that
    way
  • arrays are of a fixed size
  • with arrays, there is no operation to insert,
    append, remove or detach data
  • with an array, there is no operation to determine
    its size.

10
Introduction to Data Abstraction
  • typedef double BaseTypeclass
    Vector public Vector() Vector()
  • int size() const bool error()
    const BaseType value(const int index) bool
    insert(const int index, const BaseType value)
  • bool append(const BaseType value) //
    etcetera, etcetera private static const int
    INITIAL_SIZE 10 BaseType array
  • int allocation int count
  • bool out_of_room

11
Introduction to Data Abstraction
  • typedef double BaseTypeHere we are defining
    the base data type of an element. Later on we
    will learn how to create a generic Vector.

12
Introduction to Data Abstraction
  • class Vector publicHere we place the
    prototypes of the operations the application can
    perform on a Vector. The function definitions
    will be discussed later.
  • privateHere we place the things we want to
    hide special constants, internal structures,
    internal fields of information.

13
Introduction to Data Abstraction
  • public Vector() Vector()
  • These are special methods for an ADT. The name
    matches the name of the class. The first one is
    called a constructor and is automatically called
    when an object of the class is declared. You can
    have more than one!
  • The second one is called the destructor and is
    automatically called when the object disappears.
    There can be only one. For example, it is called
    when an object is declared locally in a function
    and that function returns.

14
Introduction to Data Abstraction
  • include "IntVector.h"
  • void use_a_vector()
  • // declaring Vector objects Vector v, w
  • for (int i 0 i lt 1000 i)
  • v.append(0.0) w.insert (i, 1.0)

15
Introduction to Data Abstraction
  • The purpose of showing this code is to get you
    used to how ADT's are defined and to understand
    the basic C syntax of class specifications.
  • We will start with a much simpler example the
    stack

16
Stacks
  • A stack is homogeneous structure with the
    characteristic of being last-in, first-out.
  • Homogeneous means that all the values stored in a
    stack are of the same type
  • Last-in, first-out means that the last value
    inserted into the stack would be the first one we
    would be able to extract
  • Insertion is called pushing, extraction is called
    popping

17
Stacks
  • An empty stack is one that contains no values.
    We cannot pop an empty stack.
  • In some implementations, it is necessary to
    define a full stack as one that has no more room
    for any more values. We cannot push onto a full
    stack.
  • The top value in a stack is the last one we
    pushed onto it and is the only one we can see.

18
Creating an ADT
  • An Abstract Data Type (ADT) is the definition of
    a representation of information
  • There are two aspects of an ADT
  • the interface
  • how the ADT can be manipulated and examined
  • the implementation
  • how the computer will represent the information
    and how it will perform the operations in the
    interface.

19
Creating an ADT
  • In C, we use the class mechanism to build
    ADT's.
  • // FILE intstack.hclass IntStack
  • public // interface goes here
  • private // implementation's
  • // representation of information goes here

20
Creating an ADT
  • The class definition just shown goes into a
    header file, to be included by source files 1.
    applications using an IntStack 2. the class
    implementation file, IntStack.cpp
  • Applications merely need to include IntStack.h in
    their source files, and need to put IntStack.cpp
    into the project.

21
IntStack
// FILE IntStack.h definition of ADT
IntStack class IntStack public IntStack
() // CONSTRUCTOR IntStack() //
DESTRUCTOR bool is_empty() const bool
is_full() const bool push (const int
value) bool pop (int value) bool pop
() int top () const private // to be
shown later
22
IntStack
  • Note that we are now defining the interface and
    it is in the public section of the definition,
    because we want the user to be able to use this
    stuff.
  • public
  • IntStack() // CONSTRUCTOR
  • IntStack() // DESTRUCTOR
  • A class can have many constructors. Here we
    need only one, the default constructor, called
    that because it takes no arguments.
  • A class can have only one destructor.
  • Note that these are specialized functions
    (methods) that have no return type.

23
IntStack
  • We never actually call the constructors or
    destructor in our programs. They are called
    automatically when the time is correct.
  • Constructors are automatically called whenever a
    declaration of a variable of this type is
    encountered. Such variables are called objects,
    and a constructor is invoked automatically when
    the object is being created.
  • The destructor is automatically called when the
    object is no longer needed upon leaving a
    function in which it has been declared.

24
IntStack
bool is_empty() const bool is_full()
const bool push (const int value) bool
pop (int value) bool pop () int top ()
const Here are the remaining functions, or
methods as we will call them. Notice that they
correspond to the things mentioned in the
definition at the start of the lecture. What new
things do you see here? What seems to be
missing?
25
IntStack
bool is_empty() const bool is_full()
const These are informational methods to
test to see if the stack is full or if the stack
is empty. Because they don't change the stack,
we put the word const at the end, so that whoever
implements these methods will not be allowed to
make any changes to the stack object. But where
is the IntStack that these methods will be
applied to? We will see later how this happens!
26
IntStack
bool push (const int value) Note that value
is an input parameter. We now have a new
notation. Instead of int value, we write const
int value. This is for efficiency. Passing
an ordinary object by value (the old way) forces
the program to copy the information into the
parameter. What if there's lots of
information? The new way just passes the address
of where the information is, but specifies const,
so that it can't be changed.
27
IntStack
bool pop (int value) bool pop () Note
that we have two methods for popping. The first
one pops the stack and saves the popped value in
the output parameter. The second one pops
without saving. Having more than one function
with the same name is called function
overloading, and is perfectly legal as long as
the compiler can decide which one to use when it
sees a call to the function.
28
IntStack
  • Note that pushing does not mean incrementing top,
    nor does popping mean decrementing top. Thats
    implementation!

29
IntStack
// FILE INTSTACK.H Stack of ints ADT
template // // AUTHOR N. Landis // // DATE
2/4/05 // ifndef _INTSTACK define _INTSTACK We
use the ifndef in our header files to prevent
duplicate inclusions. The first time such a
header file is included, _INTSTACK is undefined
and everything up to the endif at the end of the
file will be included. The next time, it is
ignored!
1
30
IntStack
class IntStack public IntStack() In
tStack() bool is_empty() const bool
is_full() const bool push (const int
value) bool pop (int value) bool pop
() int top() const private static
const int MAX_SIZE 10000 int
top_index int stackMAX_SIZE endif
2
3
4
31
IntStack
bool is_empty() const Note 2 The use of the
keyword const at the end of the prototype of a
method indicates that the implementation of the
method is not allowed to modify the
object. bool push (const int value) Note 3
The construction const int value is another way
of indicating an input parameter. We will be
using it from now on instead of int value
32
IntStack
private static const int MAX_SIZE
10000 int top_index int stackMAX_SIZE Th
e first line sets a private constant (the user
doesn't have to know about it) for the size of
the array we will use. Note 4 The word
"static" is required.
33
IntStack
  • What you have just seen is the entire contents of
    IntStack.h.
  • How will the computer know how to push and how to
    pop?
  • We provide a source file, IntStack.cpp, that
    implements each and every one of the methods
    shown in IntStack.h

34
IntStack
// FILE INTSTACK.CPP implementation of the
IntStack ADT include "IntStack.h" IntStackInt
Stack() top_index -1 IntStackIntStack
() bool IntStackis_empty()
const return (top_index -1) bool
IntStackis_full() const return (top_index
MAX_SIZE-1)
must be included!!!
35
IntStack
Note the special syntax of the method header.
This tells the compiler that this is a method and
therefore has the right to see the private
fields. bool push (const int
value) bool result false if (top_index lt
MAX_SIZE 1) top_index stacktop_index
value result true return
result bool IntStackpop () bool
result false if (top_index !
-1) top_index-- return result
IntStack
36
IntStack
bool IntStackpop (int value) bool result
false if (top_index ! -1) value
stacktop_index top_index-- result
true return result int
IntStacktop() const return
stacktop_index
37
An Application using IntStack
include ltiostreamgt include "IntStack.h" int
main() IntStack s int value while (cin gtgt
value s.push(value)) if
(s.is_full()) cout ltlt "Sorry. Stack is full.
STOP" ltlt endl else while (!s.is_empty())
s.pop(value) cout ltlt value ltlt
endl return EXIT_SUCCESS
38
Homework
  • Use intstack.h and intstack.cpp, which I will
    post on my website and write a C program that
    reads a series of int values from cin in an
    interactive fashion. The user should hit
    Control-Z (end-of-file) to terminate the input.
    Afterwards, write the data to cout in the
    following fashion all the positive numbers
    should be printed in the order they came in,
    followed by all the negative numbers in the
    reverse order from the order they came in. Zeros
    should not be printed at all.
  • Use a stack to handle the reverse order.

39
Homework
  • If you want me to give you a critique of your
    work, submit your .h files and .cpp files of the
    project you've created as either a zipped
    attachment or as individual attachments by e-mail
    to
  • csci5555f06_at_optonline.net

40
Food for Thought
  • Suppose you wanted to write a program that reads
    an arithmetic expression consisting of integer
    values, parentheses, and the usual arithmetic
    operators. You want to store the integer values
    into an IntStack. You want to store the
    operators and parentheses into a stack of
    characters.
  • What do you do?

41
Food for Thought
  • Next week we will upgrade the stack by
    introducing templates.
About PowerShow.com