Write a template that will create a static stack of any type. Demonstrate the cl
ID: 3627219 • Letter: W
Question
Write a template that will create a static stack of any type. Demonstrate the class with a driver program.Note: This code needs to be written in C++ Microsoft Visual Studio 2008 or 2010. I will only vote for full and working codes.
IntStack.h
-----------------------------------------------------------------------------------------------
class IntStack
{
private:
int *stackArray;
int capacity;
int top;
public:
IntStack(int capacity); // Constructor
~IntStack() { delete[] stackArray; }
void push(int value);
void pop(int &value);
bool isEmpty();
// Stack Exceptions
class Overflow {};
class Underflow {};
};
IntStack.cpp
--------------------------------------------------------------------------------------------------
#include "intstack.h"
//************************************
// Constructor *
//************************************
IntStack::IntStack(int capacity)
{
this->capacity = capacity;
stackArray = new int[capacity];
top = 0;
}
//***********************************
// Adds a value to the stack *
//***********************************
void IntStack::push(int value)
{
if (top == capacity) throw IntStack::Overflow();
stackArray[top] = value;
top++;
}
//****************************************
// Determines whether the stack is empty *
//****************************************
bool IntStack::isEmpty()
{
if (top == 0)
return true;
else
return false;
}
//************************************************
// Removes a value from the stack and returns it *
//************************************************
void IntStack::pop(int &value)
{
if (isEmpty()) throw IntStack::Underflow();
top --;
value = stackArray[top];
}
Pr 18-01
----------------------------------------------------------------------------------------------
#include "intstack.h"
#include <iostream>
using namespace std;
int main()
{
IntStack stack(5);
int values[] = {5, 10, 15, 20, 25};
int value;
cout << "Pushing... ";
for (int k = 0; k < 5; k++)
{
cout << values[k] << " ";
stack.push(values[k]);
}
cout << " Popping... ";
while (!stack.isEmpty())
{
stack.pop(value);
cout << value << " ";
}
cout << endl;
return 0;
}
Explanation / Answer
please rate - thanks
template <class T>
class Stack
{
private:
T *stackArray;
int capacity;
int top;
public:
Stack(int capacity); // Constructor
~Stack() { delete[] stackArray; }
void push(T value);
void pop(T &value);
bool isEmpty();
};// Stack Exceptions
class Overflow {};
class Underflow {};
//************************************
// Constructor *
//************************************
template <class T>
Stack<T>::Stack(int capacity)
{
this->capacity = capacity;
stackArray = new T[capacity];
top = 0;
}
//***********************************
// Adds a value to the stack *
//***********************************
template <class T>
void Stack<T>::push(T value)
{
//if (top == capacity) throw Stack<T>::Overflow();
stackArray[top] = value;
top++;
}
//****************************************
// Determines whether the stack is empty *
//****************************************
template <class T>
bool Stack<T>::isEmpty()
{
if (top == 0)
return true;
else
return false;
}
//************************************************
// Removes a value from the stack and returns it *
//************************************************
template <class T>
void Stack<T>::pop(T &value)
{
//if (isEmpty()) throw Stack<T>::Underflow();
top --;
value = stackArray[top];
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
Stack<int> istack(5);
Stack<double> dstack(5);
Stack<string> sstack(5);
Stack<char> cstack(5);
int values[] = {5, 10, 15, 20, 25};
int value;
double dvalues[] = {5.2, 10.4, 15.6, 20.3, 25};
double dvalue;
string svalues[] = {"abc", "def", "ghi", "jkl", "mno"};
string svalue;
char cvalues[] = {'a','b','c','d','e'};
char cvalue;
cout<<"Integers ";
cout << "Pushing... ";
for (int k = 0; k < 5; k++)
{
cout << values[k] << " ";
istack.push(values[k]);
}
cout << " Popping... ";
while (!istack.isEmpty())
{
istack.pop(value);
cout << value << " ";
}
cout << endl;
cout<<"Double ";
cout << "Pushing... ";
for (int k = 0; k < 5; k++)
{
cout << dvalues[k] << " ";
dstack.push(dvalues[k]);
}
cout << " Popping... ";
while (!dstack.isEmpty())
{
dstack.pop(dvalue);
cout << dvalue << " ";
}
cout << endl;
cout<<"Strings ";
cout << "Pushing... ";
for (int k = 0; k < 5; k++)
{
cout << svalues[k] << " ";
sstack.push(svalues[k]);
}
cout << " Popping... ";
while (!sstack.isEmpty())
{
sstack.pop(svalue);
cout << svalue << " ";
}
cout << endl;
cout<<"Characters ";
cout << "Pushing... ";
for (int k = 0; k < 5; k++)
{
cout << cvalues[k] << " ";
cstack.push(cvalues[k]);
}
cout << " Popping... ";
while (!cstack.isEmpty())
{
cstack.pop(cvalue);
cout << cvalue << " ";
}
cout << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.