Scalar vs Structured Types - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Scalar vs Structured Types

Description:

use other programmer-defined types ... Java and C define operators for use with primitive (scalar) types ... part of the class definition. body has access to ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 27
Provided by: wat88
Category:

less

Transcript and Presenter's Notes

Title: Scalar vs Structured Types


1
Scalar vs Structured Types
  • Built-in Types
  • scalar int, char, float
  • structured array, struct
  • Abstract Data Types
  • scalar Time, Student
  • values may have multiple fields but are used as a
    single entity
  • structured
  • called containers or collections

2
Container Classes
  • a structured type in which
  • the data is a collection of items (of the same
    type)
  • the operations provide ways to add, delete, and
    retrieve items
  • containers have no knowledge of the internal
    details of the items it contains
  • there are a number of common and/or useful ways
    to organize a collection of like items

3
Ways to Organize a Collection of Like Items
Set
4
Some Linear Container Classes
  • List
  • items are added/removed/retrieved by location
    within the List
  • random access vs. sequential access
  • Lists with a restricted set of operations
  • stack - addition, removal, retrieval all from one
    end of the list
  • queue - addition from one end, removal and
    retrieval from the other end
  • deque - addition, removal, retrieval can happen
    at either end of the list

5
Stacks and Queues
6
Defining the ADT Stack
  • Data
  • a linear collection of data items in which all
    operations occur at one end, called the top
  • Basic Operations
  • construct a stack (usually empty)
  • find out if stack is empty
  • Push add an item at the top of the stack
  • Top retrieve the top item of the stack
  • Pop remove the top item of the stack

7
Implementing the ADT Stack
  • decide how to store the collection of items
  • decide on data members
  • use built-in types
  • use types from the C library
  • use other programmer-defined types
  • write and implement an algorithm to provide each
    of the operations
  • algorithm depends on storage structure used

8
Some Possible Data structures for a linear
collection
  • fixed-size array
  • capacity decided at compile-time
  • size may be less than or equal to the capacity
  • dynamic array
  • capacity decided at run-time
  • size may be less than or equal to the capacity
  • need to learn about pointers and the heap
  • linked list
  • capacity and size are always the same
  • capacity changes as needed during program
    execution
  • also uses pointers and the heap

9
A Stack data structure
typedef ___ StackElement const int CAPACITY
__ int myTop StackElement myArrayCAPACITY
10
The text book's Stack class
11
Stack Applications
  • base-ten to base-two conversion (p.174)
  • remainders need to be printed in reverse order in
    which they are calculated
  • run-time stack of function activation records
    (p.193)
  • push when a function is called
  • pop when a function is returned from
  • arithmetic expression evaluation
  • easier to evaluate when stored in postfix (RPN)
  • infix to postfix conversion algorithm uses a
    stack (p.199)
  • evaluating a postfix expression very easy using a
    stack to store the operands (algorithm on p.195)

12
Postfix evaluation
infix expression (3 4) (5 - 2)
postfix expression 3 4 5 2 -
13
Infix to postfix
  • operands appear in the same order
  • operators have to wait until both operands have
    been found

6 2 - 8 3
14
Queues
  • Fixed-size array implementation of the Queue ADT
    explored in the lab assignment
  • changes occur at both ends, not just one
  • Queue applications
  • files waiting to be printed
  • "jobs" waiting for the CPU or an I/O device
  • tokens waiting to be processed
  • Simulations

15
Compilation phases
  • tokenize the source code (a text file)
  • lexical analysis
  • tokens need to be processed in the same order in
    which they are found (FIFO list)
  • syntax checking
  • parsing
  • translation to object code
  • code generator
  • optimization

16
Operator Overloading
  • Operators are functions with a different syntax
  • 2 3 vs. (2, 3) or add(2, 3)
  • Java and C define operators for use with
    primitive (scalar) types ( gt )
  • these operators are overloaded
  • defined for int, double, etc.
  • C allows overloading operators so they can be
    used with operands of a class type
  • many operators overloaded for the string class
  • can define operators for Time class

17
Some Overloading Basics
  • To overload an operator to work with objects of a
    class, define a function with the name operator
  • operator , operator lt, operator , etc.
  • Function prototypes for some examples
  • bool operator (const Time t1,
    const Time t2)
  • Time operator (const Time t1,
    const Time t2)
  • Time operator (const Time t, int minutes)

18
Using Overloaded Operators
  • Time time1 (5, 25, 'P')Time time2 (11, 15,
    'A')
  • Using operator syntax
  • Time sum time1 time2
  • if (time1 time2) ----
  • Using function syntax
  • Time sum operator (time1, time2)
  • if (operator (time1, time2)) ----

19
Implementation of operator
20
Three ways to Overload
  • free function
  • independent of the class
  • body has no access to private data members
  • friend function
  • a free function (declared as part of the class)
  • body has access to private data members
  • member function
  • part of the class definition
  • body has access to private data members
  • All can be used in the same way (t1 t2)

21
Friend functions
  • A class can give access to its private data
    members by declaring a function to be a friend of
    the class
  • function prototype must be part of the class
    declaration and prefixed by friend
  • function implementation can directly access
    private data members

22
operator as a friend
23
operator as a method
24
Overloading ltlt and gtgt
  • Allows input and output of objects using same
    notation as for built in types
  • Make extraction (ltlt) and insertion (gtgt) operators
    friend functions
  • ltlt and gtgt cannot be overloaded using methods
  • Return the stream to allow chaining of input and
    output operations
  • cout ltlt "The start time is " ltlt start ltlt endl

25
friend istream operatorgtgt (istream istr, Time
t)
istream operator gtgt (istream istr, Time
t) char ch istr gtgt t.myHourgtgt ch gtgt
t.myMinute gtgt t.myAMorPM gtgt ch
assert ( -----) // return the stream by
reference return istr
26
friend ostream operatorltlt (ostream ostr, const
Time t)
ostream operator ltlt (ostream ostr, const Time
t) ostr ltlt t.myHour ltlt '' ltlt
t.myMinute ltlt t.myAMorPM ltlt 'M'
return ostr
Write a Comment
User Comments (0)
About PowerShow.com