1. Use a below-given starter code. Use StackInterface.(h), and ArrayStack.(h,cpp). You will write your own StackDriver.cpp application file. IT'S IN C++ Also... If the class that you create is Star, remember that you will declare your stack as follows: ArrayStack myStarStack; and you will push, pop, and peek Star objects. 2. Create your own class definition. I should have at least one attribute, a constructor, and set and get methods for each attribute in your class. NOTE: If your class is offensive your lab will be rejected and you will receive a score of zero. I'm sorry I have to say this but there is a history of this happening and it is not appropriate. 3. Write a driver program that exercises the Stack class from the downloaded starter code. Create multiple instances of your own class and demonstrate the stack operations: push, pop, and peek. ( you write your own class that you will put on the stack and you will write a driver that pushes, peeks, and pops your class items onto and off of the stack.) 4. Your driver should not require input from the user. All data should be hard-coded in the driver.
1. Use a below-given starter code. Use StackInterface.(h), and ArrayStack.(h,cpp). You will write your own StackDriver.cpp application file. IT'S IN C++
Also...
If the class that you create is Star, remember that you will declare your stack as follows:
ArrayStack<Star> myStarStack;
and you will push, pop, and peek Star objects.
2. Create your own class definition. I should have at least one attribute, a constructor, and set and get methods for each attribute in your class.
NOTE: If your class is offensive your lab will be rejected and you will receive a score of zero. I'm sorry I have to say this but there is a history of this happening and it is not appropriate.
3. Write a driver program that exercises the Stack class from the downloaded starter code. Create multiple instances of your own class and demonstrate the stack operations: push, pop, and peek. ( you write your own class that you will put on the stack and you will write a driver that pushes, peeks, and pops your class items onto and off of the stack.)
4. Your driver should not require input from the user. All data should be hard-coded in the driver.
ArrayStack.cpp
#include <cassert> // For assert
#include "ArrayStack.h" // Header file
template<class ItemType>
ArrayStack<ItemType>::ArrayStack() : top(-1)
{
} // end default constructor
// Copy constructor and destructor are supplied by the compiler
template<class ItemType>
bool ArrayStack<ItemType>::isEmpty() const
{
return top < 0;
} // end isEmpty
template<class ItemType>
bool ArrayStack<ItemType>::push(const ItemType& newEntry)
{
bool result = false;
if (top < MAX_STACK - 1) // Does stack have room for newEntry?
{
top++;
items[top] = newEntry;
result = true;
} // end if
return result;
} // end push
StackInterface.h
#ifndef _STACK_INTERFACE
#define _STACK_INTERFACE
template<class ItemType>
class StackInterface
{
public:
/** Sees whether this stack is empty.
@return True if the stack is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to the top of this stack.
@post If the operation was successful, newEntry is at the top of the stack.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful or false if not. */
virtual bool push(const ItemType& newEntry) = 0;
/** Removes the top of this stack.
@post If the operation was successful, the top of the stack
has been removed.
@return True if the removal is successful or false if not. */
virtual bool pop() = 0;
/** Returns the top of this stack.
@pre The stack is not empty.
@post The top of the stack has been returned, and
the stack is unchanged.
@return The top of the stack. */
virtual ItemType peek() const = 0;
}; // end StackInterface
#endif
template<class ItemType>
bool ArrayStack<ItemType>::pop()
{
bool result = false;
if (!isEmpty())
{
top--;
result = true;
} // end if
return result;
} // end pop
template<class ItemType>
ItemType ArrayStack<ItemType>::peek() const
{
assert(!isEmpty()); // Enforce precondition
// Stack is not empty; return top
return items[top];
} // end peek
// End of implementation file.
ArrayStack.h
#ifndef _ARRAY_STACK
#define _ARRAY_STACK
#include "StackInterface.h"
const int MAX_STACK = 50;
template<class ItemType>
class ArrayStack : public StackInterface<ItemType>
{
private:
ItemType items[MAX_STACK]; // Array of stack items
int top; // Index to top of stack
public:
ArrayStack(); // Default constructor
bool isEmpty() const;
bool push(const ItemType& newEntry);
bool pop();
ItemType peek() const;
}; // end ArrayStack
#include "ArrayStack.cpp"
#endif
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 7 images