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  

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.

CSE 202 Lab 9: Stacks, Templates, and Adapters Perform the following exercises ?1nder lab, aub directory. ometimeswe might even ueeda stack ofa class (typ) thatwehre alreacy defined ourselves such asthe uut class from lab 1. Stack can be defined z an adaper, for example, oe can dp ter to act as stack. Any coatainer class tha has the push bak and pup back) operations can be adapted to act as a stack. Here i one way to define stnck as a template claas and adapter of vecor ??nder STACK Adefine STACKH yor ni Stack. date escription using naspace std public: Stack): containr void push(T x? centainer.pushbuck(x); T topC) return c col epty return container.crpty): ntainer.back) is the template paruneter, it stands or Type and ir's the typwith hich the stack is instatiated. Type in the above code un Stack. // SL.ck-wa LspF detu // ?'aeripliu

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!