D:\test> linenum < mystk3.h 
   01 //mystk3.h
   02 #ifndef _MYSTK3_h_
   03 #define _MYSTK3_h_
   04 //a template class for a STACK, CopyLeft by tsaiwn@csie.nctu.edu.tw
   05 // 這範例是把之前的 STACK 範例重新寫成 template class(樣版類別);
   06 // 寫成非template 則會分 .h(宣告) 與 .cpp(實作);
   07 // 寫成 template 時只寫成一個 .h 檔;  //注意以下 template class 寫法! 
   08 // 除了在 class 之前要寫 template < class T>  之外,
   09 // 所有寫在 class{ }; 結構之外的函數的左邊(前面)也都要寫 template<class T>
   10 // 而且在 :: 之左也要寫 類別<T>, 例如 ... MyStack<GG>:: ...
   11 // 還有, function 內該換的 type 也別忘了換成 T
   12 /// 這個堆疊是用 array 做的, 但 C++ 程式庫的 <stack> 內部有三種選擇,
   13 /// 不指定則用 Deque (雙向 Queue), 可以指定用 <vector> 或 <list>
   14 /// Java 語言的 Stack 則是用 Vector 做的!
   15 template <class GG>
   16 class MyStack{
   17    public:
   18       enum{NELEMENT=99};
   19    private:
   20       GG data[NELEMENT];
   21       int stkptr;
   22    public:
   23       MyStack(void){ stkptr= -1; }
   24       void push(GG  x);
   25       void pop(void);
   26       GG top(void);
   27       int empty(void){ return (stkptr <= -1) ;}
   28       int isfull(void){ return (stkptr >= NELEMENT-1);}
   29 };
   30 template <class GG> GG MyStack<GG> :: top(void)
   31 {   // 注意上列的寫法 !
   32     if(stkptr > -1) return data[stkptr];    // 有防呆一下喔
   33 }
   34 template <class GG>
   35   void MyStack<GG> :: push(GG  x) { data[++stkptr] = x; }
   36 //Actually,  pop( ) function is a void function in C++ STL
   37 template <class GG>
   38   void MyStack<GG> ::pop(void) { if(stkptr > -1)  --stkptr; }
   39 #endif

D:\test> linenum < mymain3.cpp
     1 // mymain3.cpp;  g++ mymain3.cpp   ; ./a.out
     2 #include "mystk3.h"
     3 //注意以下 iostream.h 為舊的寫法; 1999之後的 C++ 使用 <iostream>
     4 #include <iostream>
     5 using namespace std;   // 新#include 的寫法要用到 namesapce std
     6 int main( ) {
     7    MyStack <int> x;
     8    MyStack <double> y;
     9    x.push(880);
    10    x.push(770);
    11    x.push(53);
    12    while(!x.empty()){
    13       cout << x.top();  y.push( x.top() + 0.5 );
    14       x.pop();
    15    }
    16    cout << endl << "=== === now dump y:\n";
    17    while(!y.empty()){
    18       cout << y.top() << " "; y.pop();
    19    }
    20    cout << "\n=== bye bye ===" << endl;
    21    return 0; // normal return
    22 }

D:\test> g++ mymain3.cpp -Wno-deprecated
D:\test> a.exe
53770880
=== === now dump y:
880.5 770.5 53.5 
=== bye bye ===

D:\test>