C++程序設(shè)計(jì)基礎(chǔ)課后答案 第九章_第1頁
C++程序設(shè)計(jì)基礎(chǔ)課后答案 第九章_第2頁
C++程序設(shè)計(jì)基礎(chǔ)課后答案 第九章_第3頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

閱讀下列程序,寫出執(zhí)行結(jié)果#include<iostream.h>template<typenameT>voidfun(T&x,T&y){Ttemp;temp=x;x=y;y=temp;}voidmain(){inti,j;i=10;j=20;fun(i,j);cout<<"i="<<i<<'\t'<<"j="<<j<<doublea,b;a=1.1;b=2.2;fun(a,b);cout<<"a="<<a<<'\t'<<"b="<<b<<endl;}#include<iostream.h>template<typenameclassBase{public:Base(Ti,Tj){x=i;y=j;}Tsum(){returnx+y;}private:Tx,y;};voidmain(){Base<double>obj2(3.3,5.5);cout<<obj2.sum()<<endl;Base<int>obj1(3,5);cout<<obj1.sum()<<endl;}思考題抽象類和模板都是提供抽象的機(jī)制,請(qǐng)分析它們的區(qū)別和應(yīng)用場(chǎng)合。類屬參數(shù)可以實(shí)現(xiàn)類型轉(zhuǎn)換嗎?如果不行,應(yīng)該如何處理?嗎?請(qǐng)你寫個(gè)驗(yàn)證程序試一試。4試。抽象類和類模板都是提供抽象的機(jī)制,請(qǐng)分析它們的區(qū)別和應(yīng)用場(chǎng)合?!敬鸢浮恳矝]有操作定義。派生類必須定義實(shí)現(xiàn)版本。抽象類用于程序開發(fā)時(shí)對(duì)功能的統(tǒng)一策劃,利用程序運(yùn)行的多態(tài)性自動(dòng)匹配實(shí)行不同版本的函數(shù)。類模板抽象了數(shù)據(jù)類型,稱為類屬參數(shù)。成員函數(shù)描述了類型不同,邏輯操作相同的功能集。編譯器用建立對(duì)象的數(shù)據(jù)類型參數(shù)實(shí)例化為模板類,生成可運(yùn)行的實(shí)體。類模板用于抽象數(shù)據(jù)對(duì)象類型不同,邏輯操作完全相同類定義。這種數(shù)據(jù)類型的推導(dǎo)必須在語言功能的范疇之內(nèi)的。類屬參數(shù)可以實(shí)現(xiàn)類型轉(zhuǎn)換嗎?如果不行,應(yīng)該如何處理?【答案】類屬參數(shù)不可以實(shí)現(xiàn)類型轉(zhuǎn)換。為了解決參數(shù)隱式類型轉(zhuǎn)換的問題,可以用類型參數(shù)把函數(shù)模板重載為非模板函數(shù)。嗎?請(qǐng)你寫個(gè)驗(yàn)證程序試一試?!敬鸢浮款惸0蹇梢月暶鞯挠褑T形式有:普通函數(shù)、函數(shù)模板、普通類成員函數(shù)、類模板成員函數(shù)以及普通類、類模板。當(dāng)類模板的友員是函數(shù)模板時(shí),它們可以定義不同形式的類屬參數(shù)。程序略。試?!敬鸢浮款惸0宓撵o態(tài)數(shù)據(jù)成員可以是抽象類型。它們的存儲(chǔ)空間在生成具體模板類的時(shí)候建立,即每生成一個(gè)模板類同時(shí)建立靜態(tài)儲(chǔ)存空間并做一次文件范圍的初始化。程序略。編程題main(精度浮點(diǎn)型數(shù)組的平均值。答案9.3-1#include<iostream.h>template<typenameT>floataverage(T*array,intsize){Tsum=0;for(inti=0;i<size;i++)sum+=array[i];returnsum/size;}voidmain(){inta[]={1,2,3,4,5,6,7,8,9,10};floatb[]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10};cout<<"Averageofarraya:"<<average(a,10)<<endl;cout<<"Averageofarrayb:"<<average(b,10)<<endl;}建立結(jié)點(diǎn)包括一個(gè)任意類型數(shù)據(jù)域和一個(gè)指針域的單向鏈表類模板。在main(立數(shù)據(jù)域?yàn)檎偷膯蜗蜴湵?,并把鏈表中的?shù)據(jù)顯示出來。答案9.3-2#includetemplate<typenameclassList{public:List(Tx){data=x;}voidappend(List*node){node->next=this;next=0;}List*getnext(){returnnext;}Tgetdata(){returndata;}private:Tdata;List*next;};voidmain(){inti,idata,n,fdata;cout<<"輸入結(jié)點(diǎn)的個(gè)數(shù):";cin>>n;cout<<"輸入結(jié)點(diǎn)的數(shù)據(jù)域:";cin>>fdata;List<int>headnode(fdata);List<int>*p,*last;last=&headnode;for(i=1;i<n;i++){cin>>idata;p=newlist<int>(idata);p->append(last);last=p;}cout<<"鏈表已經(jīng)建立!"<<endl;cout<<"鏈表中的數(shù)據(jù)為:"<<endl;p=&headnode;while(p){cout<<p->getdata()<<endl;p=p->getnext();}}T_Counter*T_VectorT_MatrixT_VectorT_MatrixVectorMatrix(88.34)的語法區(qū)別和運(yùn)算功能區(qū)別?!敬鸢浮柯訫SDNLibraryVisualC++的STL55.4第4題,建立職工信息鏈表,并完成題中要求的各種操作。答案9.3-4#include<iostream>#include<list>#include<iterator>usingnamespacestd;structemployee{intnum;intage;charsex;}voidcreateList(list<employee>&,int);voidoutList(list<employee>&);voidcount(list<employee>&);voidinsert(list<employee>&);voiddel(list<employee>&eList,int);voiddelcreate(list<employee>&);voidmain(){intchoice,n,bh;list<employee>List1;list<employee>::iteratorp;L:cout<<"\n\t\t請(qǐng)鍵入操作選擇\n"<<endl;cout<<"\t1---建立單向鏈表"<<endl;cout<<"\t2---顯示單向鏈表中全部職工信息"<<endl;cout<<"\t3---統(tǒng)計(jì)男女職工人數(shù)"<<endl;cout<<"\t4---在職工尾部插入新結(jié)點(diǎn)"<<endl;cout<<"\t5---刪除指定編號(hào)的結(jié)點(diǎn)"<<endl;cout<<"\t6---刪除指定年齡段的結(jié)點(diǎn),并把被刪除結(jié)點(diǎn)保存在另一鏈表中"<<endl;cout<<"\t0---退出"<<endl;cout<<"\t\t";cin>>choice;switch(choice){case1: cout<<"\tcin>>n;createList(List1,n);gotoL;case2:outList(List1);gotoL;case3:count(List1);gotoL;case4:insert(List1);gotoL;case5:cout<<"\t輸入需刪除結(jié)點(diǎn)編號(hào):";cin>>bh;del(List1,bh);gotoL;case6:delcreate(List1);gotoL;case0: cout\t\n"<<endlbreak;default:cout<<"\t!\n"<<endl;goto}}//建立職工信息單向鏈表voidcreateList(list<employee>&List1,intlen){list<employee>::iteratorp;inti;employeedata;p=List1.begin();for(i=0;i<len;i++,p++){cout<<"\t編號(hào):";cin>>data.num;cout<<"\t年齡:";cin>>data.age;cout<<"\t性別:";cin>>data.sex;p=List1.begin();List1.insert(p,data);}}//顯示單向鏈表中全部職工信息voidoutList(list<employee>&List1){list<employee>::iteratorp;cout<<"\tp=List1.begin();while(p!=List1.end()){cout<<"\t編號(hào):";cout<<(*p).num<<endl;cout<<"\t年齡:";cout<<(*p).age<<endl;cout<<"\t性別:";cout<<(*p).sex<<endl;p++;}cout<<endl;}//統(tǒng)計(jì)男女職工人數(shù)voidcount(list<employee>&List1){list<employee>::iteratorintm=0,f=0;p=List1.begin();while(p!=List1.end()){if((*p).sex=='m')m=m+1;if((*p).sex=='f')f=f+1;p++;}cout<<endl;cout<<"\t男職工人數(shù):"<<m<<endl;cout<<"\t女職工人數(shù):"<<f<<endl;}//在鏈表尾部插入新結(jié)點(diǎn)voidinsert(list<employee>&List1){list<employee>::iteratorpemployeedata;cout<<"\tcout<<"\tcin>>data.num;cout<<"\tcin>>data.age;cout<<"\tcin>>data.sex;cout<<endl;p=List1.begin();while(p!=List1.end(){p++;};List1.insert(p,data);}//刪除指定編號(hào)的結(jié)點(diǎn)voiddel(list<employee>&List1,intbh){list<employee>::iteratorp;charch;intflag=0;p=List1.begin();while(p!=List1.end(){if((*p).num==bh){flag=1;break;}p++;};ifflag==0cout"\tendl;gotoL1;cout<<"\tendl;cout<<"\t編號(hào):";cout<<(*p).num<<endl;cout<<"\t年齡:";cout<<(*p).age<<endl;cout<<"\t性別:";cout<<(*p).sex<<endl;cout<<"\t確實(shí)要?jiǎng)h除?(y/n)";cin>>ch;if(ch=='y'||ch=='Y')List1.erase(p);L1:;}//刪除指定年齡段的結(jié)點(diǎn),并把被刪除結(jié)點(diǎn)保存在另一鏈表中voiddelcreate(list<employee>&List1){list<employee>::iteratorp1,p2,p3;list<employee>List2,List3;p1=List1.begin();p2=List2.begin();p3=List3.begin();while(p1!=List1.end()){if((*p1).age>=55&&(*p1).age<=60){List2.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論