欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

詳解C++編程中類模板的相關使用知識

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

有時,有兩個或多個類,其功能是相同的,僅僅是數(shù)據(jù)類型不同,如下面語句聲明了一個類:

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;
};

其作用是對兩個整數(shù)作比較,可以通過調(diào)用成員函數(shù)max和min得到兩個整數(shù)中的大者和小者。

如果想對兩個浮點數(shù)(float型)作比較,需要另外聲明一個類:

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;
}

顯然這基本上是重復性的工作,應該有辦法減少重復的工作。

C++在發(fā)展的后期增加了模板(template )的功能,提供了解決這類問題的途徑??梢月暶饕粋€通用的類模板,它可以有一個或多個虛擬的類型參數(shù),如對以上兩個類可以綜合寫出以下的類模板:

template <class numtype> //聲明一個模板,虛擬類型名為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;
};

請將此類模板和前面第一個Compare_int類作一比較,可以看到有兩處不同。

1) 聲明類模板時要增加一行

  template <class 類型參數(shù)名>


template意思是“模板”,是聲明類模板時必須寫的關鍵字。在template后面的尖括號內(nèi)的內(nèi)容為模板的參數(shù)表列,關鍵字class表示其后面的是類型參數(shù)。在本例中numtype就是一個類型參數(shù)名。這個名宇是可以任意取的,只要是合法的標識符即可。這里取numtype只是表示“數(shù)據(jù)類型”的意思而已。此時,mimtype并不是一個已存在的實際類型名,它只是一個虛擬類型參數(shù)名。在以后將被一個實際的類型名取代。

2) 原有的類型名int換成虛擬類型參數(shù)名numtype。
在建立類對象時,如果將實際類型指定為int型,編譯系統(tǒng)就會用int取代所有的numtype,如果指定為float型,就用float取代所有的numtype。這樣就能實現(xiàn)“一類多用”。

由于類模板包含類型參數(shù),因此又稱為參數(shù)化的類。如果說類是對象的抽象,對象是類的實例,則類模板是類的抽象,類是類模板的實例。利用類模板可以建立含各種數(shù)據(jù)類型的類。

那么,在聲明了一個類模板后,怎樣使用它呢?怎樣使它變成一個實際的類?

先回顧一下用類來定義對象的方法:

  Compare_int cmp1(4,7); // Compare_int是已聲明的類


其作用是建立一個Compare_int類的對象,并將實參4和7分別賦給形參a和b,作為進 行比較的兩個整數(shù)。

用類模板定義對象的方法與此相似,但是不能直接寫成

  Compare cmp(4,7); // Compare是類模板名

Compare是類模板名,而不是一個具體的類,類模板體中的類型numtype并不是一個實際的類型,只是一個虛擬的類型,無法用它去定義對象。必須用實際類型名去取代虛擬的類型,具體的做法是:

  Compare <int> cmp(4,7);


即在類模板名之后在尖括號內(nèi)指定實際的類型名,在進行編譯時,編譯系統(tǒng)就用int取代類模板中的類型參數(shù)numtype,這樣就把類模板具體化了,或者說實例化了。這時Compare<int>就相當于前面介紹的Compare_int類。

[例] 聲明一個類模板,利用它分別實現(xiàn)兩個整數(shù)、浮點數(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); //定義對象cmp1,用于兩個整數(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); //定義對象cmp2,用于兩個浮點數(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′); //定義對象cmp3,用于兩個字符的比較
  cout<<cmp3.max( )<<" is the Maximum of two characters."<<endl;
  cout<<cmp3.min( )<<" is the Minimum of two characters."<<endl;
  return 0;
}

運行結(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.

還有一個問題要說明: 上面列出的類模板中的成員函數(shù)是在類模板內(nèi)定義的。如果改為在類模板外定義,不能用一般定義類成員函數(shù)的形式:
  

 numtype Compare::max( ) {…} //不能這樣定義類模板中的成員函數(shù)

而應當寫成類模板的形式:

  template <class numtype>
  numtype Compare<numtype>::max( )
  {
    return (x>y)?x:y;
  }

上面第一行表示是類模板,第二行左端的numtype是虛擬類型名,后面的Compare <numtype>是一個整體,是帶參的類。表示所定義的max函數(shù)是在類Compare <numtype>的作用域內(nèi)的。在定義對象時,用戶當然要指定實際的類型(如int),進行編譯時就會將類模板中的虛擬類型名numtype全部用實際的類型代替。這樣Compare <numtype >就相當于一個實際的類。大家可以將例子改寫為在類模板外定義各成員 函數(shù)。

歸納以上的介紹,可以這樣聲明和使用類模板:
1) 先寫出一個實際的類。由于其語義明確,含義清楚,一般不會出錯。

2) 將此類中準備改變的類型名(如int要改變?yōu)閒loat或char)改用一個自己指定的虛擬類型名(如上例中的numtype)。

3) 在類聲明前面加入一行,格式為:
   

template <class 虛擬類型參數(shù)>

如:

  template <class numtype> //注意本行末尾無分號
  class Compare
  {…}; //類體

4) 用類模板定義對象時用以下形式:

  類模板名<實際類型名> 對象名;
  類模板名<實際類型名> 對象名(實參表列);


如:

  Compare<int> cmp;
  Compare<int> cmp(3,7);

5) 如果在類模板外定義成員函數(shù),應寫成類模板形式:

  template <class 虛擬類型參數(shù)>
  函數(shù)類型 類模板名<虛擬類型參數(shù)>::成員函數(shù)名(函數(shù)形參表列) {…}


關于類模板的幾點說明:
1) 類模板的類型參數(shù)可以有一個或多個,每個類型前面都必須加class,如:

  template <class T1,class T2>
  class someclass
  {…};


在定義對象時分別代入實際的類型名,如:

  someclass<int,double> obj;

2) 和使用類一樣,使用類模板時要注意其作用域,只能在其有效作用域內(nèi)用它定義對象。

3) 模板可以有層次,一個類模板可以作為基類,派生出派生模板類。有關這方面的知識實際應用較少,本教程暫不作介紹,感興趣的同學可以自行學習。

上一篇:詳解C++中基類與派生類的轉(zhuǎn)換以及虛基類

欄    目:C語言

下一篇:深入講解C++中的構(gòu)造函數(shù)

本文標題:詳解C++編程中類模板的相關使用知識

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2706.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有