<IMG SRC="../images/misg.gif" WIDTH=267 HEIGHT=65 BORDER=0><> English Version   Kimo  Sina   Yahoo 

Search Engine | Seminar  | Training Courses  |  Home
Members | Medical Images | Statistical Computing | Research | Links
Bioinformatics | Group Meeting | Toolboxs
| Chinese Articles 
wpe12.jpg (961 bytes)
  Send comments or suggestions    
Updated:2000/10/25 since 1999.3.1

 

樣板 Templates
1. 什麼是樣板?

在C++中,樣板算是新的功能。它建立通用類別和函式,可處理多種不同的資料型態,而程式樣板將會在型態參數的值固定時轉成為一個完整的程式。

2. 例子說明(類別樣板)

test1.cpp

 

#include <iostream.h>
#include <stdlib.h>

template <class type>
class Cmax
{
public:
          type max(type x1, type x2);
};

template <class type>
type Cmax<type>::max(type x1, type x2)
{
    if(x1>x2){
       return(x1);
     }
     return(x2);
}

void main( )
{
     int answer1;
     double answer2;
     Cmax<int> obj1;
     Cmax<double> obj2;
     answer1=obj1.max(3, 5);
     answer2=obj2.max(4.5, 6.7);
     cout<<answer1<<endl;
     cout<<answer2<<endl;

    return;

}

      (1) <class type> 告訴C++,type可用任何型態取代。
      (2) 可將類別樣板想成

template <class type>
class Cname
{
public:
          type function(...);
};

template <class type>
type Cname<type>::function(...)
{
   ...
}

void main()
{
    Cname<int> obj;
    ...

   return;

}


                 
                 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

logo.gif (4473 bytes)

Copyright 1999 Medical Images-Stat. Group , NCTU-STAT.

Send comments or suggestions to u8626802@stat.nctu.edu.tw