Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Container Class A Collection of Data. What is a Container Class?  A class that can contain a collection of items  A list or bag of items of the.

Similar presentations


Presentation on theme: "The Container Class A Collection of Data. What is a Container Class?  A class that can contain a collection of items  A list or bag of items of the."— Presentation transcript:

1 The Container Class A Collection of Data

2 What is a Container Class?  A class that can contain a collection of items  A list or bag of items of the same data type  Implementation of the container is encapsulated  Members of the container class provide member functions needed to handle the container data.  Client does not have to deal directly with the data structure being used in the container

3 Logical Implementations  Two logical implementations of containers  Array-based implementations  Objects in the container are kept in an array  Linked list-based implementations  Objects in the container are kept in a linked list  Member Functions should logically include:  Add item  Remove item  Get next item  Query state of container  Total size  Total capacity

4 Array-Based Implementation  Set initial state: constructor  Add item: adds a new item into the container  Remove item: removes an index item matching a search pattern  Get next item: reveals the current index + 1  Query state of container  Total Size Used: last valid array index containing data  Total Capacity: valid array indexes range from [0] to array length -1

5 Array Implementation  Contiguous memory  Direct access by index  Must declare extra variables to track size and capacity  C++ raw array doesn’t know its own size  Index arithmetic to move forward or backward  Will follow index to spot in memory, even if it is out of bounds  Insert/delete operations easiest at the end of the array  Difficult to make an array larger  Can’t just tack on more space at the end

6 Track Array Properties  Size: initial size of the array.  This is usually the total number of spaces allotted  Used: number of index slots currently holding data

7 Track Array Properties  Operations that change the array should also track size and used properties  Insert new_entry to the array data, then add one to member variable used data[used] = new_entry; used++; …or… data[used++] = new_entry;

8 Difficult to Add to the Beginning of an Array  Easiest to add data to the END of an array  May need to insert into middle or beginning of an array if the data must be sorted  To add data at the beginning of an array:  Move each existing data element over to make space  Be careful not to overwrite data

9 How Client Uses Your Container Class  #include “mybag.h”  Create an object of the bag type  Class name is the data type of the object  Call functions to handle the data in the bag using object name with dot operator


Download ppt "The Container Class A Collection of Data. What is a Container Class?  A class that can contain a collection of items  A list or bag of items of the."

Similar presentations


Ads by Google