Question: CSE 202 Lab 9: Stacks, Templates, and Adapters 1. Perform the following exercises under lab9 sub directory. 2. A stack is a template class. It

CSE 202 Lab 9: Stacks, Templates, and Adapters 1. Perform the following exercises under lab9 sub directory. 2. A stack is a template class. It is a template because we want to be able to define a stack of any type, similar to what we are used to when we declare a vector. For example, in one application we might need a stack of integers and in another application we might need a stack of strings. Sometimes we might even need a stack of a class (type) that we have already defined ourselves such as the Account class from lab 1. Stack can be defined as an adapter; for example, one can adapt vector to act as stack. Any container class that has the push_back() and pop_back() operations can be adapted to act as a stack. Here is one way to define stack as a template class and adapter of vector:

-----------------------------------------------

#ifndef STACK_H #define STACK_H // your name // Stack.h // date // description #include  using namespace std; template  class Stack { vector container; public: Stack(): container() {} void push(T x) { container.push_back(x); } void pop() { container.pop_back(); } T top() { return container.back(); } bool empty() { return container.empty(); } }; #endif

-------------------------------------------------

T is the template parameter, it stands for Type and it's the type with which the stack is instantiated. Type in the above code in Stack.h.

3. Test the correctness of your Stack using Stack_test.cpp

--------------------------------------------------

// your name // Stack_test.cpp // date // description #include  #include  #include "Stack.h" main() { Stack s1; s1.push(4); s1.push(3); s1.push(2); s1.push(1); while (!s1.empty()) { cout  s2; s2.push("Yoda said "); s2.push("something "); s2.push("to write "); while (!s2.empty()) { cout  

------------------------------

This program has instantiated a stack of integers called s1 and a stack of strings called s2. The template parameter T in s1 is int and in s2 is string. Again this is very similar to STL vector where template parameter T could be any type.

4. What would happen if we add

s2.pop();

or

cout

to the end of the program (when s2 is empty?)

Fix Stack.h so that these problems would not occur.

5. Hand in printouts of your final program and typescript of a sample run.

CSE 202 Lab 9: Stacks, Templates, and Adapters 1. Perform the following

exercises under lab9 sub directory. 2. A stack is a template class.It is a template because we want to be able to define

CSE 202 Lab 9: Stacks, Templates, and Adapters 1. Perform the following exerclses under lan sub directory. 2. A stack is ? template class. It is a template because we want to be able to define a stack of any type, similar to what we are used to when we declare a vector. For example, in one application we might need a s ack o ?nlugers and in ano her application we might need a stack u strings. Sorneumes we might even need d sla?k o a class ype uld we have a r adv defined ourselves such as lhe Account class rom ab stack can be defined as an adapter or example one?an adapt vector to act as stack ny?ontaner class that has the ? sh b"ck ] and pp bat ( ) Operations can be adapted orct as a stack Here is one way to define stack as a template class and adapter of vector #irnder 5TACK_A define STACKH your nane Stack.h date f descriptin Minclude evector using naespacetd tarplata typenana T vector container public Stack containert vaid push(Txi t container.push backix): vid popt i cantainer.pop hackO; T topo return centainer.hackO endif T is the template parameter it stands for Type and it's the type with which the stack is instantiate Type in the ahove code in srack.h. 3. Test the corectness of your stack using stack test.cpp your nan Stack tast.cPP date

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!