Download presentation
Presentation is loading. Please wait.
1
Array-Based Implementations
Chapter 3
2
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
3
Poor ADT Design Violating the wall of ADT operations by allowing client program to access data members of class directly.
4
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
5
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?
6
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.
7
Array-Based Implementation of ADT Sack
An array-based implementation of the ADT sack
8
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.
9
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
10
Defining the Core Methods
Go over method implementations
11
Defining the Core Methods
Inserting a new entry into an array-based sack
12
Methods That Remove Entries
The array items after a successful search for the string "Alice"
13
Methods That Remove Entries
A gap in the array items after the entry in items[index] is removed and decrementing itemCount;
14
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)
15
Methods That Remove Entries
Avoiding a gap in the array while removing an entry
16
Testing the Core Methods
Go over main implementation
17
End Chapter 3
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.