Question
The StackForCS2420 class requires the following members: -A private unsigned int index data member, which keeps track of the next open index in the array.
The StackForCS2420 class requires the following members:
-A private unsigned int index data member, which keeps track of the next open index in the array. Default initialize it to 0.
-A private unsigned int capacity data member. Default initialize it to 0.
-A constructor that accepts a const unsigned int parameter. This constructor needs to dynamically allocate an array of the size passed into the parameter. Use the new keyword to make this array. It should also set the capacity data member to the value of the argument passed in.
-You need a destructor because you used the new keyword in the constructor. The destructor returns ownership of the array's memory space back to the operating system.
-A size() method. The return type is unsigned int. It returns the value of index.
-A push() method. This method should have a single parameter, the data type of that parameter should be const T&. The const means it can't be changed. The & means it will be passed in by reference. The push() method should have a void return value. This method should see if the index equals the capacity. If so, simply print to the console an error message and return. Otherwise, inset the value into the array at the correct spot, and increment index.
-A pop() method. This method should not have any parameter. The return type should be void.
-A top() method. This method should not have any parameters. The return type should be T. It should return what is at the top of the stack. It should first check if the size is zero. If so, then the stack is empty, so throw an error with throw std::out_of_range("some message"); Otherwise, return the correct value.
-A pushThirdFromTop() method. As the name implies, pops the item underneath the top two items. (Note, stacks aren't typically used this way, but the method is here to help you extend your understanding of both stacks and programming.)
-A pushTwoUnderTop() method.
-A topThirdFromTop() method.
--------------------------------------------------
#include | |
#include | |
#include | |
#include | |
using std::cout; | |
using std::cin; | |
using std::cerr; | |
using std::endl; | |
using std::string; | |
using std::out_of_range; | |
//These two base classes help the assignment compile and run without any changes. | |
//Dot not modify these. You will instead override the methods in the derived classes below. | |
template | |
class BaseStack { | |
public: | |
BaseStack() {} // These methods just contain filler code to help the code compile on the initial run. | |
BaseStack(const unsigned int size) {} // In the derived classes below, you will be overriding these with your own versions. | |
BaseStack(const BaseStack& objToClone) = delete; // Disables copy constructor | |
BaseStack operator=(const BaseStack& objToClone) = delete; // Disables copy assignment | |
unsigned int size() const { return 0; } // This method will be overriden in the derived class | |
void push(const T& item) {} // This method will be overriden in the derived class | |
T top() const { T temp{}; return temp; } // This method will be overriden in the derived class | |
void pop() { } // This method will be overriden in the derived class | |
void popThirdFromTop() { } // This method will be overriden in the derived class | |
void pushTwoUnderTop(const T& item) { } // This method will be overriden in the derived class | |
T topThirdFromTop() const { T temp{}; return temp; } // This method will be overriden in the derived class | |
protected: | |
T* arr{ nullptr }; | |
}; | |
//********************************** | |
//Write your code below here | |
//********************************** | |
template | |
class StackForCS2420 : public BaseStack | |
public: | |
StackForCS2420(const unsigned int capacity); | |
// TODO: Put your destructor and other methods here | |
private: | |
// TODO: Put your two data members here (the arr is in the base class) | |
}; | |
template | |
StackForCS2420 | |
// TODO: Write this | |
} | |
//TODO: Write all other methods | |
//********************************** | |
//Write your code above here | |
//********************************** |
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