




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、姓名:_時(shí)間:_DCCBB AADAD一、選擇題(1*10=10)1如果派生類以proctected方式繼承基類,則原基類的protected和public成員在派生類的訪問性分別是:DApublic和publicBpublic和protectedCprotected和publicDprotected和protected解析:通過protected方式繼承基類后,原基類的私有成員不可訪問,而protected和public成員均變成protected成員。答案:D2有如下頭文件:int F1();static int F2();classs CApublic:int F3();static i
2、nt F4();在所描述的函數(shù)中,具有隱含this指針的是:CAF1BF2CF3DF4 本題考查的是this指針。 this指針式一個(gè)隱含的指針,它隱含于每個(gè)類的非靜態(tài)成員函數(shù)中,它明確地表示出了成員函數(shù)當(dāng)前操作的數(shù)據(jù)所屬的對(duì)象。當(dāng)對(duì)一個(gè)對(duì)象調(diào)用成員函數(shù)時(shí),編譯程序先將對(duì)象的地址賦值給this指針,然后調(diào)用成員函數(shù),每次成員函數(shù)存取數(shù)據(jù)成員時(shí),則隱含使用this指針。 this指針是指向?qū)ο蟊旧淼闹羔?,它只存在于類的非靜態(tài)成員中。f1,f2不是成員函數(shù),不存在隱含指針;f4為靜態(tài)成員函數(shù),也不含有this指針;含有this指針的函數(shù)在調(diào)用時(shí)按thiscall調(diào)用約定調(diào)用。故本題答案為C。3派生
3、類的成員函數(shù)不能訪問基類的:CA共有成員和保護(hù)成員B共有成員C私有成員D保護(hù)成員 本題考查的是繼承的類型。 類的繼承方式有公有繼承、保護(hù)繼承和私有繼承三種方式。對(duì)于公有繼承基類中的成員訪問屬性不變,對(duì)于保護(hù)和私有繼承基類中的成員轉(zhuǎn)換為相應(yīng)的訪問類型。但是如果基類成員的訪問屬性為private的,則不能被繼承。故本題答案為C。4按照“后進(jìn)先出”原則組織數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu)是BA隊(duì)列B棧C雙向鏈表D二叉樹答案為B。5下列關(guān)于虛函數(shù)的說明中,正確的是:BA從虛基類繼承的函數(shù)都是虛函數(shù)B虛函數(shù)不得是靜態(tài)成員函數(shù)C只能通過指針或者引用調(diào)用虛函數(shù)D抽象類中的中的成員函數(shù)都是虛函數(shù)。答案為B。6已知Value是個(gè)
4、類,value是Value的一個(gè)對(duì)象。下列以非成員函數(shù)形式重載的運(yùn)算符函數(shù)原型中,正確的是:AAValue operator+(Value v, int i);BValue operator+(Value v=value, int i);CValue operator+(Value v, int=0);DValue operator+(Value v=value, int i=0);7有如下類的定義:Class MyClassint value;public:MyClass(int n):value(n)int getValue() const return value;則類Myclass的構(gòu)
5、造函數(shù)的個(gè)數(shù)是:AA1個(gè)B2個(gè)C3個(gè)D4個(gè)還有默認(rèn)拷貝構(gòu)造函數(shù), 應(yīng)該選B8有如下類的定義:class Constantspublic:static double GetPI(void)return 3.14159;Constants constants;下列各組語句中,能輸出3.14159的是:BAcout<<constants->GetPI();和cout<<Constants:GetPI();Bcout<<constants.GetPI();和cout<<Constants.GetPI();Ccout<<constants
6、->GetPI();和cout<<Constants->GetPI();Dcout<<constants.GetPI();和cout<<Constants:GetPI();9有如下程序:#include <iostream>using namespace std;class VACpublic:int f() constreturn 3;int f()return 5;int main()VAC v1;const VAC v2;cout<<v1.f()<<v2.f();return 0;運(yùn)行時(shí)的輸出結(jié)果是:AA
7、53B35C55D3310有如下類聲明:class Baseprotected:int amount;public:Base(int n = 0):amount(n)int getAmount() const return amount;class Derived:public Baseprotected:int value;public:Derived(int m, int n):value(m),Base(n)int getData() const return value + amount;已知x是一個(gè)Derived對(duì)象,則下列表達(dá)式中正確的是:DBAx.value + x.getAmo
8、unt();Bx.getData() + x.getAmount();Cx.getData() x.amount;Dx.value + x.amount;二、填空題(8*2=16)400_ 6 4 4 4 4 return *this _ Dog speak Voice 1.下列中a的值是_400_#define AAA 200#define BBB AAA+100int a= BBB*22. 以下為Windows NT下的32位C+程序,請(qǐng)計(jì)算sizeof的值。char str = “Hello” ;char *p = str ;int n = 10;請(qǐng)計(jì)算sizeof (str ) = _
9、5_sizeof ( p ) = _4_sizeof ( n ) = _4_void Func ( char str100)/請(qǐng)計(jì)算sizeof( str ) =_4_void *p = malloc( 100 );/請(qǐng)計(jì)算sizeof ( p ) =_4_ 3. 補(bǔ)充完整下面的類定義:class XCH char* a;public: XCH(char* aa) /構(gòu)造函數(shù) a=new charstrlen(aa)+1; strcpy(a,aa); XCH& operator=(const XCH& x) /重載賦值函數(shù) delete a; a=new charstrlen(
10、x.a)+1; strcpy(a,x.a); _; XCH()delete a;_ return *this _4. 請(qǐng)寫出下面程序的輸出結(jié)果#include<iostream>using namespace std;class Animal public: virtual char* getType() const return "Animal" virtual char* getVoice() const return "Voice"class Dog:public Animalpublic: char* getType() const
11、return "Dog" char* getVoice() const return "Woof"void type(Animal& a) cout<<a.getType();void speak(Animal a) cout<<a.getVoice();int main() Dog d;type(d);cout<<" speak "speak(d);cout<<endl; return 0; _ Dog speak Voice _三、問答題(5*10+9+15=74)1.編寫類
12、String的拷貝構(gòu)造函數(shù)和賦值函數(shù)(可以調(diào)用C+/C的字符串庫函數(shù))(15)。 已知類String的原型為:class String public: String(const char *str = NULL); / 普通構(gòu)造函數(shù) String(const String &other); / 拷貝構(gòu)造函數(shù) String(void); / 析構(gòu)函數(shù) String & operate =(const String &other); / 賦值函數(shù)private:char *m_data; / 用于保存字符串; 請(qǐng)編寫String的上述4個(gè)函數(shù)。/ String的析構(gòu)函數(shù) S
13、tring: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) m_data = new char1; / 若能加 NULL 判斷則更好 *m_data = 0; else int length = strlen(str); m_data = new charlength+1; / 若能加 NULL 判斷則更好 strcpy(m_data, str); / 拷貝構(gòu)造函數(shù) St
14、ring:String(const String &other) / 3分 int length = strlen(other.m_data); m_data = new charlength+1; / 若能加 NULL 判斷則更好 strcpy(m_data, other.m_data); / 賦值函數(shù) String & String:operate =(const String &other) / 13分 / (1) 檢查自賦值 / 4分 if(this = &other) return *this; / (2) 釋放原有的內(nèi)存資源 / 3分 delete
15、m_data; / (3)分配新的內(nèi)存資源,并復(fù)制內(nèi)容 / 3分 int length = strlen(other.m_data); m_data = new charlength+1; / 若能加 NULL 判斷則更好 strcpy(m_data, other.m_data); / (4)返回本對(duì)象的引用 / 3分 return *this; 2 不調(diào)用C+/C的字符串庫函數(shù),請(qǐng)編寫函數(shù)strcmp的實(shí)現(xiàn)(10)。int strcmp ( const char * src, const char * dst )char *strcpy(char *strDest, const char *
16、strSrc) char *address = strDest; while (*strDest+ = * strSrc+) NULL ; return address ; 3 F(n)=F(n-1)+F(n-2),F(0)=1,F(1)=1.分別用遞歸和循環(huán)求F(5)(10)。public int R(int num)if(num<=0)num=1;else if(num=1)num=1;elsenum=R(num-1)+R(num-2);return num;public int c(int num) int a = 1;int b = 1;int c = 0;for (int i
17、= 0; i < num- 2; i+) c = a + b;a = b; b = c;return c;4 寫一算法,對(duì)單鏈表實(shí)現(xiàn)就地逆置(不要構(gòu)造新結(jié)點(diǎn))(10)。node *reserve(node*head)node*p1,*p2,*p3;if(head=NULL)|(head->next=NULL)return head;p1=head;p2=p1->next;while(p2!=NULL)p3=p2->next;p2->next=p1;p1=p2;p2=p3;head->next=NULL;p1=head;return head;5 從冒泡排序、
18、直接插入排序、二分插入排序和選擇排序四種排序算法中,選擇其中一種算法,寫出它的實(shí)現(xiàn)?(10)#include<stdio.h>#include<time.h>#include<math.h>#include<malloc.h> void BubbleSort(int *L,int N) /冒泡 int i,j; int t; for(i=1;i<=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
19、) int i,min=n; for(i=n+1;i<=N;i+) if(Li<Lmin) min=i; return min; void SelectSort(int *L,int N) /選擇 int i,j; int t; for(i=1;i<N;i+) j=SelectMinKey(L,N,i); if(i!=j) t=Li; Li=Lj; Lj=t; void InsertSort(int *L,int N) /插入 int i,j; for(i=2;i<=N;i+) if(Li<Li-1) L0=Li; Li=Li-1; for(j=i-2;L0<
20、;Lj;j-) Lj+1=Lj; Lj+1=L0; void ShellInsert(int *L,int N, int dk) / 對(duì)順序表L作一趟希爾插入排序。本算法對(duì)算法10.1作了以下修改: / 1. 前后記錄位置的增量是dk,而不是1; / 2. r0只是暫存單元,不是哨兵。當(dāng)j<=0時(shí),插入位置已找到。 int i,j; for(i=dk+1;i<=N;+i) if(Li<Li-dk) / 需將L.ri插入有序增量子表 L0=Li; / 暫存在L.r0 for(j=i-dk;(j>0&&L0<Lj);j-=dk) Lj+dk=Lj; /
21、 記錄后移,查找插入位置 Lj+dk=L0; / 插入 / ShellInsertvoid ShellSt(int *L,int N, int dlta, int t) / 算法10.5 / 按增量序列dlta0.t-1對(duì)順序表L作希爾排序。 for(int k=0;k<t;+k) ShellInsert(L,N, dltak); / 一趟增量為dltak的插入排序 / ShellSortvoid ShellSort(int *L,int N) /希爾 int t=(int)log(N); int k,*dlta; dlta=(int*)malloc(t*4); /產(chǎn)生增量序列 for(
22、k=0;k<t;k+) dltak=(int)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; srand(time(NULL); printf("長(zhǎng)度t|冒泡t|選擇t|插入t|希爾n"); printf("-+-"); for(j=0;N<100000;j+) L=(int *)malloc(N+1)*4); t=0; for(i=1;i<=N;i+) Li=rand(); tit+=clock()
23、; BubbleSort(L,N); tit+=clock(); for(i=1;i<=N;i+) Li=rand(); tit+=clock(); SelectSort(L,N); tit+=clock(); for(i=1;i<=N;i+) Li=rand(); tit+=clock(); InsertSort(L,N); tit+=clock(); for(i=1;i<=N;i+) Li=rand(); tit+=clock(); ShellSort(L,N); tit+=clock(); printf("n%dt",N); for(k=0;k<
24、;4;k+) printf("| %dt",(ti2*k+1-ti2*k); N*=5; printf("nn");6 一個(gè)類中,const類型成員函數(shù)的主要作用是什么?在該函數(shù)中可以調(diào)用該類的什么類型的成員變量和成員函數(shù)?該類的一個(gè)非const對(duì)象可以調(diào)用const成員函數(shù)嗎?(10)(1)可以定義const常量,具有不可變性。 (2)便于進(jìn)行類型檢查,使編譯器對(duì)處理內(nèi)容有更多了解,消除了一些隱患。例如: void f(const int i) 編譯器就會(huì)知道i是一個(gè)常量,不允許修改;(3)可以避免意義模糊的數(shù)字出現(xiàn),同樣可以很方便地進(jìn)行參數(shù)的調(diào)整和修改。 同宏定義一樣,可以做到不變則已,一變都變!如(1)中,如果想修改Max的內(nèi)容,只需要:c
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年高性能特種合金材料項(xiàng)目合作計(jì)劃書
- 同城工地出售合同范本
- 合作建材協(xié)議合同范例
- 共同投資協(xié)議合同范本
- 賣地買房合同范本
- 卷宗管理服務(wù)合同范例
- 合同范本庫編制說明
- 資質(zhì)借用合同范本
- 農(nóng)田煙桿出售合同范本
- 幼兒園塑膠地板購(gòu)銷施工合同范本
- 民航基礎(chǔ)知識(shí)應(yīng)用題庫100道及答案解析
- 數(shù)字孿生水利項(xiàng)目建設(shè)可行性研究報(bào)告
- SolidWorks-2020項(xiàng)目教程全套課件配套課件完整版電子教案
- 2025年全國(guó)計(jì)算機(jī)二級(jí)考試模擬考試題庫及答案(共280題)
- 中國(guó)水資源與水環(huán)境-王浩
- DL-T 2680-2023 電力建設(shè)施工企業(yè)安全生產(chǎn)標(biāo)準(zhǔn)化實(shí)施規(guī)范
- 2024年用電監(jiān)察員(用電檢查員)技師職業(yè)鑒定考試題庫(含答案)
- 2024年輔警招聘考試試題庫附參考答案(綜合題)
- 垃圾發(fā)電廠汽機(jī)培訓(xùn)
- 村情要素模板
- 高考作文標(biāo)準(zhǔn)方格紙-A4-可直接打印
評(píng)論
0/150
提交評(píng)論