日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++ 中的类模板

發(fā)布時(shí)間:2025/3/20 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ 中的类模板 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文連接:http://see.xidian.edu.cn/cpp/biancheng/view/213.html

有時(shí),有兩個(gè)或多個(gè)類,其功能是相同的,僅僅是數(shù)據(jù)類型不同,如下面語(yǔ)句聲明了一個(gè)類:
class Compare_int
{
? ?public :
? ?Compare(int a,int b)
? ?{
? ? ? x=a;
? ? ? y=b;
? ?}
? ?int max( )
? ?{
? ? ? return (x>y)?x:y;
}
int min( )
{
? ?return (x<y)?x:y;}
? ?private :
? ?int x,y;
};
其作用是對(duì)兩個(gè)整數(shù)作比較,可以通過(guò)調(diào)用成員函數(shù)max和min得到兩個(gè)整數(shù)中的大者和小者。

如果想對(duì)兩個(gè)浮點(diǎn)數(shù)(float型)作比較,需要另外聲明一個(gè)類:
class Compare_float
{
? ?public :
? ?Compare(float a,float b)
? ?{x=a;y=b;}
? ?float max( )
? ?{return (x>y)?x:y;}
? ?float min( )
? ?{return (x<y)?x:y;}
? ?private :
? ?float x,y;
}
顯然這基本上是重復(fù)性的工作,應(yīng)該有辦法減少重復(fù)的工作。

C++在發(fā)展的后期增加了模板(template )的功能,提供了解決這類問(wèn)題的途徑。可以聲明一個(gè)通用的類模板,它可以有一個(gè)或多個(gè)虛擬的類型參數(shù),如對(duì)以上兩個(gè)類可以綜合寫出以下的類模板:
template <class numtype> //聲明一個(gè)模板,虛擬類型名為numtype
class Compare //類模板名為Compare
{
? ?public :
? ?Compare(numtype a,numtype b)
? ?{x=a;y=b;}
? ?numtype max( )
? ?{return (x>y)?x:y;}
? ?numtype min( )
? ?{return (x<y)?x:y;}
? ?private :
? ?numtype x,y;
};

請(qǐng)將此類模板和前面第一個(gè)Compare_int類作一比較,可以看到有兩處不同:

  • 聲明類模板時(shí)要增加一行
    ? ?template <class 類型參數(shù)名>
  • 原有的類型名int換成虛擬類型參數(shù)名numtype。
    在建立類對(duì)象時(shí),如果將實(shí)際類型指定為int型,編譯系統(tǒng)就會(huì)用int取代所有的numtype,如果指定為float型,就用float取代所有的numtype。這樣就能實(shí)現(xiàn)“一類多用”。

  • 由于類模板包含類型參數(shù),因此又稱為參數(shù)化的類。如果說(shuō)類是對(duì)象的抽象,對(duì)象是類的實(shí)例,則類模板是類的抽象,類是類模板的實(shí)例。

    利用類模板可以建立含各種數(shù)據(jù)類型的類。在聲明了一個(gè)類模板后,怎樣使用它?怎樣使它變成一個(gè)實(shí)際的類?
    先回顧一下用類來(lái)定義對(duì)象的方法:
    ? ?Compare_int cmp1(4,7); // Compare_int是已聲明的類
    用類模板定義對(duì)象的方法與此相似,但是不能直接寫成
    ? ?Compare cmp(4,7); // Compare是類模板名
    ? ?Compare是類模板名,而不是一個(gè)具體的類,類模板體中的類型numtype并不是一個(gè)實(shí)際的類型,只是一個(gè)虛擬的類型,無(wú)法用它去定義對(duì)象。
    必須用實(shí)際類型名去取代虛擬的類型,具體的做法是:
    ? ?Compare <int> cmp(4,7);
    即在類模板名之后在尖括號(hào)內(nèi)指定實(shí)際的類型名,在進(jìn)行編譯時(shí),編譯系統(tǒng)就用int取代類模板中的類型參數(shù)numtype,這樣就把類模板具體化了,或者說(shuō)實(shí)例化了。這時(shí)Compare<int>就相當(dāng)于前面介紹的Compare_int類。

    例9.14是一個(gè)完整的例子。
    例9.14 聲明一個(gè)類模板,利用它分別實(shí)現(xiàn)兩個(gè)整數(shù)、浮點(diǎn)數(shù)和字符的比較,求出大數(shù)和小數(shù)。
    #include <iostream>
    using namespace std;
    template <class numtype>
    //定義類模板
    class Compare
    {
    ? ?public :
    ? ?Compare(numtype a,numtype b)
    ? ?{x=a;y=b;}
    ? ?numtype max( )
    ? ?{return (x>y)?x:y;}
    ? ?numtype min( )
    ? ?{return (x<y)?x:y;}
    ? ?private :
    ? ?numtype x,y;
    };
    int main( )
    {
    ? ?Compare<int > cmp1(3,7);//定義對(duì)象cmp1,用于兩個(gè)整數(shù)的比較
    ? ?cout<<cmp1.max( )<<″ is the Maximum of two integer numbers.″<<endl;
    ? ?cout<<cmp1.min( )<<″ is the Minimum of two integer numbers.″<<endl<<endl;
    ? ?Compare<float > cmp2(45.78,93.6); //定義對(duì)象cmp2,用于兩個(gè)浮點(diǎn)數(shù)的比較
    ? ?cout<<cmp2.max( )<<″ is the Maximum of two float numbers.″<<endl;
    ? ?cout<<cmp2.min( )<<″ is the Minimum of two float numbers.″<<endl<<endl;
    ? ?Compare<char> cmp3(′a′,′A′); //定義對(duì)象cmp3,用于兩個(gè)字符的比較
    ? ?cout<<cmp3.max( )<<″ is the Maximum of two characters.″<<endl;
    ? ?cout<<cmp3.min( )<<″ is the Minimum of two characters.″<<endl;
    ? ?return 0;
    }
    運(yùn)行結(jié)果如下:
    7 is the Maximum of two integers.
    3 is the Minimum of two integers.
    93.6 is the Maximum of two float numbers.
    45.78 is the Minimum of two float numbers.
    a is the Maximum of two characters.
    A is the Minimum of two characters.

    還有一個(gè)問(wèn)題要說(shuō)明: 上面列出的類模板中的成員函數(shù)是在類模板內(nèi)定義的。如果改為在類模板外定義,不能用一般定義類成員函數(shù)的形式:
    ? ?numtype Compare::max( ) {…} //不能這樣定義類模板中的成員函數(shù)
    而應(yīng)當(dāng)寫成類模板的形式:
    ? ?template <class numtype>
    ? ?numtype Compare<numtype>::max( )
    ? ?{{return (x>y)?x:y;}

    歸納以上的介紹,可以這樣聲明和使用類模板:

  • 先寫出一個(gè)實(shí)際的類。由于其語(yǔ)義明確,含義清楚,一般不會(huì)出錯(cuò)。
  • 將此類中準(zhǔn)備改變的類型名(如int要改變?yōu)閒loat或char)改用一個(gè)自己指定的虛擬類型名(如上例中的numtype)。
  • 在類聲明前面加入一行,格式為
    template <class 虛擬類型參數(shù)>,如
    template <class numtype> //注意本行末尾無(wú)分號(hào)
    class Compare
    {…}; //類體
  • 用類模板定義對(duì)象時(shí)用以下形式:
    ? ?類模板名<實(shí)際類型名> 對(duì)象名;
    ? ?類模板名<實(shí)際類型名> 對(duì)象名(實(shí)參表列);

    ? ?Compare<int> cmp;
    ? ?Compare<int> cmp(3,7);
  • 如果在類模板外定義成員函數(shù),應(yīng)寫成類模板形式:
    ? ?template <class 虛擬類型參數(shù)>
    ? ?函數(shù)類型 類模板名<虛擬類型參數(shù)>::成員函數(shù)名(函數(shù)形參表列) {…}

  • 關(guān)于類模板的幾點(diǎn)說(shuō)明:

  • 類模板的類型參數(shù)可以有一個(gè)或多個(gè),每個(gè)類型前面都必須加class,如
    template <class T1,class T2>
    class someclass
    {…};
    在定義對(duì)象時(shí)分別代入實(shí)際的類型名,如
    ? ?someclass<int,double> obj;
  • 和使用類一樣,使用類模板時(shí)要注意其作用域,只能在其有效作用域內(nèi)用它定義對(duì)象。
  • 模板可以有層次,一個(gè)類模板可以作為基類,派生出派生模板類。
  • 總結(jié)

    以上是生活随笔為你收集整理的C++ 中的类模板的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。