chapter 3 data abstraction modularity and abstraction
play

Chapter 3: Data Abstraction Modularity and Abstraction Abstraction, - PowerPoint PPT Presentation

Chapter 3: Data Abstraction Modularity and Abstraction Abstraction, modularity,


  1. � ��������������������������������������������������� Chapter 3: Data Abstraction Modularity and Abstraction � Abstraction, modularity, information hiding � Important when developing large programs. � Abstract data types � Divide program in small manageable modules � each module understood individually � Example-1: List ADT � easier to write, understand, modify, and debug � Example-2: Sorted list ADT � Modules communicate using well-defined � C++ Classes interfaces � C++ Namespaces � different module implementations use same interface � C++ Exceptions � provide a different and easier interface to communicating modules � abstraction EECS 268 Programming II 1 EECS 268 Programming II 2 Fundamental Concepts Abstraction � Functional abstraction � Modularity � separates the purpose and use of a module from its � manages complexity of large programs implementation � isolates errors � eliminates redundancies ������������������������������������������ � program is easier to read, write, and modify � Data abstraction � Information hiding � asks you to think what you can do to a collection of data independently of how you do it � hides certain implementation details within a module � allows you to develop each data structure in relative � makes these details inaccessible from outside the isolation from the rest of the solution module EECS 268 Programming II 3 EECS 268 Programming II 4

  2. � ������������������������������������������������� Isolated Tasks Isolation of Modules is Not Total how it interacts with other modules Figure 3-2 A slit in the wall EECS 268 Programming II 5 EECS 268 Programming II 6 Abstract Data Type (ADT) Abstract Data Types � An ADT is composed of � collection of data � set of operations on that data � Specifications of an ADT indicate � what the ADT operations do, not how to implement them � Implementation of an ADT � includes choosing a particular data structure Figure 3-4 A wall of ADT operations isolates a data structure from the program that uses it EECS 268 Programming II 7 EECS 268 Programming II 8

  3. Designing an ADT List ADT Example � The design of an ADT should evolve naturally � ADT for a list of items: grocery list, TO-DO list during the problem-solving process � What operations do we perform on/with a list? � Questions to ask when designing an ADT � add item, delete item, find item, read, etc. � cannot think of everything? � What data does a problem require? � should refine iteratively! � What operations does a problem require? � How to store the data � implementation detail hidden from users of the list � arrays or linked lists EECS 268 Programming II 9 EECS 268 Programming II 10 List ADT Example � Properties List ADT Example � Operations � Except for the first and last items, each item has a � Create an empty list unique predecessor and successor � Destroy a list � Items are referenced by their position in the list � Determine whether a list is empty � Specifications of the ADT operations � Determine the number of items in a list � Define an operation contract for the ADT list � Do not specify how to store the list or how to perform � Insert an item at a given position in the list the operations � Delete the item at a given position in the list � ADT operations can be used in an application without the knowledge of how the operations � Retrieve the item at a given position in the list will be implemented EECS 268 Programming II 11 EECS 268 Programming II 12

  4. List ADT � Operation Contract List ADT Example � Operations � createList() � Create the list -- milk, eggs, butter � aList.createList() � destroyList() � aList.insert(1, milk, success) � isEmpty():boolean {query} � aList.insert(2, eggs, success) � getLength():integer {query} � aList.insert(3, butter, success) � insert(in index:integer, in newItem:ListItemType, � Insert bread after milk out success:boolean) � aList.insert(2, bread, success) � remove(in index:integer, out success:boolean) milk, bread, eggs, butter � Insert juice at end of list � retrieve(in index:integer, dItem:ListItemType, � aList.insert(5, juice, success) out success:boolean) {query} milk, bread, eggs, butter, juice EECS 268 Programming II 13 EECS 268 Programming II 14 List ADT Example � Operations List ADT Example -- Operations � Algorithm description independent of list � Remove eggs implementation, as long as each item has an � aList.remove(3, success) index � milk, bread, butter, juice � Pseudocode function that displays a list � Insert apples at beginning of list displayList(in aList:List){ � aList.insert(1, apples, success) for (position=1 to aList.getLength()){ aList.retrieve(position, dataItem, success) � apples, milk, bread, butter, juice display dataItem } } EECS 268 Programming II 15 EECS 268 Programming II 16

  5. List ADT Example -- Implementation List ADT Example -- Implementation � To delete an item, remove gap in array � How to implement the List ADT ? � ���������� th item is stored in items[k-1] � To insert an item, make room in the array Figure 3-13 (a) Deletion causes a gap; (b) fill gap by shifting EECS 268 Programming II 17 18 List ADT � Options ADT Sorted List -- Properties � Many other design options are possible � Maintains items in sorted order � retrieve items by name, instead of by index � Inserts and deletes items by their values, not � sort items by name or some other factor their positions � display list in some sorted order � Several data structures can be used during implementation � arrays, linked lists, trees, hash-tables, etc. � different advantages, restrictions, and costs EECS 268 Programming II 19 EECS 268 Programming II 20

  6. Implementing ADTs ADT Sorted List � Operation Contract � sortedIsEmpty():boolean{query} � Choosing the data structure to represent the � sortedGetLength():integer{query} �������������������������������������� � sortedInsert(in nItem:ListItemType, out success:boolean) � sortedRemove(in index:integer, out success :boolean) � Choice of a data structure depends on � sortedRetrieve(in index:integer, out dItem:ListItemType, � ������������������������������� out success :boolean){query} � Context in which the operations will be used � locatePosition(in anItem:ListItemType, � Implementation details should be hidden out isPresent:boolean):integer{query} behind a wall of ADT operations � A program (client) should only be able to access the data structure by using the ADT operations EECS 268 Programming II 21 EECS 268 Programming II 22 Violating Information Hiding Hiding Data Structures and Code Figure 3-8 Figure 3-9 Violating the wall of ADT operations ADT operations provide access to a data structure EECS 268 Programming II 23 EECS 268 Programming II 24

  7. � ���������������������������������������������� � �������������������������������������������� ����������������������������� C++ Classes C++ Classes operations to form an object Figure 3-10 � an object is an instance of a class are encapsulated � a class defines a new data type � a class contains data members and methods (member functions) � by default, all data members in a class are private � but, can specify them as public � can only be accessed by other class members � some member functions have to be public � encapsulation hides implementation details EECS 268 Programming II 25 EECS 268 Programming II 26 C++ Classes C++ Classes: Constructors � Constructors � Each class definition is placed in a header file � create and initialize new instances of a class � Classname .h � invoked when you declare an instance of the class � have the same name as the class placed in an implementation file � have no return type, not even void � Classname .cpp � A class can have several constructors � a default constructor has no arguments � compiler will generate a default constructor if you do not define any constructors EECS 268 Programming II 27 EECS 268 Programming II 28

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend