Programming in C++

jpFlute

SafeStack

Stack object data structures do not contain code to throw an exception when a stack.pop() function is called on an empty stack. This is due to the fact that it is easy to establish this exception handing elsewhere. Create a class named SafeStack that implements a stack of strings. 

  • Use an instance of stack from <string> to hold string values and implement the same interface as the data type. However, your implementation (class) should throw an exception if an attempt is made to remove a value from an empty stack.
  • See Chapter 17 (attached) with information for how to complete this assignment.

Submit a screenshot of the executed program and the code of the program.

 

Special requirements and notes from my Professor

 

Hello class,

 

I want to give some insight on the Week 7 assignment. The main purpose of that assignment is to add exception handling. Let's say you add 3 items to a vector and then you try to remove 4. It shouldn't let you do that. Rather, when you try to pop something from the vector after it's empty, you should throw an exception alerting the user. Obviously, this will be done using a try-catch block (see section 17.3). Also, look at section 12.4 on page 494. You can check the size of the stack and then throw the exception as needed. Better yet, see section 17.1 on page 666 and 667. It discusses stacks and potential issues that can arise. As far as what you need to push on the stack, it can be anything, such as your family members names or your favorite hobbies.

 

Here's the problem description once again:

 

One argument used to justify the fact that the stack data structure in the standard library does not throw exceptions is that it is easy to add these facilities. Create a class SafeStack that implements a stack of strings. Use an instance of stack<string> to hold the underlying values, and implement the same interface as the data type. However, your class should throw an exception if an attempt is made to remove a vlaue from an empty stack.

  • 10 years ago
  • 30
Answer(1)

Purchase the answer to view it

NOT RATED
  • exception.zip