Lab Assignment – (Lab#06) – Stack


The following code is an implementation of the stack operation. Fill in the appropriate code in the blanks and test your program with C++ compiler and run the program.

Hand in:

1) A Single zipped file which contains Source code and output:

(Lab6.cpp, Lab6.h, Lab6.doc (Capture your screen shot- Don’t type )

2) Upload two files to the Black board.


The elements of copyStack: 38 45 23

otherStack is not empty.

The top element of otherStack: 38

The original stack is not empty.

The top element of the original stack: 38

The elements of dummyStack: 38 45 23


//Print your Name, Section Name and ID

#include <iostream>

#include “Lab6.h”

using namespace std;

void testCopyConstructor(stackType<int> otherStack);

int main()


stackType<int> stack(50);

stackType<int> copyStack(50);

stackType<int> dummyStack(100);

//Initialize stack



//Push 38 to the stack

copyStack = stack; //copy stack into copyStack

cout << “The elements of copyStack: “;

while (!copyStack.isEmptyStack()) //print copyStack


cout << << ” “;



cout << endl;

copyStack = stack;

testCopyConstructor(stack); //test the copy constructor

if (!stack.isEmptyStack())

cout << “The original stack is not empty.” << endl

<< “The top element of the original stack: ”

<< << endl;

dummyStack = stack; //copy stack into dummyStack

cout << “The elements of dummyStack: “;

while (!dummyStack.isEmptyStack()) //print dummyStack


cout << << ” “;



cout << endl;


return 0;


void testCopyConstructor(stackType<int> otherStack)


if (!otherStack.isEmptyStack())

cout << “otherStack is not empty.” << endl

<< “The top element of otherStack: ”

<< << endl;


//Header file: myStack.h


//Print your Name, Section Name and ID

#ifndef H_StackType

#define H_StackType

#include <iostream>

#include <cassert>

using namespace std;

template <class Type>

class stackType



const stackType<Type>& operator=(const stackType<Type>&);

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<Type>& otherStack);



//Declare three private valuables for the class stackType

a. Declare a valuable named maxStackSize type of int

b. Declare a valuable named stackTop type of int

c. Declare a pointer named list to the array that holds the stack element

void copyStack(const stackType<Type>& otherStack);


template <class Type>

void stackType<Type>::initializeStack()


stackTop = 0;

}//end initializeStack

// Implement isEmptyStack operation

template <class Type>

bool stackType<Type>::isFullStack() const


return (stackTop == maxStackSize);

} //end isFullStack

template <class Type>

void stackType<Type>::push(const Type& newItem)


if (!isFullStack())


list[stackTop] = newItem; //add newItem to the

//top of the stack

stackTop++; //increment stackTop



cout << “Cannot add to a full stack.” << endl;

}//end push

template <class Type>

Type stackType<Type>::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

//Implement pop() operation

template <class Type>

stackType<Type>::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;



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

//Implement Destructor operation

template <class Type>

void stackType<Type>::copyStack(const stackType<Type>& 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 <class Type>

stackType<Type>::stackType(const stackType<Type>& otherStack)


list = nullptr;


}//end copy constructor

template <class Type>

const stackType<Type>& stackType<Type>::operator=

(const stackType<Type>& otherStack)


if (this != &otherStack) //avoid self-copy


return *this;

} //end operator=


Page 5 of 5