Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me break down this assinment please?! INSTRUCTIONS Note the following: The class and the methods have a template prefix Each time a

Can someone help me break down this assinment please?! INSTRUCTIONS Note the following:
The class and the methods have a template prefix
Each time a method is defined, the syntax contains a : className::member().
The getItem() method has a T return type
Instructions:
Complete all TODO sections, writing code for StackForCS2420.
For my unit tests, I made a base class with methods that simply have enough logic to compile. You should not
modify the base class. Instead, you should modify the derived class and override the constructor, destructor,
and all methods there.
The StackForCS2420 class requires the following members:
A private arr data member which is a pointer to type T. Default initialize it to nullptr. The constructor
will allocate the array and store the address in this data member.
A private int amount data member, which keeps track of the next open index in the array. Default
initialize it to 0.
A private int capacity data member. Default initialize it to 0.
A constructor that accepts a const 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 arrays memory space back to the operating system.
A size() method. The return type is int. It returns the value of amount.
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
(instead of by value, which makes a copy). The push() method should have a void return value. This
method should see if the amount equals the capacity (seeing if it is full). If so, simply print to the
console an error message and return. Otherwise, insert the value into the array at the correct spot, and
increment amount.
A pop() method. This method should not have any parameter. The return type should be void. The
purpose of the method is to pop the item off the stack. It doesn't actually pop the item off the array, it
just changes amount.
A top() method. This method should not have any parameter. The return type should be T. It should
return what is at the top of the stack. It should first check if 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 popThirdFromTop() method. As the name implies, pops the item underneath the top two items.
Ensure the code checks for any bad scenarios and returns if those are found. (Note, stacks typically
arent used this way, but this method is here to help you extend your understanding of both stacks and
programming.)
A pushTwoUnderTop() method. As the name implies, pushes an item under the top two items. Ensure the
code checks for any bad scenarios and returns if those are found.
A topThirdFromTop() method. As the name implies, retrieves the value of the item under the top two
items. Throws an error with throw std::out_of_range(some message); if this isnt possible.
Use the .cpp file given. Your assignment should pass all tests both locally and on GitHub. Submit with your
GitHub repo https address with /pull/1 appended CODE (stacks.hpp) #include
#include
#include
#include
using std::cout;
using std::cin;
using std::cerr;
using std::endl;
using std::string;
using std::out_of_range;
//This class helps the assignment compile and run without any changes.
//Dot not modify. 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 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
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 pushTwoUnderTop(const T& item){}// This method will be overriden in the derived class
T topThirdFromTop() const { T temp{}; return temp; }

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

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions