電大《C++語言程序設(shè)計》期末考試試題及答案小抄參考_第1頁
電大《C++語言程序設(shè)計》期末考試試題及答案小抄參考_第2頁
電大《C++語言程序設(shè)計》期末考試試題及答案小抄參考_第3頁
電大《C++語言程序設(shè)計》期末考試試題及答案小抄參考_第4頁
電大《C++語言程序設(shè)計》期末考試試題及答案小抄參考_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

PAGE小抄版C++語言程序設(shè)計期末考試試題及答案姓名____________學號____________班號___________題號一二(1)二(2)三總分成績一、填空1.在類中必須聲明成員函數(shù)的原型,成員函數(shù)的實現(xiàn)部分可以寫在類外。2.如果需要在被調(diào)函數(shù)運行期間,改變主調(diào)函數(shù)中實參變量的值,則函數(shù)的形參應該是引用類型或指針類型。3.抽象類只能作為基類使用,而不能聲明它的對象。4.進行函數(shù)重載時,被重載的同名函數(shù)如果都沒有用const修飾,則它們的形參個數(shù)或類型必須不同。5.通過一個常對象只能調(diào)用它的常成員函數(shù),不能調(diào)用其他成員函數(shù)。6.函數(shù)的遞歸調(diào)用是指函數(shù)直接或間接地調(diào)用自身。7.拷貝構(gòu)造函數(shù)的形參必須是本類對象的引用。二、閱讀下列程序,寫出其運行時的輸出結(jié)果

如果程序運行時會出現(xiàn)錯誤,請簡要描述錯誤原因。1.請在以下兩題中任選一題,該題得分即為本小題得分。如兩題都答,則取兩題得分之平均值為本小題得分。(1)程序:#include<iostream.h>#include<string.h>classBase{private:charmsg[30];protected:intn;public:Base(chars[],intm=0):n(m){strcpy(msg,s);}voidoutput(void){cout<<n<<endl<<msg<<endl;}};classDerived1:publicBase{private: intn;public: Derived1(intm=1):Base("Base",m-1) {n=m;} voidoutput(void) {cout<<n<<endl; Base::output(); }};classDerived2:publicDerived1{private: intn;public: Derived2(intm=2):Derived1(m-1) {n=m;} voidoutput(void) {cout<<n<<endl; Derived1::output(); }};intmain(){ BaseB("BaseClass",1); Derived2D; B.output(); D.output();}運行結(jié)果:1BaseClass210Base(2)程序:#include<iostream.h>classSamp{public: voidSetij(inta,intb){i=a,j=b;} ~Samp() { cout<<"Destroying.."<<i<<endl; } intGetMuti(){returni*j;}protected: inti; intj;};intmain(){ Samp*p; p=newSamp[5]; if(!p) { cout<<"Allocationerror\n"; return1; } for(intj=0;j<5;j++) p[j].Setij(j,j); for(intk=0;k<5;k++) cout<<"Muti["<<k<<"]is:"<<p[k].GetMuti()<<endl; delete[]p; return0;}運行結(jié)果:Muti[0]is:0Muti[1]is:1Muti[2]is:4Muti[3]is:9Muti[4]is:16Destroying..4Destroying..3Destroying..2Destroying..1Destroying..02.請在以下兩題中任選一題,該題得分即為本小題得分。如兩題都答,則取兩題得分之平均值為本小題得分。(1)程序:#include<iostream.h>#include<stdlib.h>classVector{public: Vector(ints=100); int&Elem(intndx); voidDisplay(void); voidSet(void); ~Vector(void);protected: intsize; int*buffer;};Vector::Vector(ints){ buffer=newint[size=s];}int&Vector::Elem(intndx){ if(ndx<0||ndx>=size) { cout<<"errorinindex"<<endl; exit(1); } returnbuffer[ndx];}voidVector::Display(void){ for(intj=0;j<size;j++) cout<<Elem(j)<<endl;}voidVector::Set(void){ for(intj=0;j<size;j++) Elem(j)=j+1;}Vector::~Vector(void){ delete[]buffer;}intmain(){ Vectora(10); Vectorb(a); a.Set(); b.Display();}運行結(jié)果:12345678910最后出現(xiàn)錯誤信息,原因是:聲明對象b是進行的是淺拷貝,b與a共用同一個buffer,程序結(jié)束前調(diào)用析構(gòu)函數(shù)時對同一內(nèi)存區(qū)進行了兩次釋放。(2)程序:#include<iostream.h>classCAT{public: CAT(); CAT(const&CAT); ~CAT(); intGetAge(){return*itsAge;} voidSetAge(intage){*itsAge=age;}protected: int*itsAge;};CAT::CAT(){ itsAge=newint; *itsAge=5;}CAT::~CAT(){ deleteitsAge; itsAge=NULL;}intmain(){CATa;cout<<"a'sage:"<<a.GetAge()<<endl;a.SetAge(6);CATb(a);cout<<"a'sage:"<<a.GetAge()<<endl;cout<<"b'sage:"<<b.GetAge()<<endl;a.SetAge(7);cout<<"a'sage:"<<a.GetAge()<<endl;cout<<"b'sage:"<<b.GetAge()<<endl;}運行結(jié)果:a'sage:5a'sage:6b'sage:6a'sage:7b'sage:7最后出現(xiàn)錯誤信息,原因是:聲明對象b是進行的是淺拷貝,b與a共用同一個buffer,程序結(jié)束前調(diào)用析構(gòu)函數(shù)時對同一內(nèi)存區(qū)進行了兩次釋放。三、閱讀下列程序及說明和注釋信息,在方框中填寫適當?shù)某绦蚨危钩绦蛲瓿芍付ǖ墓δ艹绦蚬δ苷f明:從鍵盤讀入兩個分別按由小到大次序排列的整數(shù)序列,每個序列10個整數(shù),整數(shù)間以空白符分隔。用這兩個序列分別構(gòu)造兩個單鏈表,每個鏈表有10個結(jié)點,結(jié)點的數(shù)據(jù)分別按由小到大次序排列。然后將兩個鏈表合成為一個新的鏈表,新鏈表的結(jié)點數(shù)據(jù)仍然按由小到大次序排列。最后按次序輸出合并后新鏈表各結(jié)點的數(shù)據(jù)。程序運行結(jié)果如下,帶下劃線部分表示輸入內(nèi)容,其余是輸出內(nèi)容:13579111315171924681012141618201234567891011121314151617181920#include<iostream.h>#include<stdlib.h>//類定義部分template<classT>classNode{private:Node<T>*next;//指向后繼節(jié)點的指針public:Tdata;//數(shù)據(jù)域Node(constT&item,Node<T>*ptrnext=NULL);//構(gòu)造函數(shù)voidInsertAfter(Node<T>*p);//在本節(jié)點之后插入一個同類節(jié)點pNode<T>*DeleteAfter(void);//刪除本節(jié)點的后繼節(jié)點,返回其地址Node<T>*NextNode(void)const;//獲取后繼節(jié)點的地址};template<classT>classLinkedList{private:Node<T>*front,*rear;//表頭和表尾指針Node<T>*prevPtr,*currPtr;//記錄表當前遍歷位置的指針,由插入和刪除操作更新intsize;//表中的元素個數(shù)intposition;//當前元素在表中的位置序號。由函數(shù)Reset使用Node<T>*GetNode(constT&item,Node<T>*ptrNext=NULL);//生成新節(jié)點,數(shù)據(jù)域為item,指針域為ptrNextvoidFreeNode(Node<T>*p);//釋放節(jié)點voidCopyList(constLinkedList<T>&L);//將鏈表L拷貝到當前表

//(假設(shè)當前表為空)。被拷貝構(gòu)造函數(shù)、operator=調(diào)用public:LinkedList(void);//構(gòu)造函數(shù)LinkedList(constLinkedList<T>&L);//拷貝構(gòu)造函數(shù)~LinkedList(void);//析構(gòu)函數(shù)LinkedList<T>&operator=(constLinkedList<T>&L);//重載賦值運算符intListSize(void)const;//返回鏈表中元素個數(shù)(size)intListEmpty(void)const;//size為0時返回TRUE,否則返回FALSEvoidReset(intpos=0);//將指針currPtr移動到序號為pos的節(jié)點,

//prevPtr相應移動,position記錄當前節(jié)點的序號voidNext(void);//使prevPtr和currPtr移動到下一個節(jié)點intEndOfList(void)const;//currPtr等于NULL時返回TRUE,否則返回FALSEintCurrentPosition(void)const;//返回數(shù)據(jù)成員positionvoidInsertFront(constT&item);//在表頭插入一個數(shù)據(jù)域為item的節(jié)點voidInsertRear(constT&item);//在表尾添加一個數(shù)據(jù)域為item的節(jié)點voidInsertAt(constT&item);//在當前節(jié)點之前插入一個數(shù)據(jù)域為item的節(jié)點voidInsertAfter(constT&item);//在當前節(jié)點之后插入一個數(shù)據(jù)域為item的節(jié)點TDeleteFront(void);//刪除頭節(jié)點,釋放節(jié)點空間,更新prevPtr、currPtr和sizevoidDeleteAt(void);//刪除當前節(jié)點,釋放節(jié)點空間,更新prevPtr、currPtr和sizeT&Data(void);//返回對當前節(jié)點成員data的引用voidClearList(void);//清空鏈表:釋放所有節(jié)點的內(nèi)存空間。};//類實現(xiàn)部分略template<classT>voidMergeList(LinkedList<T>*la,LinkedList<T>*lb,LinkedList<T>*lc){//合并鏈表la和lb,構(gòu)成新鏈表lc。//函數(shù)結(jié)束后,程序的數(shù)據(jù)所占內(nèi)存空間總數(shù)不因此函數(shù)的運行而增加。while(!la->ListEmpty()&&!lb->ListEmpty()){ if(la->Data()<=lb->Data()) {lc->InsertRear(la->Data()); la->DeleteAt(); }else {lc->InsertRear(lb->Data()); lb->DeleteAt(); }}while(!la->ListEmpty()){lc->InsertRear(la->Data()); la->DeleteAt();}while(!lb->ListEmpty()){lc->InsertRear(lb->Data()); lb->DeleteAt(); }}intmain(){LinkedList<int>la,lb,lc; intitem,i;//讀如數(shù)據(jù)建立鏈表la for(i=0;i<10;i++) { cin>>item;la.InsertRear(item); } la.Reset();//讀如數(shù)據(jù)建立鏈表lb for(i=0;i<10;i++) { cin>>item;lb.InsertRear(item); } lb.Reset(); MergeList(&la,&lb,&lc);//合并鏈表 lc.Reset();//輸出各節(jié)點數(shù)據(jù),直到鏈表尾while(!lc.EndOfList()) {cout<<lc.Data()<<"";lc.Next();//使currPtr指向下一個節(jié)點 }cout<<endl;}Visa-freepolicybringsChengdubiz,tourismboost.Makingnationalheadlinesseveraltimes,Chengdu's72-hourvisa-freepolicyhasattractedwideattentionfrombothChineseandforeignexpertsandbusinessmensinceittookeffectonSept1lastyear.Theprogrampermitscitizensfrom51countriesandregionsincludingtheUnitedStates,Australia,CanadaandJapanwhohavevalidvisasandflightticketstoathirdcountrytospendthreedaysinthecity.ThecapitalofSichuanprovinceisthefirstcityinthewesternregionofChinatoofferforeigntouristsathree-dayvisaandthefourthnationwidetoadoptthepolicyfollowingShanghai,BeijingandGuangzhou.LiZhiyong,deputydeanofthetourisminstituteatSichuanUniversity,saidthemove"contributestoalargeincreaseinthenumberofoverseastouristsandraisesthecity'slevelofinternationalization"."Thepolicywillalsobringdirecteconomicrevenue,"Lisaid."Chengduhasmanyculturallegaciesandisalsoaparadiseforpandaloverswiththeworld'slargestbreedingandresearchcenter.Threedaysarelongenoughforforeignvisitorstovisitthoseiconictouristspots,"henoted.ThecityishometotheremainsoftheJinshacivilizationthatdatesbackmorethan3,000yearsaswellastheQingchengMountainsandtheDujiangyanirrigationsystem.QingchenghaslongbeenrecognizedasthebirthplaceofTaoism,China'sancientindigenousreligion,whileDujiangyanisconsideredtobetheoldestfunctioningwater-controlprojectintheworld.Chengdurankedthirdintouristfacilities,managementandservicesamong60Chinesecitiesinacustomersatisfactionsurveyreleasedlastyear.But,Liaddedthateffortsarestillneededtodevelopmoretourismproducts,improveEnglishservicesandprovideaccuratetranslationoftrafficsignsandscenicbillboards.ZhaoYun,chairwomanofBritishChamberofCommerceSouthwestChina,toldChinaDailythathiscolleaguesfoundthepolicyveryconvenient."ABritishclientonceflewhereandstayedforjustonedaytocheckherorderedgoods,"shesaid.ZhaowasborninShanxiprovince,butshehaslivedinChengduformorethan10years."Mylifewaslikearunningracemovingfromplacetoplace.IalsolivedinBeijingandShanghaibefore,"shesaid."ButChengduisaplacethatyouneverwanttoleaveoncesettlingdown.Itisnowmysecondhometown,"shesaid.Iftheenvironmentisfurtherimproved,thecitywillattractmorepeopletovisitandlive,withthe72-hourvisa-freepolicyandcompellingconditionsintransportation,culture,climateandcuisine,hesaid.Foreignersalsogavepositivefeedbackonthepolicy.AspokesmanfromDellIncsaidthecompanyhasaglobalhubofoperationinChengdu,sothethree-dayvisa"hasanimmediateandpositiveinfluenceonthecompany'sbusinessdevelopment".RudyButtignol,presidentofthepublicbroadcastingcompanyinBritishColumbia,Canada,saidhisworkrequiresfrequenttraveltoChengduandthepolicy"makesthetripseasier".Datafromthecity'spublicsecuritybureaushowssome100foreignvisitorsenjoyedthe72-hourpolicybytheendofMarch,mostofthemfromtheUnitedStates,theUnitedKingdomandGermany.Chengdualsoreportedrobustgrowthinitsoveralltouristindustrylastyear.Officialstatisticsshowthatitreceivedsome150milliontouristslastyear,anincreaseof28percentfrom2012.Around1.7millioncamefromabroad,anincreaseof12percent.Totalrevenuefromtourismsurpassed133billionyuan($21.7billion).DuringhisvisittoKazakhstaninSeptember,ChinesePresidentXiJinpingproposedthatChinaandCentralAsiajoinhandstobuildaSilkRoadeconomicbelttoboostcooperation.TheideahasbeenwidelyechoedinCentralAsiancountries,becominganencouragingblueprintforChineseareasalongtheSilkRoadthathaslinkedAsiaandEuropeformorethan2,000years.Inthenextthreeweeks,ChinaDailyreporterswilltravelthroughthebeltinChi

溫馨提示

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

評論

0/150

提交評論