Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part A: Implement a container class called sequence. The items contained in a sequence are arranged one after the other, unlike a bag, which implies
Part A: Implement a container class called sequence. The items contained in a sequence are arranged one after the other, unlike a bag, which implies an unorganized arrangement of items. The class should include member functions to allow a program to step through a sequence one item at a time, as well as member functions to control exactly where items are inserted and removed in the sequence. The class should include a data member that keeps track of the current item in the sequence and member functions to set the current item to the first data member of the sequence and to advance the current itemto the next data member. If there is a current item, then that item is in data[current_index]. Otherwise, current_index equals used.
You are provided with the header file (see page 2 of this document). You will code the implementation and driver as specified.
Your class should include the following constant and 3 private data members:
An array (data) to store the data in the sequence with a maximum capacity of 10 items.
A constant (CAPACITY) to hold the maximum capacity of the sequence (physical size).
A variable (used) to keep track of the number of items currently in the sequence.
A variable (current_index) to hold the index of the current item in the sequence (if there is one, i.e. the end of sequence has been found out of bounds value).
Include the following features in your code:
Test your class with an interactive driver program that includes a menu feature to allow the user to choose operations to perform on an initially empty sequence and quit when desired. Also include a print function in your driver that will print the sequence in order (from position 0 through used -1) by calling the appropriate public member functions from the sequence class.
Break the program into 3 files- definition (.h), implementation(.cpp) and driver(.cpp).
Test your program with a sequence of type double and a sequence of char (you should only have to make one change in the typedef statement to do this).
Note: You need submit only one run to me.
All programs should include commenting, as well as indenting, spacing and variable/function names choices that improve readability. Submit a hard copy of all your code including output, and additionally email me all source and data files as attachments.
#ifndef SEQUENCE_H
#define SEQUENCE_H
template
class sequence
{
public:
sequence( ); // Constructor set used and current_index to 0
// MUTATOR MEMBER FUNCTIONS
//Postcondition: The first item in the sequence becomes the current item
void start( );
//Precondition: is_item returns true.
//Postcondition: If the current item was already the last item in the
//sequence, then there is no longer any current item. Otherwise, the new
//current item is the item immediately after the original current item.
void advance( );
//Precondition: size( ) < CAPACITY.
//Postcondition: A copy of entry has been inserted in the sequence
//before the current item. If there was no current item, then the new entry
//has been inserted at the front of the sequence (position 0). In either //case, the newly inserted item is now the current item of the sequence.
void insert(const data_type & entry);
//Precondition: size( ) < CAPACITY.
//Postcondition: A copy of entry has been inserted in the sequence
//after the current item. If there was no current item, then the new entry //has been attached to the end of the sequence. In either case, the newly
//inserted item is now the current item of the sequence.
void attach(const data_type & entry);
//Precondition: is_item returns true.
//Postcondition: The current item has been removed from the sequence, and //the item after this (if there is one) is now the new current item.
void remove_current( );
// ACCESSOR MEMBER FUNCTIONS
//Postcondition: The value returned is the number of items in the //sequence.
int size( ) const;
//Postcondition: A true return value indicates that there is a valid
//"current" item that may be retrieved by invoking the current
//member function below. A false return value indicates that
//there is no valid current item.
bool is_item( ) const{ return (current_index < used);}
//Precondition: is_item( ) returns true.
//Postcondition: The item returned is the current item in the sequence.
data_type current( ) const;
private:
static const int CAPACITY = 10;
data_type data[CAPACITY];
int used;
int current_index;
}; #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started