版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、2020年6月28日,1,第九章 Templates,2020年6月28日,2,一、模板函數(shù)的定義,模板函數(shù)即是利用模板的概念來編寫函數(shù),讓函數(shù)能夠以相同的代碼程序處理不同的數(shù)據(jù)類型。 定義格式如下: templateType fun(Type, Type,) 該定義格式有兩部分內(nèi)容: template /template的定義部分 關(guān)鍵字template表示:所定義的函數(shù)為模板函數(shù); 關(guān)鍵字class表示:模板函數(shù)接受的數(shù)據(jù)類型可以是任何數(shù)據(jù)類型,包括用戶定義的數(shù)據(jù)類型(不是類的概念); Type:template的形式參數(shù),表示模板函數(shù)的形式數(shù)據(jù)類型; class Type 為關(guān)鍵字tem
2、plate的參數(shù)行。,2020年6月28日,3,一、模板函數(shù)的定義, Type fun(Type, Type,) /函數(shù)定義部分 Type:為經(jīng)模板定義后形式數(shù)據(jù)類型,真正的數(shù)據(jù)類型含義取決于調(diào)用模板函數(shù)實參的數(shù)據(jù)類型。 編譯器決定Type的依據(jù)是當(dāng)調(diào)用函數(shù)時所給出的實在參數(shù)而非函數(shù)的返回值類型。,2020年6月28日,4,Prog9_1.cpp,templateX max(X var1,X var2) return(var1var2)? var1:var2; void main() int m1=max(100,200); double m2=max(35.5,65.6); char *m3
3、=max(Micro, Soft); cout The maxium of 100 and 200 is: m1endl; cout The maxium of 35.5 and 65.6 is: m2endl; cout The maxium of Micro and Soft is: m3endl; getch(); ,運算結(jié)果: The maxium of 100 and 200 is:200 The maxium of 35.5 and 65.6 is:65.6 The maxium of Micro and Soft is:Soft,2020年6月28日,5,Prog8_2.cpp,
4、template T sum(T *array, int size=0) T total=0; for(int i=0; isize;i+) total+=arrayi; return total; int intarray=1,2,3,4,5,6,7,8,9,10; double douarray=1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.0; int itotal=sum(intarray,10); double dtotal=sum(douarray,10); void main() cout The summary of integer arry a
5、re: itotalendl; cout The summary of double floating array are: dtotalendl; ,運行結(jié)果: The summary of integer arry are:55 The summary of double floating array are:59.5,2020年6月28日,6,二、template函數(shù)的重載,利用template函數(shù)的參數(shù)表不同便可以實現(xiàn)模板函數(shù)的重載。Overloding template不僅可以解決參數(shù)數(shù)據(jù)類型不同的問題,也解決了參數(shù)個數(shù)不同的問題。 如: templateType add(Type
6、a, Type b) templateType add(Type a, int b) templateType *add(Type a, char *b),2020年6月28日,7,Prog9_3.cpp,templateT sum(T *array,int size=0) static T total; for(int i=0;iT sum(T *array, T *array2, int size=0) static T total; for(int i=0; isize;i+) total+=arrayi+array2i; return total; ,2020年6月28日,8,int
7、intarr1=1,2,3,4,5,6,7,8,9,10; int intarr2=2,4,6,8,10,12,14,16; double douarr=1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.0; int main(int argc, char* argv) int itotal=sum(intarr1,10); double dtotal=sum(douarr,10); int iatotal=sum(intarr1,intarr2,8); coutitotal= itotalendl; coutdtotal= dtotalendl; coutiato
8、tal= iatotalvar2)? var1:var2; char *m3=max(Micro, Soft); cout The maxium of Micro and Soft is: m3var2)? var1:var2; char *max(char *str1, char * str2) return(strcmp(str1,str2)=0)? str1:str2; ,2020年6月28日,12,四、多重類型參數(shù),函數(shù)定義格式: template Type1 fun(Type1 a, Type2 b) 說明: Class關(guān)鍵字不能省; 每一個已定義的類型參數(shù)都必須至少被使用一次。 如
9、 int m1=max(100,35.50); int m2=max(A,30);,2020年6月28日,13,五、Template類,定義格式: templateclass newclass 定義成員函數(shù)時,必須以關(guān)鍵字template開始,然后接類型參數(shù)列表。 定義在類體為時,必須以范圍運算符指明其所屬范圍。 必須加上類型參數(shù)以分辯成員函數(shù)將來是屬于此template類的那一個類型。 定義模板類對象的格式: Newclass newclass_object; Type 為用戶指定的數(shù)據(jù)類型。,2020年6月28日,14,如:,template class A protected: T y;
10、 public: A(T x)y=x; void show() cout“y= “y endl; ,void main() A aa(10); aa.show(); char cc=a; A bb(cc); bb.show(); ,2020年6月28日,15,Prog9_5.cpp,const int AS=20; templateclass Array private: Type *element; int size; public: Array(const int size); Array(); Type ,templateinline Array:Array() delete eleme
11、nt; template Type ,2020年6月28日,16,Prog9_5.cpp,void main() Array iobj(AS); Array dobj(AS); Array cobj(AS); cout.flags(ios:showpoint); for(int i=0;iAS;i+) iobji=i; dobji=i; cobj=c; for(i=0;iAS;i+) coutsetw(3)iobji setw(15)dobji cobji endl; ,2020年6月28日,17,運行結(jié)果: 0 0.000000 c 1 1.00000 c 2 2.00000 c 3 3.0
12、0000 c 4 4.00000 c 5 5.00000 c 6 6.00000 c 7 7.00000 c 8 8.00000 c 9 9.00000 c 10 10.0000 c 11 11.0000 c 12 12.0000 c 13 13.0000 c 14 14.0000 c 15 15.0000 c 16 16.0000 c 17 17.0000 c 18 18.0000 c 19 19.0000 c,2020年6月28日,18,六、template類的friend函數(shù),template類的friend函數(shù)有三種: 1. 一般friend函數(shù) 如 friend void displ
13、ay(const int size); 2. 第一種template friend函數(shù) Templateclass Array friend void display(const Array ,2020年6月28日,19,六、template類的friend函數(shù),3. 第二種template friend函數(shù) templateclass Array template friend void display(Array ,2020年6月28日,20,Prog9_7.cpp,templateclass Array friend ostream ,2020年6月28日,21,Prog9_7.cpp,
14、template Array:Array(const int s) size=s; element=new Tsize; for(int i=0;i Array:Array() delete eelement; template T ,template void Array:operator =(T temp) for(int i=0;i ostream ,2020年6月28日,22,const int AS=5; void main() Array iobj(AS); Array dobj(AS); Array cobj(AS); cout.flags(ios:showpoint); iob
15、j=100; dobj=3.14159; cobj=C; cout Integer: iobjendl Double: dobjendl Character: cobjendl; ,2020年6月28日,23,運行結(jié)果:,Integer: 100 100 100 100 100 Double: 3.14159 3.14159 3.14159 3.14159 3.14159 Character: C C C C C,2020年6月28日,24,七、模板繼承,模板與繼承相結(jié)合,將使C的功能變的很強大。 如定義模板類為:,template void A:show() cout“y= “y endl; ,template class A public: T y
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 酒類年關(guān)活動策劃方案(3篇)
- 水田拓展活動方案策劃(3篇)
- 答謝活動策劃方案范本(3篇)
- 租賃衣服活動策劃方案(3篇)
- 氣體混凝土施工方案(3篇)
- 大紅圍巾活動策劃方案(3篇)
- 2025年大學(xué)大三(生物工程概論)工程原理實踐測試試題及答案
- 2025年中職航空服務(wù)(客艙安全)試題及答案
- 2025年大學(xué)病理學(xué)實踐(病理實踐操作)試題及答案
- 2025年高職(市場營銷)崗位能力認(rèn)證測試題及解析
- 14J936《變形縫建筑構(gòu)造》
- 魯班鎖魯班球課件
- 新概念英語第二冊階段一練習(xí)冊
- 2024屆河北省石家莊市普通高中學(xué)校畢業(yè)年級教學(xué)質(zhì)量摸底檢測物理試卷含答案
- 建設(shè)工程施工內(nèi)部承包協(xié)議
- 【角色游戲?qū)τ變荷鐣园l(fā)展影響及促進對策7900字(論文)】
- 第四講 Meta分析的數(shù)據(jù)提取與分析-課件
- 宮內(nèi)節(jié)育器放置術(shù)
- 新制定《無障礙環(huán)境建設(shè)法》主題PPT
- 期末復(fù)習(xí)主題班會
- 道路交通基礎(chǔ)設(shè)施韌性提升
評論
0/150
提交評論