1. Engineering
  2. Computer Science
  3. stack class implement template stack class that is capable of...

Question: stack class implement template stack class that is capable of...

Question details

stack class Implement template stack class that is capable of storing integer and string values. Write both header and implementation in one file (stack.h) Private data fields: data: An array with maximum size of 20. (Declare a constant in stack.h called MAX_SIZE and set it to 20.) size: stores the current number of elements in the stack. Public interface: stack0: constructs an empty stack. push(T val): inserts a new element (val) of type T (T could be integer or string) into the data. If the data array is full, this function should throw an overflow_error exception with error message Called push on full stack. pop0: removes the last element from data. If the data array is empty, this function should throw an outofrange exception with error message Called pop on empty stack. top0: returns the top element of stack (last inserted element). If stack is empty, this function should throw an underflow_error exception with error message Called top on empty stack.. empty0: returns true if the stack is empty otherwise it returns false. main.cpp test harness Use this main.cpp file for testing your stackUse this main.cpp file for testing your stack. #include #include #include #include <iostream> <string> stackh <stdexcept? using namespace std,; int main () cout << Enter a test number (1-5): ; int test; cin >> test; cout << endl; //tests constructor, push, pop, top and empty if (test1) tryf cout << nstackl constructor called stack<int> stack1 ; if (stackl.empty)) cout<< stackl is empty. else cout<< stackl is not empty; cout << Inpush 10; stackl.push( 10) cout <<npush 20; stackl.push 20 cout << npush 30; stackl.push 30) cout <<nstackl top:; coutくくstack1 .top () ; cout <<npop stackl.pop cout<< nstackl top: coutくくstack! . top (); cout << pop; stackl.pop ) cout <<nstackl top: cout«くstackl .top () ; cout<<npop; stackl.pop ); if (stackl.empty)) cout<< stackl is empty. elsef cout<< stackl is not empty; cout << endl;
catch (underflow error & e)( coutくくe. what ( }くくendl; catch (overflow error & e) [ cout<<e.what () <<endl; catch (out_of_range&e)f coutくくe . what ( }くくendl ; //tests top on empty stack if (test-2) { try cout << stack2 constructor called; stack<int> stack2; cout << stack2 top:; coutくくstack2. top () ; cout << endl; catch (underflow error & e) ( cout<<e. what ( } <<endl ; catch (overflow error & e) coutくくe . what ( }くくendl ; catch (out of range & e) cout<<e.what ) <<endl; //tests pop from an empty stack if (test 3) { tryf cout <<nstack3 constructor called; stack<int> stack3; cout<<npop from empty stack ; stack3.pop ) cout << endl; catch (underflow error & e) f coutくくe. what ( }くくendl; catch (overflow error & e) [ cout<<e.what () <<endl; catch (out_of_range & e) f coutくくe . what ( }くくendl ; //tests push to a full stack if (test-4) { try cout << stack4 constructor called; stack<int> stack4; cout << push 20 elements; for (int i-1; i <-20; ++i)
for (int i = 1; stack4.push ); i <-20; ++i)( cout<<nstack4 top: ; coutくくstack4. top ; cout<<npush 21 ; stack4.push (21); cout < endl; catch (underflow error & e)1 coutくくe. what ( )くくendl; catch (overflow error & e) cout<<e.what )<<endl; catch (out_of_range & e) I cout<<e.what )<<endl; //tests stack of strings if (test5) tryl cout <<nstack5 constructor called stack<string> stack5 cout <<npush A; stack5.push (A) cout <<npush B; stack5.push (B cout << npush C stack5.push (C cout <<nstack5 top: ; cout<<stack5.top cout << npop stack5.pop cout <<nstack5 top: cout<<stack5.top cout <<npop stack5.pop ) cout << nstack5 top: cout<<stack5.top) cout << npop stack5.pop if (stack5.empty) cout<<Instack5 is empty. else f cout<<nstack5 is not empty; cout << nstack5 top: stack5.top cout << endl; catch (underflow error& e) cout<<e.what )<<endl; catch (overflow error & e) cout<<e.what )<<endl; catch (out of_range & e) I cout<<e.what )<endl; return 0;

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution