Question: // Specification file for the MathStack class #ifndef MATHSTACK_H #define MATHSTACK_H #include IntStack.h class MathStack : public IntStack { public: // Constructor MathStack(int s) :
// Specification file for the MathStack class
#ifndef MATHSTACK_H
#define MATHSTACK_H
#include "IntStack.h"
class MathStack : public IntStack
{
public:
// Constructor
MathStack(int s) : IntStack(s) {}
// MathStack operations
void add();
void sub();
};
#endif
MathStack.cpp
// Implementation file for the MathStack class
#include "MathStack.h"
//************************************************
// Member function add. add pops *
// the first two values off the stack and *
// adds them. The sum is pushed onto the stack. *
//************************************************
MathStack::add()
{
int num, sum;
// Pop the first two values off the stack.
pop(sum);
pop(num);
// Add the two values, store in sum.
sum += num;
// Push sum back onto the stack.
push(sum);
}
//***********************************************
// Member function sub. sub pops the *
// first two values off the stack. The *
// second value is subtracted from the *
// first value. The difference is pushed *
// onto the stack. *
//***********************************************
void MathStack::sub()
{
int num, diff;
// Pop the first two values off the stack.
pop(diff);
pop(num);
// Subtract num from diff.
diff = num;
// Push diff back onto the stack.
push(diff);
}
// This program demonstrates the MathStack class.
#include
#include "MathStack.h"
using namespace std;
int main()
{
int catchVar; // To hold values popped off the stack
// Create a MathStack object.
MathStack stack(5);
// Push 3 and 6 onto the stack.
cout << "Pushing 3 ";
stack.push(3);
cout << "Pushing 6 ";
stack.push(6);
// Add the two values.
stack.add();
// Pop the sum off the stack and display it.
cout << "The sum is ";
stack.pop(catchVar);
cout << catchVar << endl << endl;
// Push 7 and 10 onto the stack
cout << "Pushing 7 ";
stack.push(7);
cout << "Pushing 10 ";
stack.push(10);
// Subtract 7 from 10.
stack.sub();
// Pop the difference off the stack and display it.
cout << "The difference is ";
stack.pop(catchVar);
cout << catchVar << endl;
return 0;
}
1. Please Implement the above codes it as a dynamic stack (do not use a static stack as it is designed in the book).
2. Add functionality for Mult, Div, Pop, and Display.
3. Create a looping menu-driven program to demonstrate your code so that the user has the choice of:
1. Push (an integer onto the stack)
2. Pop (an integer off the stack)
3. Add (the top two values in the stack and replace those two values with the sum)
4. Subtract (the top two values on the stack and replace those two values with the difference)
5. Mult (same as above)
6. Div (same as above but check for div by 0)
7. Display Stack
8. End
have both high and lower-level validation. Watch out for division by 0 and not having enough integers on your stack to do the operation. Make sure to only allow integers on your stack. implement low-level errors (by returning a status flag, -1 if there's an issue). Finally, make sure main() is properly organized into functions and don't forget to document your code including your class.
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
