Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Problem Is this the proper way of using a pure virtual funtion and stack, and if so how can i get the program working?

C++ Problem

Is this the proper way of using a pure virtual funtion and stack, and if so how can i get the program working?

Thank You

//StackInterface.cpp

#include

#ifndef STACKINTERFACE_H

#define STACKINTERFACE_H

template

class StackInterface

{

public:

virtual bool isEmpty{} const = 0;

virtual bool push(const T& newEntry) = 0;

virtual bool pop() = 0;

virtual T peek() const = 0;

virtual ~StackInterface() {}

};

#endif

//OurStack.h

#include

#include

#ifndef OURSTACK_H

#define OURSTACK_H

#include "StackInterface.h"

template

class OurStack :public StackInterface

{

private:

stack OurStack;

public:

bool isEmpty() const; //Test if stack is empty

T& top();//Returns refference to top

bool pop(); // removes top of stack

bool push(const T& newEntry);

};

#include"OurStack.cpp"

#endif

//Ourstack.cpp

#include "OurStack.h"

#include

#include

template

OurStack::~OurStack()

{

}

template

bool OurStack::isEmpty() {

return OurStack.empty();

}

template

bool OurStack::push(const T& newEntry) {

OurStack.push(newEntry);

if (OurStack.top() == newEntry)

return true;

else

return false;

}

template

bool OurStack::pop() {

if (!OurStack.empty()) {

OurStack.pop();

return true;

}

else

return false;

}

template

T OurStack::top() {

return OurStack.top();

}

// Main.cpp

#include

#include

#include"OurStack.h"

#include"StackInterface.h"

using namespace std;

int main() {

OurStack testStack;

if (testStack.isEmpty) {

cout << "Empty Stack";

}

testStack.push(4);

testStack.push(5);

testStack.push(3);

testStack.push(2);

testStack.push(1);

cout << "Current Top to remove is: " << testStack.pop();

cout << " Current top now: " << testStack.top();

system("pause");

return 0;

}

Thank You!!!

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions