Question: y write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the

y write the following files:
mystack.h - contains the class definition for the mystack class.
mystack.cpp - contains the definitions for member functions of the mystack class.
These files are described in more detail below. The header file should contain header guards to
prevent it from being included multiple times in the same source file.
3.1. The mystack class
The mystack class represents a stack of characters implemented as an array. This stack will be
used in the second part of the assignment by an algorithm that converts an infix string to a postfix
string.
Like the other classes we've written this semester, this class should be implemented as two
separate files. The class definition should be placed in a header file called mystack.h.
Data Members
The mystack class should contain the following private data members:
a pointer to a char. I'll refer to this data member as the stack array pointer. It will be used to
dynamically allocate an array of char (the stack array).
a size_t variable used to keep track of the number of elements in the stack array. I'll refer
to this data member as the stack capacity.
a size_t variable used to track the number of values current stored in the stack array. I'll
refer to this data member as the stack size. The stack size must always be less than or
equal to the stack capacity. Unless the stack is empty, the top item in the stack is always
located in the stack array at location (stack size -1).
The data type size_t is defined in several header files, including and .
In addition to the data members described above, your class definition will need prototypes for the
public member functions described below.
Member Functions
The definitions for the member functions of the class should be placed in a separate source code
file called mystack.cpp. Make sure to #include "mystack.h" at the top of this file.
The mystack class should have the following member functions (most of which are quite small):
mystack::mystack()
This default constructor for the mystack class should initialize a new mystack object to
an empty stack. When the function ends:
o The stack size for the new object should be 0.
o The stack capacity for the new object should be 0.
o The stack array pointer should be nullptr.
mystack::mystack(const mystack& x)
This copy constructor for the mystack class should initialize a new mystack object to the
same values for all of its data members as the existing mystack object x. When the
function ends:
o The stack size for the new object should be equal to the stack size of the object x.
o The stack capacity for the new object should be equal to the stack capacity of the
object x.
o If the stack capacity is 0, the stack array pointer for the new object should be
nullptr. Otherwise, the stack array pointer should point to an array of char with a
number of elements equal to the stack capacity. The array should contain the same
values as the stack array of the object x. The contents of any array elements that are
not actually part of the stack are unimportant.
mystack::~mystack()
The destructor should delete the stack array.
mystack& mystack::operator=(const mystack& x)
This overloaded copy assignment operator should assign one mystack object (the object
x) to another (the object that called the member function, which is pointed to by this). The
state of the data members when the function ends should be same as described above for
the copy constructor.
size_t mystack::capacity() const
This member function should return the stack capacity.
size_t mystack::size() const
This member function should return the stack size.
bool mystack::empty() const
This member function should return true if the stack size is 0. Otherwise, it should return
false.
void mystack::clear()
This member function should set the stack size back to 0.
void mystack::reserve(size_t n)
This member function modifies an object's stack capacity without changing the stack size or
the contents of the stack array. The required logic is:
o If n is less than or equal to the current stack capacity, simply return.
o Set the stack capacity to n.
o Declare a temporary array pointer (a pointer to a char).
o Use the temporary array pointer to dynamically allocate an array of char. The
number of elements in the new temporary array should be equal to the stack
capacity.
o Copy the contents of the stack array into the temporary array.
o Delete the stack array.
o Set the stack array pointer to the temporary array pointer.
const char& mystack::top() const
This member function should return the top item in the stack. You may assume this function
will not be called if the stack is empty.
void mystack::push(char value)
This member function should push the character value onto the top of the stack. The
required logic is:
o If the stack size is equal to the stack capacity, the reserve() function

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!