Question
Resizable Array Stacks in C++ Part 1 - Declare the ResizableArrayStack class Create a header file named ResizableArrayStack.h . Add header comments similar to what
Resizable Array Stacks in C++
Part 1 - Declare the ResizableArrayStack class
- Create a header file namedResizableArrayStack.h.
- Add header comments similar to what you find incsc232.h.
- Create a macro guard blockwhich will encapsulate your ResizableArrayStack class declaration.
4. #ifndef RESIZABLE_ARRAY_STACK_H__
5. #define RESIZABLE_ARRAY_STACK_H__
6.
#endif // RESIZABLE_ARRAY_STACK_H__
- Within your macro guard block,#include \"StackInterface.h.
- Write your ResizableArrayStack class declaration such that it implements the StackInterface. Be sure to declare all the methods you're inheriting from the StackInterface as an override and add any and all appropriate Doxygen comments.
Part 2 - Define the ResizableArrayStack class
- Create an implementation file namedResizableArrayStack.cpp.
- Add header comments, again using the format prescribed incscs232.h.
- After your header comments,#include \"ResizableArrayStack.hand then go on to define all your methods. Most of these methods will be exactly the same as what you find for the ArrayStack class
//csc232.h
#ifndef CSC232_H__
#define CSC232_H__
#define FALSE 0
#define TRUE 1
#define EXECUTE_BLOCK FALSE
#include
#include
#include
#include
#include
#include
#include
#include \"expanded_templates.h\"
/** Common iostream objects */
using std::cout;
using std::cerr;
using std::cin;
using std::endl;
/** Common iomanip objects */
using std::setw;
using std::right;
using std::left;
using std::setprecision;
/** TODO: Add any new header files you create below */
#include \"StackInterface.h\"
#endif
//StackInterface.h
#ifndef CSC232_STACKINTERFACE_H__
#define CSC232_STACKINTERFACE_H__
template
class StackInterface
{
public:
virtual bool isEmpty() const = 0;
virtual bool push(const ItemType& newEntry) = 0;
virtual bool pop() = 0;
virtual ItemType peek() const = 0;
virtual ~StackInterface() = default;
};
#endif
Dynamic Allocation of Arrays
Declare an array in C++ by using statements such as
const int MAX_SIZE = 50;
double myArray[MAX_SIZE];
the compiler reserves a specific number -- MAX_SIZE, in this case -- of memory cells for the array. You actually can allocate memory for many at one time. If you write
int arraySize = 50;
double* anArray = new double[arraySize];
the pointer variable anArray will point to the first item in an array of 50 items. Unlike MAX_SIZE, you can assign a value to arraySize at execution time and thus determine how large you array is.
Regardless of how you allocate an array--statically, as in the first example, or dynamically, as in the second--you can use an index and the familiararray notation to access its elements. For example, anArray[0] and anArray[1] are the first two items in the array anArray.
When you allocate an array dynamically, you need to return its memory cells to the system when you no longer need them. To deallocate the array anArray, you write
delete [ ] anArray;
The following statements double the size of anArray:
double* oldArray = anArray; // copy pointer to array
anArray = new double[2 * arraySize]; // double array size
for (int index = 0; index
{
// copy the old (original) array into the newly
// allocated block that is double the size of the
// originally allocated block
anArray[index] = oldArray[index];
}
delete [ ] oldArray;
Resizable Array Stacks in C++
Part 1 - Declare the ResizableArrayStack class
- Create a header file namedResizableArrayStack.h.
- Add header comments similar to what you find incsc232.h.
- Create a macro guard blockwhich will encapsulate your ResizableArrayStack class declaration.
4. #ifndef RESIZABLE_ARRAY_STACK_H__
5. #define RESIZABLE_ARRAY_STACK_H__
6.
#endif // RESIZABLE_ARRAY_STACK_H__
- Within your macro guard block,#include \"StackInterface.h.
- Write your ResizableArrayStack class declaration such that it implements the StackInterface. Be sure to declare all the methods you're inheriting from the StackInterface as an override and add any and all appropriate Doxygen comments.
Part 2 - Define the ResizableArrayStack class
- Create an implementation file namedResizableArrayStack.cpp.
- Add header comments, again using the format prescribed incscs232.h.
- After your header comments,#include \"ResizableArrayStack.hand then go on to define all your methods. Most of these methods will be exactly the same as what you find for the ArrayStack class
//csc232.h
#ifndef CSC232_H__
#define CSC232_H__
#define FALSE 0
#define TRUE 1
#define EXECUTE_BLOCK FALSE
#include
#include
#include
#include
#include
#include
#include
#include \"expanded_templates.h\"
/** Common iostream objects */
using std::cout;
using std::cerr;
using std::cin;
using std::endl;
/** Common iomanip objects */
using std::setw;
using std::right;
using std::left;
using std::setprecision;
/** TODO: Add any new header files you create below */
#include \"StackInterface.h\"
#endif
//StackInterface.h
#ifndef CSC232_STACKINTERFACE_H__
#define CSC232_STACKINTERFACE_H__
template
class StackInterface
{
public:
virtual bool isEmpty() const = 0;
virtual bool push(const ItemType& newEntry) = 0;
virtual bool pop() = 0;
virtual ItemType peek() const = 0;
virtual ~StackInterface() = default;
};
#endif
Dynamic Allocation of Arrays
Declare an array in C++ by using statements such as
const int MAX_SIZE = 50;
double myArray[MAX_SIZE];
the compiler reserves a specific number -- MAX_SIZE, in this case -- of memory cells for the array. You actually can allocate memory for many at one time. If you write
int arraySize = 50;
double* anArray = new double[arraySize];
the pointer variable anArray will point to the first item in an array of 50 items. Unlike MAX_SIZE, you can assign a value to arraySize at execution time and thus determine how large you array is.
Regardless of how you allocate an array--statically, as in the first example, or dynamically, as in the second--you can use an index and the familiararray notation to access its elements. For example, anArray[0] and anArray[1] are the first two items in the array anArray.
When you allocate an array dynamically, you need to return its memory cells to the system when you no longer need them. To deallocate the array anArray, you write
delete [ ] anArray;
The following statements double the size of anArray:
double* oldArray = anArray; // copy pointer to array
anArray = new double[2 * arraySize]; // double array size
for (int index = 0; index
{
// copy the old (original) array into the newly
// allocated block that is double the size of the
// originally allocated block
anArray[index] = oldArray[index];
}
delete [ ] oldArray;
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