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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

2、象的引用 。二、閱讀下列程序,寫出其運行時的輸出結果 如果程序運行時會出現(xiàn)錯誤,請簡要描述錯誤原因。1請在以下兩題中任選一題,該題得分即為本小題得分。如兩題都答,則取兩題得分之平均值為本小題得分。(1)程序:- 12 -#include #include class base private: char msg30; protected: int n; public: base(char s,int m=0):n(m) strcpy(msg,s); void output(void) coutnendlmsgendl; ;class derived1:public baseprivate:in

3、t n;public:derived1(int m=1):base(base,m-1) n=m; void output(void) coutnendl; base:output();class derived2:public derived1private:int n;public:derived2(int m=2):derived1(m-1) n=m; void output(void) coutnendl; derived1:output();int main()base b(base class,1);derived2 d;b.output();d.output();運行結果:1bas

4、e class210base(2)程序:#include class samppublic:void setij(int a,int b)i=a,j=b;samp()coutdestroying.iendl;int getmuti()return i*j; protected:int i;int j;int main()samp *p;p=new samp5;if(!p)coutallocation errorn;return 1;for(int j=0;j5;j+)pj.setij(j,j);for(int k=0;k5;k+)coutmutik is: pk.getmuti()endl;d

5、eletep;return 0;運行結果:muti0 is:0muti1 is:1muti2 is:4muti3 is:9muti4 is:16destroying.4destroying.3destroying.2destroying.1destroying.02請在以下兩題中任選一題,該題得分即為本小題得分。如兩題都答,則取兩題得分之平均值為本小題得分。(1)程序:#include #include class vector public: vector(int s=100); int& elem(int ndx); void display(void); void set(void);

6、vector(void); protected: int size; int *buffer;vector:vector(int s)buffer=new intsize=s;int& vector:elem(int ndx)if(ndx=size)couterror in indexendl;exit(1);return bufferndx;void vector:display(void)for(int j=0; jsize; j+)coutelem(j)endl;void vector:set(void)for(int j=0; jsize; j+)elem(j)=j+1;vector:

7、vector(void)delete buffer;int main()vector a(10);vector b(a);a.set();b.display();運行結果:12345678910最后出現(xiàn)錯誤信息,原因是:聲明對象b是進行的是淺拷貝,b與a共用同一個buffer,程序結束前調用析構函數(shù)時對同一內存區(qū)進行了兩次釋放。(2)程序:#includeclass cat public: cat(); cat(const &cat); cat(); int getage() return *itsage; void setage( int age ) *itsage=age; protect

8、ed: int * itsage;cat:cat()itsage=new int;*itsage=5;cat:cat()delete itsage;itsage=null;int main()cat a;coutas age:a.getage()endl;a.setage(6);cat b(a);coutas age:a.getage()endl;coutbs age:b.getage()endl;a.setage(7);coutas age:a.getage()endl;coutbs age:b.getage()endl;運行結果:as age:5as age:6bs age:6as age

9、:7bs age:7最后出現(xiàn)錯誤信息,原因是:聲明對象b是進行的是淺拷貝,b與a共用同一個buffer,程序結束前調用析構函數(shù)時對同一內存區(qū)進行了兩次釋放。三、閱讀下列程序及說明和注釋信息,在方框中填寫適當?shù)某绦蚨?,使程序完成指定的功?程序功能說明:從鍵盤讀入兩個分別按由小到大次序排列的整數(shù)序列,每個序列10個整數(shù),整數(shù)間以空白符分隔。用這兩個序列分別構造兩個單鏈表,每個鏈表有10個結點,結點的數(shù)據(jù)分別按由小到大次序排列。然后將兩個鏈表合成為一個新的鏈表,新鏈表的結點數(shù)據(jù)仍然按由小到大次序排列。最后按次序輸出合并后新鏈表各結點的數(shù)據(jù)。 程序運行結果如下,帶下劃線部分表示輸入內容,其余是輸出內

10、容:1 3 5 7 9 11 13 15 17 192 4 6 8 10 12 14 16 18 201 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20#include #include /類定義部分template class node private: node *next; /指向后繼節(jié)點的指針 public: t data; /數(shù)據(jù)域 node (const t& item, node* ptrnext = null); / 構造函數(shù) void insertafter(node *p); /在本節(jié)點之后插入一個同類節(jié)點p node *d

11、eleteafter(void); /刪除本節(jié)點的后繼節(jié)點,返回其地址 node *nextnode(void) const; / 獲取后繼節(jié)點的地址;template class linkedlist private: node *front, *rear; / 表頭和表尾指針 node *prevptr, *currptr; /記錄表當前遍歷位置的指針,由插入和刪除操作更新 int size; / 表中的元素個數(shù) int position; / 當前元素在表中的位置序號。由函數(shù)reset使用 node *getnode(const t& item,node *ptrnext=null);

12、/ 生成新節(jié)點,數(shù)據(jù)域為item,指針域為ptrnext void freenode(node *p); /釋放節(jié)點 void copylist(const linkedlist& l); / 將鏈表l 拷貝到當前表 /(假設當前表為空)。被拷貝構造函數(shù)、operator=調用 public: linkedlist(void); / 構造函數(shù) linkedlist(const linkedlist& l); /拷貝構造函數(shù) linkedlist(void); / 析構函數(shù) linkedlist& operator= (const linkedlist& l);/重載賦值運算符 int list

13、size(void) const; /返回鏈表中元素個數(shù)(size) int listempty(void) const; /size為0時返回true,否則返回false void reset(int pos = 0); /將指針currptr移動到序號為pos的節(jié)點, /prevptr相應移動,position記錄當前節(jié)點的序號 void next(void); /使prevptr和currptr移動到下一個節(jié)點 int endoflist(void) const; / currptr等于null時返回true, 否則返回false int currentposition(void) co

14、nst; /返回數(shù)據(jù)成員position void insertfront(const t& item); /在表頭插入一個數(shù)據(jù)域為item的節(jié)點 void insertrear(const t& item); /在表尾添加一個數(shù)據(jù)域為item的節(jié)點 void insertat(const t& item); /在當前節(jié)點之前插入一個數(shù)據(jù)域為item的節(jié)點 void insertafter(const t& item); /在當前節(jié)點之后插入一個數(shù)據(jù)域為item的節(jié)點 t deletefront(void); /刪除頭節(jié)點,釋放節(jié)點空間,更新prevptr、currptr和size void

15、deleteat(void); /刪除當前節(jié)點,釋放節(jié)點空間,更新prevptr、currptr和size t& data(void); / 返回對當前節(jié)點成員data的引用 void clearlist(void); / 清空鏈表:釋放所有節(jié)點的內存空間。;/類實現(xiàn)部分略.template void mergelist(linkedlist* la, linkedlist* lb,linkedlist* lc)/合并鏈表la和lb,構成新鏈表lc。/函數(shù)結束后,程序的數(shù)據(jù)所占內存空間總數(shù)不因此函數(shù)的運行而增加。 while ( !la-listempty() &!lb-listempty()

16、 if (la-data()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();int main() linkedlist la, lb, lc; int item, i;/讀如數(shù)據(jù)建立鏈表la for (

17、i=0;i item; la.insertrear(item); la.reset();/讀如數(shù)據(jù)建立鏈表lb for (i=0;i 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;if we dont do that it will go on and go on. we have to stop

18、 it; we need the courage to do it.his comments came hours after fifa vice-president jeffrey webb - also in london for the fas celebrations - said he wanted to meet ivory coast international toure to discuss his complaint.cska general director roman babaev says the matter has been exaggerated by the

19、ivorian and the british media.blatter, 77, said: it has been decided by the fifa congress that it is a nonsense for racism to be dealt with with fines. you can always find money from somebody to pay them.it is a nonsense to have matches played without spectators because it is against the spirit of f

20、ootball and against the visiting team. it is all nonsense.we can do something better to fight racism and discrimination.this is one of the villains we have today in our game. but it is only with harsh sanctions that racism and discrimination can be washed out of football.the (lack of) air up there w

21、atch mcayman islands-based webb, the head of fifas anti-racism taskforce, is in london for the football associations 150th anniversary celebrations and will attend citys premier league match at chelsea on sunday.i am going to be at the match tomorrow and i have asked to meet yaya toure, he told bbc

22、sport.for me its about how he felt and i would like to speak to him first to find out what his experience was.uefa hasopened disciplinary proceedings against cskafor the racist behaviour of their fans duringcitys 2-1 win.michel platini, president of european footballs governing body, has also ordere

23、d an immediate investigation into the referees actions.cska said they were surprised and disappointed by toures complaint. in a statement the russian side added: we found no racist insults from fans of cska.baumgartner the disappointing news: mission aborted.the supersonic descent could happen as ea

24、rly as sunda.the weather plays an important role in this mission. starting at the ground, conditions have to be very calm - winds less than 2 mph, with no precipitation or humidity and limited cloud cover. the balloon, with capsule attached, will move through the lower level of the atmosphere (the t

25、roposphere) where our day-to-day weather lives. it will climb higher than the tip of mount everest (5.5 miles/8.85 kilometers), drifting even higher than the cruising altitude of commercial airliners (5.6 miles/9.17 kilometers) and into the stratosphere. as he crosses the boundary layer (called the

26、tropopause),e can expect a lot of turbulence.the balloon will slowly drift to the edge of space at 120,000 feet ( then, i would assume, he will slowly step out onto something resembling an olympic diving platform.below, the earth becomes the concrete bottom of a swimming pool that he wants to land on, but not too hard. still, hell be traveling fast, so despite the distance, it will not be like diving into the deep end of a pool. it will be like he is diving into the shallow end.skydiver preps for the big jumpwhen he jumps, he is expected to reach the speed of sound

溫馨提示

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

評論

0/150

提交評論