Objektorientierte Programmierung WS 2013/2014 - Datei stackloes.cc
#include <cassert> #include <iostream> using namespace std; template<class T> class simpleStack { public: static const int MAX_SIZE = 255; simpleStack() { numberofelems = 0; } bool empty() { return numberofelems == 0; } bool full() { return numberofelems == MAX_SIZE; } T* top(){ assert(!empty()); return array[numberofelems-1]; } void pop(){ assert(!empty()); --numberofelems; } void push(T *x) { assert(!full()); array[numberofelems++] = x; } private: int numberofelems; T* array[MAX_SIZE]; }; class Element { public: Element(int val) { value = val; } Element(){} void printMe() { cout << value << endl; } private: int value; }; class Test{ public: static void myMain () { simpleStack<Element> *myStack = new simpleStack<Element>(); myStack->push(new Element(5)); myStack->push(new Element(7)); myStack->push(new Element(9)); myStack->top()->printMe(); myStack->pop(); myStack->top()->printMe(); } }; int main(int argc, char ** argv) { Test::myMain(); }
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 19.11.2013