Array-Based Implementations Chapter 3
The Approach An ADT is A collection of data … and … A set of operations on that data Specifications indicate What ADT operations do But not how to implement First step for implementation 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 Define entire class and attempt to test Better plan – Identify, then test basic (core) methods Typical core methods Create the container (constructors) Setters Getters Add items Display/list items 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 of ADT Sack Core ArraySack methods +getCurrentSize(): integer + isEmpty(): boolean +add(item: T): boolean +remove(item: T): boolean +clear(): void +contains(item: T) :boolean Also; Constructors (plus copy), assignment operator, destructor, iostream operator overloads.
Array-Based Implementation of ADT Sack An array-based implementation of the ADT sack
SackInterface – 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.
ArraySack ADT The header file for the class ArraySack Code for header and implementation are available on lecture page of class web site Notice use of template parameter T Notice use of inheritance; ArraySack is a child class of the SackInterface 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 itemCount;
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