Skip to content

Instantly share code, notes, and snippets.

@LizardLeliel
Created January 23, 2015 16:43
Show Gist options
  • Save LizardLeliel/9e868b04ed77efda2acb to your computer and use it in GitHub Desktop.
Save LizardLeliel/9e868b04ed77efda2acb to your computer and use it in GitHub Desktop.
Virtual Stack Things
#include <ostream>
#include <string>
#include "vStacks.h"
using namespace std;
int main(int argc, char** argv) {
virtualStack* thisThing = new spamNode("Hello");
virtualStack::push(thisThing, new spamNode("Goodbye"));
virtualStack::push(thisThing, new mathNode(1, 5));
virtualStack::push(thisThing, new spamNode("o_o"));
virtualStack::goDownAndDo(thisThing);
cin.ignore();
}
#ifndef V_STACK
#include <iostream>
#include <string>
// Base class
class virtualStack
{
public:
static void pop(virtualStack*& stack);
static void push(virtualStack*& stack, virtualStack* newNode);
static void goDownAndDo(virtualStack* theStack);
virtual ~virtualStack() = 0;
virtual void operator()() = 0;
protected:
virtualStack* next;
};
// Spam node
class spamNode: public virtualStack
{
public:
spamNode(const std::string& spamWord);
~spamNode();
void operator()();
private:
const std::string word;
};
// Math node
class mathNode: public virtualStack
{
public:
mathNode(const int a, const int b);
~mathNode();
void operator()();
private:
const int m;
const int n;
};
#define V_STACK
#endif
#include <string>
#include <iostream>
#include "vStacks.hpp"
// Base Class
virtualStack::~virtualStack()
{
}
void virtualStack::pop(virtualStack*& stack)
{
virtualStack* toPop = stack;
stack = stack->next;
delete toPop;
}
void virtualStack::push(virtualStack*& stack, virtualStack* newNode)
{
newNode->next = stack;
stack = newNode;
}
void virtualStack::goDownAndDo(virtualStack* theStack)
{
while (theStack)
{
(*theStack)();
theStack = theStack->next;
}
}
// Spam node
spamNode::spamNode(const std::string& spamWord):
word(spamWord)
{
next = nullptr;
}
spamNode::~spamNode()
{
}
void spamNode::operator()()
{
for (int i = 0; i < 10; ++i)
{
std::cout << word << " ";
}
std::cout << std::endl;
}
// math node
mathNode::mathNode(const int a, const int b):
m(a), n(b)
{
next = nullptr;
}
mathNode::~mathNode()
{
}
void mathNode::operator()()
{
std::cout << m + n << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment