Question: C++ Programming I cant seem to get my program to compile, any help would be great! The question is You are creating a .h and
C++ Programming
I cant seem to get my program to compile, any help would be great!
The question is
You are creating a .h and a .cpp for OurStack, which uses the stl stack in its internal implementation. This should be a class using templates that can support multiple data types.
Create a .cpp file with a main that creates a 2 OurStacks holding different data types and pushes three items onto them and pops three items off of them. Demonstrate the other methods as well.
Note: Make sure you are inheriting from the abstract stack class defined in the chapter in code listing 6-1. You should include and use the stl stack class in your OurStack class via aggregation.
// StackInterface.h
#ifndef STACK_INTERFACE #define STACK_INTERFACE
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() { } };
#endif
// OurStack.h
#include "StackInterface.h" #include
template
class OurStack { private: stack
public: bool OurStack::isEmpty() { return OurStack.empty(): } bool OurStack::push(const ItemType& newEntry) { OurStack.push(newEntry);
if( OurStack.top() == newEntry) return true; else return false; }
bool OurStack:: pop() { if(!OurStack.empty()) { OurStack.pop(); return true; } else return false; }
ItemType OurStack::peek() { return OurStack.top(); } };
// OurStack.cpp
#include
using namespace std;
template
int main() { OurStack testStack; if(testStack.isEmpty()) cout << "The stack is empty" << endl; testStack.push(4); testStack.push(5); testStack.push(3);
cout << "Current top is: " << testStack.pop() << endl; cout << "Top popped! " << endl; cout << "Current top is: " << testStack.pop() << endl;
system ("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
