Comparison between files for a non-template and a template implementation of a stack. Corresponding files are listed in the left and right column. Line numbers (down the center) are given for lines on which the files differ. // File: main.cxx | | // File: main.cxx // | | // // Demo of a stack | 3| // Demo of stack template // | | // // CSCI 1300 | | // CSCI 1300 // Karl Winklmann | | // Karl Winklmann // Spring 2004 | | // Spring 2004 | | #include | | #include #include | | #include #include | | #include #include | | #include | | #include "stack.h" | | #include "stack.h" | | using namespace std; | | using namespace std; | | int main () | | int main () { | | { stack testStack; | 20| stack testStack; | | char inputChar; | | char inputChar; | | while (inputChar != 'q') | | while (inputChar != 'q') { | | { cout << "StackDemo> "; | | cerr << "StackDemo> "; cin >> inputChar; | | cin >> inputChar; | | switch (inputChar) | | switch (inputChar) { | | { case 't': cout << testStack.top () ... | | case 't': cerr << testStack.top () ... break; | | break; | | case 'u': cin >> inputChar; testSta... | | case 'u': cin >> inputChar; testSta... break; | | break; | | case 'o': if (!testStack.empty ()) ... | | case 'o': if (!testStack.empty ()) ... break; | | break; | | case 'q': return EXIT_SUCCESS; | | case 'q': return EXIT_SUCCESS; break; | | break; | | default: system ("grep case main.cx... | | default: system ("grep case main.cx... } | | } } | | } | | return EXIT_SUCCESS; | | return EXIT_SUCCESS; } | | } ================================================================================================================ // File: stack.h | | // File: stack.h // | | // // Demo of a stack | 3| // Demo of a stack template // | | // // CSCI 1300 | | // CSCI 1300 // Karl Winklmann | | // Karl Winklmann // Spring 2004 | | // Spring 2004 | | #ifndef STACK | | #ifndef STACK #define STACK | | #define STACK | | #include | | #include #include | | #include | | | 15| template class stackNode; | | class stackNode; | | | 18| template class stack | | class stack { | | { public: | | public: stack (); | | stack (); ~stack (); | | ~stack (); | | char top () const; | 25| Entry top () const; void push (char e); | 26| void push (Entry e); void pop (); | | void pop (); | | bool empty () const; | | bool empty () const; | | private: | | private: stackNode* topNode; | 32| stackNode* topNode; }; | | }; | | | 35| template class stackNode | | class stackNode { | | { friend class stack; | 38| friend class stack; | | public: | | public: stackNode ( | | stackNode ( char c = '?', | 42| Entry e, stackNode* b = NULL | | stackNode* b = NULL ); | | ); ~stackNode (); | | ~stackNode (); | | private: | | private: char entry; | 48| Entry entry; stackNode* below; | | stackNode* below; }; | | }; | | | 52| #include "stack.template" | | #endif | | #endif ================================================================================================================ // File: stack.cxx | 1| // File: stack.template // | | // // Demo of a stack | 3| // Demo of a stack template // | | // // CSCI 1300 | | // CSCI 1300 // Karl Winklmann | | // Karl Winklmann // Spring 2004 | | // Spring 2004 | | #include "stack.h" | 9| | | | 11| template stack::stack () | 12| stack::stack () { | | { topNode = NULL; | | topNode = NULL; } | | } | | | 17| template stack::~stack () | 18| stack::~stack () { | | { delete topNode; | | delete topNode; } | | } | | | 23| template char stack::top () const | 24| Entry stack::top () const { | | { assert (!empty ()); | | assert (!empty ()); return topNode->entry; | | return topNode->entry; } | | } | | | 30| template void stack::push (char e) | 31| void stack::push (Entry e) { | | { topNode = new stackNode (e, topNode); | 33| topNode = new stackNode (e, topNode); } | | } | | | 36| template void stack::pop () | 37| void stack::pop () { | | { assert (!empty ()); | | assert (!empty ()); | | stackNode* oldTopNode = topNode; | 41| stackNode* oldTopNode = topNode; topNode = topNode->below; | | topNode = topNode->below; | | oldTopNode->below = NULL; | | oldTopNode->below = NULL; delete oldTopNode; | | delete oldTopNode; } | | } | | | 48| template bool stack::empty () const | 49| bool stack::empty () const { | | { return topNode == NULL; | | return topNode == NULL; } | | } | | | 54| template stackNode::stackNode ( | 55| stackNode::stackNode ( char e, | 56| Entry e, stackNode* b = NULL | | stackNode* b = NULL ) | | ) { | | { entry = e; | | entry = e; below = b; | | below = b; } | | } | | | 64| template stackNode::~stackNode () | 65| stackNode::~stackNode () { | | { delete below; | | delete below; } | | } ================================================================================================================ # File: Makefile | | # File: Makefile # Stack demo | 2| # Stack template demo # | | # # CSCI 1300 | | # CSCI 1300 # Karl Winklmann | | # Karl Winklmann # Spring 2004 | | # Spring 2004 # | | # main: main.o stack.o | 8| main: main.o g++ -o main main.o stack.o | 9| g++ -o main main.o # | | # main.o: main.cxx stack.h | 11| main.o: main.cxx stack.h stack.template g++ -g -c -Wall main.cxx | | g++ -g -c -Wall main.cxx # | | # stack.o: stack.cxx stack.h | 14| # g++ -g -c -Wall stack.cxx | 15| # # | | # run: main | | run: main main | | main # | | # clean: | | clean: rm -f *.o *.class main main.exe | | rm -f *.o *.class main main.exe