


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、某某: 時間:DCCBB AADAD一、選擇題(1*10=10)1 .如果派生類以proctected方式繼承基類,如此原基類的 protected和public成 員在派生類的訪問性分別是:DA. public 和 publicB. public 和 protectedC. protected和 publicD. protected和 protected解析:通過protected方式繼承基類后,原基類的私有成員不可訪問,而protected 和public成員均變成protected成員。答案:D2. 有如下頭文件:int F1();static int F2();classs CApub
2、lic:int F3();static int F4();在所描述的函數(shù)中,具有隱含this指針的是:CA. F1B. F2C. F3D. F4此題考查的是this指針。this指針式一個隱含的指針,它隱含于每個類的非靜態(tài)成員函數(shù)中,它明確 地表示出了成員函數(shù)當(dāng)前操作的數(shù)據(jù)所屬的對象。當(dāng)對一個對象調(diào)用成員函數(shù) 時,編譯程序先將對象的地址賦值給this指針,然后調(diào)用成員函數(shù),每次成員函 數(shù)存取數(shù)據(jù)成員時,如此隱含使用this指針。this指針是指向?qū)ο蟊旧淼闹羔?,它只存在于類的非靜態(tài)成員中。f1,f2不是成員函數(shù),不存在隱含指針;f4為靜態(tài)成員函數(shù),也不含有this指針;含有this 指針的函數(shù)
3、在調(diào)用時按thiscall調(diào)用約定調(diào)用。故此題答案為Co3. 派生類的成員函數(shù)不能訪問基類的:CA .共有成員和保護成員B .共有成員C.私有成員D .保護成員此題考查的是繼承的類型。類的繼承方式有公有繼承、保護繼承和私有繼承三種方式。對于公有繼承基類中的成員訪問屬性不變, 對于保護和私有繼承基類中的成員轉(zhuǎn)換為相應(yīng)的訪問類型。但是如果基類成員的訪問屬性為 private的,如此不能被繼承。故此題答案為Co4. 按照“后進先出原如此組織數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu)是BA .隊列B .棧D .二叉樹C.雙向鏈表答案為B5. 如下關(guān)于虛函數(shù)的說明中,正確的答案是:BA .從虛基類繼承的函數(shù)都是虛函數(shù)B.虛函數(shù)不得
4、是靜態(tài)成員函數(shù)C.只能通過指針或者引用調(diào)用虛函數(shù) D .抽象類中的中的成員函數(shù)都是虛函 數(shù)。答案為B6. Value是個類,value是Value的一個對象。如下以非成員函數(shù)形式重載的運 算符函數(shù)原型中,正確的答案是:AA. Value operator+(Value v, int i);B.Value operator+(Value v=value, int i);C. Value operator+(Value v, int=0); D. Value operator+(Value v=value, int i=0);7. 有如下類的定義:Class MyClassint value;pu
5、blic:MyClass(i nt n):value( n)int getValue() const return value; ;B. 2個D. 4個如此類Myclass的構(gòu)造函數(shù)的個數(shù)是:A. 1個C. 3個還有默認(rèn)拷貝構(gòu)造函數(shù),應(yīng)該選B8. 有如下類的定義: class Con sta nts public:static double GetPI(void)return 3.14159; ;Con sta nts con sta nts;如下各組語句中,能輸出3.14159的是:bA. cout«constants->GetPI()和 cout<<Consta
6、nts:GetPI();B. cout«constants.GetPI()和 cout<<Constants.GetPI();C. cout«constants->GetPI()和 cout<<Constants->GetPI();D. cout«constants.GetPI()和 cout<<Constants:GetPI();9. 有如下程序:#i nclude <iostream>using n amespace std;class VACpublic:int f() con stretur n
7、3;int f()return 5;int main()VAC v1;const VAC v2;cout<<v1.f()vvv2.f();return 0;運行時的輸出結(jié)果是:AA. 53B. 35C. 55D. 3310. 有如下類聲明:class Baseprotected:int amount;public:Base(i nt n = 0):am oun t( n)int getAm oun t() const retur n amoun t;class Derived:public Baseprotected:int value;public:Derived(i nt m,
8、 i nt n ):value(m),Base( n)int getData() const return value + amoun t;x是一個Derived對象,如此如下表達(dá)式中正確的答案是:BA. x.value + x.getAmount();B. x.getData() + x.getAmount();C. x.getData() -x.amount;D. x.value + x.amount;二、填空題(8*2=16)400_6 4 4 4 4 return *this _Dog speak Voice1.如下中a的值是 400#define AAA 200#define BBB
9、 AAA+100int a= BBB*22. 以下為 Windows NT下的32位C+程序,請計算 sizeof的值。char str =“ Helle;char *p = str ;int n = 10;請計算sizeof (str ) = 5sizeof ( p ) = 4sizeof ( n ) = 4void Func ( char str1OO)請計算sizeof( str ) =4void *p = malloc( 100 );/請計算sizeof ( p ) =43. 補充完整下面的類定義:class XCHchar* a;public:XCH(char* aa) / 構(gòu)造函數(shù)
10、 a=new charstrle n( aa)+1; strcpy(a,aa);XCH& operator=(const XCH& x) / 重載賦值函數(shù) delete a;a=new charstrle n(x.a)+1;strcpy(a,x.a);JXCH()delete a;return *this4. 請寫出下面程序的輸出結(jié)果#in clude<iostream>using n amespace std;class Animal public:virtual char* getType() const return "A ni mal"
11、virtual char* getVoice() const retur n "Voice" ;class Dog:public An imalpublic:char* getType() const retur n "Dog"char* getVoice() const retur n "Woof" ;void type(A ni mal& a) cout<<a.getType();void speak(A ni mal a) cout<<a.getVoice(); int mai n()Dog d;
12、type(d);cout<<" speak "speak(d);cout<<e ndl; return 0;Dog speak Voice 、問答題(5*10+9+15=74)1.編寫類String的拷貝構(gòu)造函數(shù)和賦值函數(shù)(可以調(diào)用C+/C的字符串庫函 數(shù))(15) o類String的原型為:class Stringpublic:String(const char *str = NULL);/ 普通構(gòu)造函數(shù)String(const String &other);/ 拷貝構(gòu)造函數(shù) String(void);/ 析構(gòu)函數(shù)String &
13、operate =(const String &other); / 賦值函數(shù)private:char *m_data;/用于保存字符串;請編寫String的上述4個函數(shù)。/ String的析構(gòu)函數(shù)String:String(void)/ 3 分delete m_data;/由于m_data是內(nèi)部數(shù)據(jù)類型,也可以寫成delete m_data; / String的普通構(gòu)造函數(shù)String:String(const char *str)/ 6 分if(str=NULL)/假如能加 NULL判斷如此更好m_data = new char1;*m_data =0''elsein
14、t len gth = strle n( str);m_data = new charle ngth+1; strcpy(m_data, str);/拷貝構(gòu)造函數(shù)Strin g:Stri ng(const String &other) int len gth = strle n( other.m_data); m_data = new charle ngth+1; strcpy(m_data, other.m_data);/賦值函數(shù)/假如能加NULL判斷如此更好/ 3分/假如能加 NULL判斷如此更好String & String:operate =(const String
15、&other)/ 13 分/ (1)檢查自賦值4分if(this = & other)return *this;/ (2)釋放原有的內(nèi)存資源delete m_data;/ 3分/ 3分配新的內(nèi)存資源,并復(fù)制內(nèi)容/ 3分int len gth = strle n( other.m_data);m_data = new charlength+1;假如能加 NULL 判斷如此更好strcpy(m_data, other.m_data);/ 4返回本對象的引用/ 3分return *this;2.不調(diào)用C+/C的字符串庫函數(shù),請編寫函數(shù) strcmp的實現(xiàn)(10)。int strcmp
16、 ( const char * src, const char * dst )char *strcpy(char *strDest, const char *strSrc)char *address = strDest;while (*strDest+ = * strSrc+)NULL ;retur n address ;3. F(n)=F(n-1)+F(n-2),F(0)=1,F(1)=1.分別用遞歸和循環(huán)求F(5)(10)public int R(i nt nu m)if(nu m<=0) num=1;else if(num=1)num=1;else num=R( nu m-1)+R
17、 (n um-2); return num;public int c(i nt num) int a = 1;int b = 1;int c = 0;for (i nt i = 0; i < num- 2; i+) c = a + b;a = b;b = c;return c;4.寫一算法,對單鏈表實現(xiàn)就地逆置(不要構(gòu)造新結(jié)點)(10)node *reserve (no de*head)node*p1,*p2,*p3;if(head=NULL)|(head-> next=NULL) retur n head;p1=head; p2=p1- >n ext; while(p2!=
18、NULL)p3=p2->n ext;p2->n ext=p1;p1=p2;p2=p3;head-> next=NULL;p仁head;retur n head;5. 從冒泡排序、直接插入排序、二分插入排序和選擇排序四種排序算法中,選 擇其中一種算法,寫出它的實現(xiàn)? (10)#i nclude<stdio.h>#in clude<time.h>#in clude<math.h>#i nclude<malloc.h>void BubbleSort(int *L,int N) /冒泡int i,j;int t;for(i=1;i<
19、;=N;i+)for(j=N;j>i;j-)if(Lj<Lj-1)t=Lj;Lj=Lj-1;Lj-1=t;int SelectMinKey(int *L,int N,int n)int i,m in=n;for(i=n+1;i<=N;i+)if(Li<Lmi n)min=i;return mi n;void SelectSort(i nt *L,i nt N) /選擇int i,j;int t;for(i=1;i<N;i+)j=SelectMi nKey(L,N,i);if(i!=j)t=Li;Li=Lj;Lj=t;void InsertSort(int *L,i
20、nt N) /插入 int i,j;for(i=2;i<=N;i+)if(Li<Li-1)L0=Li;Li=Li-1;for(j=i-2;L0<Lj;j-)Lj+1=Lj;Lj+1=L0;10.1作了以下修改: 時,插入位置已找到。void ShellI nsert(i nt *L,i nt N, i nt dk) / 對順序表L作一趟希爾插入排序。本算法對算法/1.前后記錄位置的增量是dk,而不是1;/ 2. r0只是暫存單元,不是哨兵。當(dāng)j<=0int i,j;for(i=dk+1;i<=N;+i)if(Li<Li-dk)暫存在L.r0記錄后移,查找插入
21、位置 插入 / 需將L.ri插入有序增量子表LO=Li;/for(j=i-dk;(j>0&&LO<Lj);j-=dk) Lj+dk=Lj;/Lj+dk=LO;/ / Shelll nsert void ShellSt(int *L,int N, int dlta, int t)/ 按增量序列dlta0.t-1對順序表L作希爾排序。for(int k=0;k<t;+k)ShellInsert(L,N, dltak);/一趟增量為 dltak的插入排序 / ShellSort void ShellSort(i nt *L,i nt N) /希爾int t=(i n
22、t)log(N);產(chǎn)生增量序列int k,*dlta;dlta=(i nt*)malloc(t*4); / for(k=0;k<t;k+) dltak=(i nt)pow(2,t-k)-1;ShellSt(L,N,dlta,t); int main()int N=250;int i,j,k;int t;int ti16;int *L;sran d(time(NULL);printf("長度t|冒泡t|選擇t|插入t|希爾n");printf(”+");for(j=0;N<100000;j+)L=(i nt *)malloc(N+1)*4);t=0;fo
23、r(i=1;i<=N;i+) Li=ra nd();tit+=clock(); BubbleSort(L,N); tit+=clock();for(i=1;i<=N;i+) Li=ra nd();tit+=clock(); SelectSort(L,N); tit+=clock();for(i=1;i<=N;i+) Li=ra nd();tit+=clock();In sertSort(L,N); tit+=clock();for(i=1;i<=N;i+) Li=ra nd();tit+=clock(); ShellSort(L,N); tit+=clock();pri
24、n tf("n%dt",N); for(k=0;k<4;k+)prin tf("| %dt",(ti2*k+1-ti2*k);N*=5;prin tf("nn");6. 個類中,const類型成員函數(shù)的主要作用是什么?在該函數(shù)中可以調(diào)用該 類的什么類型的成員變量和成員函數(shù)?該類的一個非const對象可以調(diào)用const成員函數(shù)嗎? (10)1可以定義const常量,具有不可變性。2便于進展類型檢查,使編譯器對處理內(nèi)容有更多了解,消除了一些隱患。 例如:void f(const int i) 編譯器就會知道i是一個常量,不允許修改;3可以防止意義模糊的數(shù)字出現(xiàn),同樣可以很方便地進展參數(shù)的調(diào)整和修改。同宏定義一樣,可以做到不變
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 公路項目人員聘請合同范本
- 農(nóng)村房屋安裝維修合同范本
- 公司員工勞動合同范本
- 北京企業(yè)住房合同范本
- 產(chǎn)品交付標(biāo)準(zhǔn)合同范本
- 公司擔(dān)保合同范本6
- 綜合實踐項目《制作細(xì)胞模型》教學(xué)設(shè)計-2024-2025學(xué)年魯科版生物六年級上冊
- 2人合伙合同范本
- 修路混凝土合同范本
- 產(chǎn)品加工定制合同范本
- 2024年度考研政治全真模擬試卷及答案(共六套)
- (完整版)幕墻施工方案
- 24年追覓在線測評28題及答案
- 智能建造施工技術(shù) 課件 項目1 智能建造施工概論;項目2 土方工程;項目3 基礎(chǔ)工程
- 醫(yī)學(xué)教材 超聲引導(dǎo)下乳腺真空微創(chuàng)旋切(VABB)
- 2024年鐵路線路工(高級技師)技能鑒定理論考試題庫(含答案)
- 2025高考物理步步高同步練習(xí)選修1練透答案精析
- 汽車修理工勞動合同三篇
- 職業(yè)本科《大學(xué)英語》課程標(biāo)準(zhǔn)
- 修建水壩施工合同模板
- 北師大版三年級下冊除法豎式計算題練習(xí)100道及答案
評論
0/150
提交評論