a stack as a class : Your stack « Data Types « C++ Tutorial






#include <iostream>  
  using namespace std;  
  
  class Stack  {  
     private:  
        enum { MAX = 10 };   
        int st[MAX];         
        int top;             
     public:  
        Stack(){ top = 0; }  
        void push(int var) { st[++top] = var; }  
        int pop(){ return st[top--]; }  
  };  
  int main(){  
     Stack s1;  
    
     s1.push(11);  
     s1.push(22);  
     cout << "1: " << s1.pop() << endl;  //22  
     cout << "2: " << s1.pop() << endl;  //11  
     s1.push(33);  
     s1.push(44);  
     s1.push(55);  
     s1.push(66);  
     cout << "3: " << s1.pop() << endl;  //66  
     cout << "4: " << s1.pop() << endl;  //55  
     cout << "5: " << s1.pop() << endl;  //44  
     cout << "6: " << s1.pop() << endl;  //33  
     return 0;  
  }








2.41.Your stack
2.41.1.Declare your own class stack
2.41.2.Generic stack
2.41.3.generic stack implementation
2.41.4.a stack as a class
2.41.5.Stack implementation with constructor