QuestionQuestion

Problem Description
Your task is to write a template class to implement a unique stack data structure. In addition, the stack operations have to be monitored by using exception handling. That is, you need to protect the stack operations using the appropriate exception handling techniques.
You may read Chapter 18.1 Introduction to the Stack ADT to learn one possible implementation of a stack. The unique stack is a special kind of stack, which has unique items pushed onto it and has items popped off the top.
A unique stack may contain any data type. In the example above, the stack contains either integers or strings. A stack could contain floats, doubles, or even other classes (such as the Person class in Assignment 7).
Instead of writing individual classes and functions to handle each possible kind of unique stack, it is useful to create a generic template unique stack, define its type in the main code, and allow the compiler to provide the specific implementation at compile time.

Programming Tasks
Create a template unique stack class named TUStack, with at least the following public member functions:
TUStack(intnSize):
This is the constructor that initializes the maximum size of the stack. In other words, you must not hardcode the size of the stack! Each instance must have a dynamic stack size according to the value passed into this constructor.
void Push(T item):
This function places a new, unique item on the top of the stack. This new item should be different from any of the current items in the stack.
T Pop():
This function pops and returns the item on the top of the stack. Before returning, the Pop function should make sure that the second item on the stack is now the first.
int Size(): This function returns the maximum size of the stack.
int Position(): This function returns the current position of the stack pointer (i.e., the top of the stack).
operator[]: Overload the [] operator so that it returns a copy of the item located at a specified index. This operation is a read-only operation. That is, it does not affect the content in the stack.
In addition to the above required public member functions of your class, you must include exception handling to disallow any error conditions to occur in your code. Each exception class must allow the calling code (the code that uses the stack class) to obtaina detailed message about the nature of the error, the current stack size, the current position of the stack pointer, and the new input item if applicable. Here are a few exceptions you have to consider:
initialize the maximum size of the stack with a negative value;
push duplicate items;
push an item when the stack is full;
return a copy of the item at an illegal index (e.g., negative index and index above the maximum size of the stack);
pop value when the stack is empty.
The class obviously contains other private data and public functions.
Write driver code in your main program that clearly demonstrates all the required elements of this assignment. Specifically, your driver program must test two kinds of stack: a stack with integers and a stack with strings. Make sure to add appropriate comments to each of your test cases. It is your responsibility to prove to the grader your code is correct, not for him to guess if it worked correctly.

Good Practice Hint:
1. Design, implement, and test a stack with integers without considering exception handling.
2. Change the stack with integers to a stack with strings without considering exception handling.
3. Add the exception handling.
4. Modify the tested example to be a template

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

void integerTest(){
    int capcity = 10;
    TUStack<int> intStack(capcity);
    cout<<"Init stack with capacity of "<<capcity<<"\n\n";
    int i;
    try{
       cout<<"push item to stack\n\n";
       for (i = 1; i <= 11; i++) {
            intStack.Push(i);
            cout<<"i = "<<i<<" is entered the stack\n";
       }
    }catch (TUStack<int>::Overflow){
       cout << "\nAn Overflow exception occurred at i = "<<i<<".\n";
    }
   
    cout<<"\n\n";
    int item;
    try{
       cout<<"pop item to stack\n\n";
       while (true) {...
$30.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats