Create a class named safestack

Assignment Help Basic Computer Science
Reference no: EM13708280

Assignment: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.
 

#include <stack>
#include <string>
#include <iostream>
#include <stdexcept>
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<string> 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;
}

Reference no: EM13708280

Questions Cloud

What new threats do computer systems and networks pose : What new threats do computer systems and networks pose to personal privacy? Conversely, what threats are enabled or enhanced by computer systems and networks? How does cryptography help or hinder protection of privacy and public safety? What po..
Write the query to display the employee number : Write the query to display the employee number,last name,first name,salary "from" date,salary end date, and salary amount for employees 83731,83745 , and 84039.Sort the output by employee number and salary "from"date
Develop the flow diagram of the information : Develop the flow diagram of the information and any control elements needed to ensure proper access for the information.  Submit a 700- to 1,050-word section of the paper with the following elements:  A diagram of the information flow and any element..
How many streetlights are socially optimal : What is likely to happen to the overall level of neighborhood beautification and how many streetlights are socially optimal - Explain the trade-offs involved in this decision.
Create a class named safestack : 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 exceptio..
How many grams of copper nitrate would be produced : How many grams of copper (lll) nitrate would be produced from 4.30 g of copper metal reacting with excess nitric acid
An increase in the amount of h2o : In the following equilibrium, would you raise or lower the temperature to obtain these results? C2H2(g) + H2O(g) CH3CHO(g) delta H degrees= -151 kJ a. An increase in the amount of CH3CHO b. A decrease in the amount of C2H2 c. An incre..
Is effective leadership behavior universal : Is effective leadership behavior universal or does it vary from culture to culture? Please explain in some detail.
Reaction between aqueous mercury ion and complexing agent : Consider the following reaction between aqueous mercury ions and a complexing agent "RWW" also known as a chelating agent.Hg^2+ (aq) + 2RWW (aq) Hg(RWW)2^2+ (aq) Kf=7.91E10

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Html5 can validate weather an email address true or false

HTML5 can validate weather an email address entered by the user actually exists. True or False

  Where do othe researchers drop ff the ewaste

Where do othe researchers drop ff the ewaste and where does it end up?

  Which of these conditions returns true

Which of these conditions returns true? Check the Java documentation for the inheritance patterns.

  Businesses today are extremely reliant on large amounts

Businesses today are extremely reliant on large amounts of data for making intelligent business decisions. Likewise, the data warehouses are often structured in a manner that optimizes processing large amounts of data.

  Cost data incurred in processing lot quality cost data

What conclusions can be drawn from cost data incurred in processing lot, as found in the Excel workbook Quality Cost Data? Customer returns are simply scrapped and replaced.

  What functions does the ciso perform

What functions does the CISO perform? What are the key qualifications and requirements for the position?

  It support policies

IT Support Policies

  Facilitate data exchange functionality in a cloud service en

Database solutions today must be able to adapt and integrate among various computing applications. Determine at least three forms of database connectivity methods that can be used as an interface between applications. For each method you have ..

  Consider the following code fragment

Consider the following code fragment: for (int i = 0; i = 0; j--) { System.out.println(i + " " + j); } } How would i revise this fragment to output this: 0 0 1 1 1 0 2 2 2 1 2 0 3 3 3 2 3 1 3 0

  Recognize the make and model of one 1 laser printer and one

identify the make and model of one 1 laser printer and one 1 inkjet printer that are similarly priced. evaluate the

  Create a program to keep track of the participants

Create a program to keep track of the participants in a triathlon. Your program will keep track of the times in three events: Running, Swimming, and Biking.

  Write java code for your hello/loop

Write java code for your hello/loop

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd