Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone comment the two below files so that I can understand that is going on? I coded it out of my textbook but have

Can someone comment the two below files so that I can understand that is going on? I coded it out of my textbook but have yet been able to actually understand what is going on. Many Thanks!!

stackType.h

#ifndef H_StackType #define H_StackType

#include #include #include "stackADT.h" using namespace std; template class stackType : public stackADT { public: const stackType& operator=(const stackType&); void initializeStack(); bool isEmptyStack() const; bool isFullStack() const; void push(const Type& newItem); Type top() const; void pop(); stackType(int stackSize = 100); stackType(const stackType& otherStack); ~stackType(); private: int maxStackSize; //variable to store the maximum stack size int stackTop; //variable to point to the top of the stack Type *list; //pointer to the array that holds the //stack elements void copyStack(const stackType& otherStack); }; template void stackType::initializeStack() { stackTop = 0; }//end initializeStack template bool stackType::isEmptyStack() const { return (stackTop == 0); }//end isEmptyStack template bool stackType::isFullStack() const { return (stackTop == maxStackSize); } //end isFullStack template void stackType::push(const Type& newItem) { if (!isFullStack()) { list[stackTop] = newItem; //add newItem to the //top of the stack stackTop++; //increment stackTop } else cout << "Cannot add to a full stack." << endl; }//end push template Type stackType::top() const { assert(stackTop != 0); //if stack is empty, //terminate the program return list[stackTop - 1]; //return the element of the //stack indicated by //stackTop - 1 }//end top template void stackType::pop() { if (!isEmptyStack()) stackTop--; //decrement stackTop else cout << "Cannot remove from an empty stack." << endl; }//end pop template stackType::stackType(int stackSize) { if (stackSize <= 0) { cout << "Size of the array to hold the stack must " << "be positive." << endl; cout << "Creating an array of size 100." << endl; maxStackSize = 100; } else maxStackSize = stackSize; //set the stack size to //the value specified by //the parameter stackSize stackTop = 0; //set stackTop to 0 list = new Type[maxStackSize]; //create the array to //hold the stack elements }//end constructor template stackType::~stackType() //destructor { delete[] list; //deallocate the memory occupied //by the array }//end destructor template void stackType::copyStack(const stackType& otherStack) { delete[] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; //copy otherStack into this stack for (int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; } //end copyStack

template stackType::stackType(const stackType& otherStack) { list = nullptr; copyStack(otherStack); }//end copy constructor template const stackType& stackType::operator= (const stackType& otherStack) { if (this != &otherStack) //avoid self-copy copyStack(otherStack); return *this; } //end operator= #endif

stackADT.h

template class stackADT { public: virtual void initializeStack() = 0; virtual bool isEmptyStack() const = 0; virtual bool isFullStack() const = 0; virtual void push(const Type&) = 0; virtual Type top() const = 0; virtual void pop() = 0; };

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions

Question

What is DDL?

Answered: 1 week ago