Answered step by step
Verified Expert Solution
Link Copied!

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 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();
bool operator==(const stackType& otherStack) const;
private:
int maxStackSize;
int stackTop;
Type *list;
void copyStack(const stackType& otherStack);
};
template
void stackType::initializeStack()
{
stackTop =0;
}
template
bool stackType::isEmptyStack() const
{
return(stackTop ==0);
}
template
bool stackType::isFullStack() const
{
return(stackTop == maxStackSize);
}
template
void stackType::push(const Type& newItem)
{
if (!isFullStack())
{
list[stackTop]= newItem;
stackTop++;
}
else
cout << "Cannot add to a full stack." << endl;
}//end push
template
Type stackType::top() const
{
assert(stackTop !=0);
return list[stackTop -1];
}
template
void stackType::pop()
{
if (!isEmptyStack())
stackTop--;
else
cout << "Cannot remove from an empty stack." << endl;
}
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;
stackTop =0;
list = new Type[maxStackSize];
}
template
stackType::~stackType()
{
delete [] list;
}
template
void stackType::copyStack(const stackType& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
for (int j =0; j < stackTop; j++)
list[j]= otherStack.list[j];
}
template
stackType::stackType(const stackType& otherStack)
{
list = nullptr;
copyStack(otherStack);
}
template
const stackType& stackType::operator=
(const stackType& otherStack)
{
if (this != &otherStack)
copyStack(otherStack);
return *this;
}
#endif
stackADT.h
#ifndef H_StackADT
#define H_StackADT
template
class stackADT
{
public:
virtual void initializeStack()=0;
virtual bool isEmptyStack() const =0;
virtual bool isFullStack() const =0;
virtual void push(const Type& newItem)=0;
virtual Type top() const =0;
virtual void pop()=0;
};
#endif

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

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

=+a) Compute the EV for each alternative decision.

Answered: 1 week ago