Asignmentcreate a class named safestack that implements a


Assignment:Create a class named SafeStack that implements a stack of strings. Use an instance of stack from 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.
 

#include
#include
#include
#include
using namespace std;
class FutureValueError
{
public:
    FutureValueError(const string& r);
    string& what();
private:
    string reason;
    
    };
    
    FutureValueError::FutureValueError(const string& r): reason(r) { }
   string& what()
   {
       return reason;
   }
class SafeStack
{
    
public:
   SafeStack();
   void push(string str);
   string top();
   void pop();
   int size();
   bool empty();  

private:
   stack stk;
};

SafeStack::SafeStack()
{}

void SafeStack::push(string str)
{
   stk.push(str);
}

string SafeStack::top()
{
   if(stk.empty())
       throw  FutureValueError ("stack is empty!");
   else
       return stk.top();
}

void SafeStack::pop()
{
   if(stk.empty())
       throw  FutureValueError ("stack is empty!");
   else
       stk.pop();
}

int SafeStack::size()
{
   return stk.size();
}

bool SafeStack::empty()
{
   return stk.empty();
}

int main ()

{
     SafeStack mystack;
     try
            
     {
      mystack.pop();   
     }
     catch (FutureValueError& e)
     {
         cout << "Caught exception: " << e.what() << "n";
         
     }
    
    return 0;
}

Solution Preview :

Prepared by a verified Expert
Basic Computer Science: Asignmentcreate a class named safestack that implements a
Reference No:- TGS0961545

Now Priced at $20 (50% Discount)

Recommended (93%)

Rated (4.5/5)