C++ stack storing integers, strings, pairs of numbers

Extend the improved stack class stack2 so that the derived class stack would be also capable to store integers, strings, or pairs of integers. Model the pair by a structure with two integer-valued fields x and y.

In the main program, define several integers, strings, and pairs and demonstrate pushing them to and popping from your stack.

© SolutionLibrary Inc. solutionlibary.com 9836dcf9d7 https://solutionlibrary.com/computer-science/software-development/c-stack-storing-integers-strings-pairs-of-numbers-7e86
Attachments

Solution Preview

...
struct Pair
{
int x;
int y;
};

class stack2 : public stack // improved stack
{
private:
string strs[MAX]; // stack array for strings
Pair pairs[MAX]; // stack array for integer pairs

public:
bool push(int n) // push now checks for stack overflow
{
if (top < MAX-1)
{
stack::push(n); // calling push() of superclass
return true;
}
else
return false;
}

bool pop(int &n) // new pop() prevents popping from empty stack
{
if (top >= 0)
{
n = stack::pop();
return true;
}
else
return false;
}

//push override for strings
bool push(string str)
{
if (top < MAX-1)
{
strs[++top] = str;
return true;
}
else
return false;
}
//pop override for strings
bool pop(string &str)
{
if (top >= 0)
{
str = (strs[top--]);
return ...