Download presentation
Presentation is loading. Please wait.
Published byCora Johnston Modified over 9 years ago
1
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank 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
2
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)
3
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)
4
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
5
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
6
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.
7
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
8
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
9
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)
10
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
11
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)
12
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
13
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 displayList and the implementation of the ADT list
14
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.)
15
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Axioms (aList.createList()).getLength()=0 (aList.insert(i,x)).getLength() = aList.getLength() + 1 (aList.remove(i)).getLength() = aList.getLength() – 1 (aList.createList()).is_empty() = true (aList.insert(i,item)).is_empty() = false (aList.createList()).remove(i) = error (aList.insert(i,x)).remove(i) = aList (aList.createList()).retrieve(i) = error (aList.insert(i,x)).retrieve(i) = x aList.retrieve(i) = (aList.insert(i,x)).retrieve(i+1) aList.retrieve(i+1)=(aList.remove(i)).retrieve(i)
16
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
17
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++
18
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
19
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
20
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
21
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-13a (a) Deletion causes a gap
22
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-13b (b) Fill gap by shifting
23
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 ListA.h and ListA.cpp.
24
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)
25
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 (ExceptionClass identifier) { statement(s); } When error is detected, exception is thrown: throw ExceptionClass(stringArgument);
26
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 myMethod(int x) throw (BadArgException, MyException) { … if (x==MAX) throw BadArgExceotion(“BadArgException: reason”); … throw MyException (“MyException: reason”); … }
27
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: ListIndexOutOfRangeException, ListException Look at files: ListIndexOutOfRangeException.h, ListException.h, ListAexcept.h, ListAInsertexcept.cpp.
28
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.