Data Abstraction and Problem Solving with C Walls

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. M. Carrano and Janet J. Prichard© 2002 Addison Wesley CHAPTER 3 Data Abstraction: The Walls DATA ABSTRACTION AND PROBLEM SOLVING WITH C++ WALLS AND MIRRORS Third Edition Frank M. Carrano Janet J. Prichard

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Outline • Abstract data types (ADTs) • Specifying ADTs (ADT list, ADT sorted list, designing an ADT) • Implementing ADTs (C++ classes, C++ namespaces, array based implementation of ADTs, C++ exceptions)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley ADTs • Modular program is easier to write, read and modify. • Write specification for each module before implementing it. • Use information hiding. • ADT (collection of data and operations)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -1 Isolated tasks: the implementation of task T does not affect task Q

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -2 A slit in the wall

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley ADTs (contd. ) • Basic operations (add, remove, ask questions) • Think always “what” instead of “how” • Data structures are part of an ADT’s implementation • Carefully specify an ADTs operations before implementing. • ADTs vs. data structures.

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -3 A dispenser of chilled water, crushed ice, and ice cubes A program should not depend on the details of an ADT’s implementation

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -4 A wall of ADT operations isolates a data structure from the program that uses it

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Specifying an ADTs • • • Head or front Tail or end Predecessor Successor Operations (add, remove, find, display)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -5 A grocery list

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley ADT List • • Create empty list Destroy a list Is the list empty ? Num. of elements in the list Insert an item at a given position in the list Delete an item Retrieve an element. ADT sorted list (maintains items in sorted order)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -6 UML diagram for ADT List

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -7 The wall between display. List and the implementation of the ADT list

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Designing an ADT • What data the problem requires ? • What operations ? • Example: Determine the dates of all the holidays in a given year (data: dates, operations: determine if a date is holiday, determine the date of the day that follows a given date, determine whether a date is before an other date, etc. )

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Axioms • • • (a. List. create. List()). get. Length()=0 (a. List. insert(i, x)). get. Length() = a. List. get. Length() + 1 (a. List. remove(i)). get. Length() = a. List. get. Length() – 1 (a. List. create. List()). is_empty() = true (a. List. insert(i, item)). is_empty() = false (a. List. create. List()). remove(i) = error (a. List. insert(i, x)). remove(i) = a. List (a. List. create. List()). retrieve(i) = error (a. List. insert(i, x)). retrieve(i) = x a. List. retrieve(i) = (a. List. insert(i, x)). retrieve(i+1) a. List. retrieve(i+1)=(a. List. remove(i)). retrieve(i)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Implementing an ADT Figure 3 -8 ADT operations provide access to a data structure

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -9 Violating the wall of ADT operations, solution: use OOL such as C++

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -10 C++ classe. An object’s data and methods are encapsulated

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -11 An array-based implementation of the ADT list

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -12 Shifting items for insertion at position 3

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -13 a (a) Deletion causes a gap

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Figure 3 -13 b (b) Fill gap by shifting

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley Array based impl. of an ADT • Look at files List. A. h and List. A. cpp.

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley ADT implementation using exceptions • Exception: mechanism for handling an error during execution • C++: try-catch blocks • Try block: statements that might cause an exception • Each try block has one or more catch blocks (in case of multiple errors)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley ADT implementation using exceptions(contd. ) • General syntax: try { statement(s); } catch (Exception. Class identifier) { statement(s); } • When error is detected, exception is thrown: throw Exception. Class(string. Argument);

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley ADT implementation using exceptions(contd. ) • Use C++ exception class, or define your own. • Example: a function whose code can throw an exception: void my. Method(int x) throw (Bad. Arg. Exception, My. Exception) { … if (x==MAX) throw Bad. Arg. Exceotion(“Bad. Arg. Exception: reason”); … throw My. Exception (“My. Exception: reason”); … }

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley ADT implementation using exceptions(contd. ) • Previous array based implementation of ADT used success flag. • Possible errors are: an out-of-bounds list index, insert into a full list, delete or retrieve from an empty list. • 2 user defined exceptions: List. Index. Out. Of. Range. Exception, List. Exception • Look at files: List. Index. Out. Of. Range. Exception. h, List. Aexcept. h, List. AInsertexcept. cpp.

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard © 2002 Addison Wesley SELF-TEST EXERCISES (page 157) • Answers are on page A-112.
- Slides: 28