ArrayBased Implementations Chapter 3 The Approach An ADT
Array-Based Implementations Chapter 3
The Approach • An ADT is o A collection of data … and … o A set of operations on that data • Specifications indicate o What ADT operations do o But not how to implement • First step for implementation o Choose data structure
Poor ADT Design • Violating the wall of ADT operations by allowing client program to access data members of class directly.
Core Methods • Poor approach o Define entire class and attempt to test • Better plan – Identify, then test basic (core) methods • Typical core methods o Create the container (constructors) o Setters o Getters o Add items o Display/list items o Remove items
Using Fixed-Size Arrays • Must keep track of array elements used, available • Consider if the add method places elements in consecutive positions of array • What happens when add method has used up final available position? • Where can elements be removed? • What happens when last element is removed?
Array-Based Implementation
Array-Based Implementation
Sack. Interface – Chapter 3 • Template class – supports generics, the class collection may hold any valid C++ type/class. • All methods are virtual allowing the derived class to override the method, thus late binding occurs at execution time not compile time – which method to call – the child’s or parent’s? • All methods are abstract = 0, no bodies meaning the class can not be instantiated, no objects of the class can be created. • The class is a pure abstract class, abstract if at least one method is virtual with an empty body, pure because all methods are defined as such (except destructor). • The class interface is used for inheritance of specification – all derived classes of the interface must include the interface methods.
Array. Sack ADT The header file for the class Array. Sack Code for header and implementation are available on lecture page of class web site • Notice use of template parameter T • Notice use of inheritance; Array. Sack is a child class of the Sack. Interface class • Inheritance is “public” – everything that is public in Base class is public in Derived class • Go over method specifications and data member declarations
Defining the Core Methods • Go over method implementations
Defining the Core Methods • Inserting a new entry into an array-based sack
Methods That Remove Entries • The array items after a successful search for the string "Alice"
Methods That Remove Entries • A gap in the array items after the entry in items[index] is removed and decrementing item. Count;
Methods That Remove Entries • Could shift subsequent entries to avoid a gap as in figure or simply copy last entry into gap (as in next slide)
Methods That Remove Entries • Avoiding a gap in the array while removing an entry
Testing the Core Methods • Go over main implementation
End Chapter 3
- Slides: 17