#ifndef FALSE #define FALSE 0 #endif #ifndef NULL #define NULL 0 #endif template class Gstack { public: virtual void push(T) = 0; // pure virtual function virtual T pop() = 0; // pure virtual function virtual bool isempty() = 0; // pure virtual function virtual bool isfull() = 0; // pure virtual function }; #include template class Array_stack : public Gstack { int sz; int top; T* astack; public: Array_stack(int Nitems = 100) { sz = Nitems; astack = new T[sz]; top = sz; } ~Array_stack() { delete[] astack; } void push(T x) { assert( !isfull() ); astack[--top] = x; } T pop() { assert( !isempty() ); return( astack[top++] ); } bool isempty() { return( top == sz ); } bool isfull() { return( top == 0 ); } }; template class Link_stack : public Gstack { struct node { T data; node* next; node(T d, node* n) { data = d; next = n; } }; node* top; public: Link_stack( int Nitems = 100 ) { top = NULL; } void push(T x) { top = new node(x, top); } T pop() { assert( !isempty() ); T t = top -> data; node* oldtop = top; top = top -> next; delete oldtop; return t; } bool isempty() { return( top == NULL ); } bool isfull() { return( FALSE ); } }; #include //#include "Vstack.h" // Doesn't know about Arraystack or Linkedstack //void reverse( Gstack &, int ); // a prototype template void reverse( Gstack &s, int n ) { int i; for( i = 0; i < n; i++ ) { s.push((T)('a'+i)); } for( i = 0; i < n; i++ ) { cout << s.pop() << ' '; } cout << endl; } //#include "Vstack.h" //#include "Linkedstack.h" //#include "Arraystack.h" //extern void reverse( stack &s, int n ); int main() { Array_stack s1(500); Link_stack s2; // or s2(137); but not s2(); Array_stack s3; Link_stack s4(50); reverse (s1, 20); reverse (s2, 20); reverse (s3, 20); //problem reverse only knows about integers reverse (s4, 20); //need to make reverse a member function, perhaps? return 0; } /* //////////////Code Warrior 12 says://////////////// Warning : variable 'Nitems' is not used in function TStack.cc line 90 void main() Warning : variable 'Nitems' is not used in function TStack.cc line 90 void main() 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 t s r q p o n m l k j i h g f e d c b a t s r q p o n m l k j i h g f e d c b a */ /* /////////////g++ says////////////// TStack.cc:91: warning: return type for `main' changed to integer type TStack.cc:36: warning: `class Array_stack' has virtual functions but non-virtual destructor TStack.cc:36: warning: `class Array_stack' has virtual functions but non-virtual destructor TStack.cc: In function `int main(...)': TStack.cc:97: type unification failed for function template `template void reverse(Gstack<...> &, int)' TStack.cc:98: type unification failed for function template `template void reverse(Gstack<...> &, int)' TStack.cc:99: type unification failed for function template `template void reverse(Gstack<...> &, int)' TStack.cc:100: type unification failed for function template `template void reverse(Gstack<...> &, int)' TStack.cc: In method `Link_stack::Link_stack(int)': TStack.cc:49: warning: unused parameter `int Nitems' TStack.cc: In method `Link_stack::Link_stack(int)': TStack.cc:49: warning: unused parameter `int Nitems' */