Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In c + + need help with main.cpp Instructions Two stacks of the same type are the same if they have the same number of
In c need help with main.cpp
Instructions
Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same.
Overload the relational operator for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise.
Also, write the definition of the function template to overload this operator.
Write a program to test the various overloaded operators and functions of class stackType.
main.cpp
#include
#include "myStack.h
using namespace std;
int main
return;
myStack.h
#ifndef HStackType
#define HStackType
#include
#include
#include "stackADT.h
using namespace std;
template
class stackType: public stackADT
public:
const stackType& operatorconst stackType&;
void initializeStack;
bool isEmptyStack const;
bool isFullStack const;
void pushconst Type& newItem;
Type top const;
void pop;
stackTypeint stackSize ;
stackTypeconst stackType& otherStack;
~stackType;
bool operatorconst stackType& otherStack const;
private:
int maxStackSize;
int stackTop;
Type list;
void copyStackconst stackType& otherStack;
;
template
void stackType::initializeStack
stackTop ;
template
bool stackType::isEmptyStack const
returnstackTop ;
template
bool stackType::isFullStack const
returnstackTop maxStackSize;
template
void stackType::pushconst Type& newItem
if isFullStack
liststackTop newItem;
stackTop;
else
cout "Cannot add to a full stack." endl;
end push
template
Type stackType::top const
assertstackTop ;
return liststackTop ;
template
void stackType::pop
if isEmptyStack
stackTop;
else
cout "Cannot remove from an empty stack." endl;
template
stackType::stackTypeint stackSize
if stackSize
cout "Size of the array to hold the stack must
be positive." endl;
cout "Creating an array of size endl;
maxStackSize ;
else
maxStackSize stackSize;
stackTop ;
list new TypemaxStackSize;
template
stackType::~stackType
delete list;
template
void stackType::copyStackconst stackType& otherStack
delete list;
maxStackSize otherStack.maxStackSize;
stackTop otherStack.stackTop;
list new TypemaxStackSize;
for int j ; j stackTop; j
listj otherStack.listj;
template
stackType::stackTypeconst stackType& otherStack
list nullptr;
copyStackotherStack;
template
const stackType& stackType::operator
const stackType& otherStack
if this &otherStack
copyStackotherStack;
return this;
#endif
stackADT.h
#ifndef HStackADT
#define HStackADT
template
class stackADT
public:
virtual void initializeStack;
virtual bool isEmptyStack const ;
virtual bool isFullStack const ;
virtual void pushconst Type& newItem;
virtual Type top const ;
virtual void pop;
;
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started