C++筆試題目-帶答案.doc_第1頁
C++筆試題目-帶答案.doc_第2頁
C++筆試題目-帶答案.doc_第3頁
C++筆試題目-帶答案.doc_第4頁
C++筆試題目-帶答案.doc_第5頁
免費預(yù)覽已結(jié)束,剩余7頁可下載查看

下載本文檔

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

文檔簡介

姓名:_時間:_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 int F4();在所描述的函數(shù)中,具有隱含this指針的是:CAF1BF2CF3DF4 本題考查的是this指針。 this指針式一個隱含的指針,它隱含于每個類的非靜態(tài)成員函數(shù)中,它明確地表示出了成員函數(shù)當前操作的數(shù)據(jù)所屬的對象。當對一個對象調(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ù)在調(diào)用時按thiscall調(diào)用約定調(diào)用。故本題答案為C。3派生類的成員函數(shù)不能訪問基類的:CA共有成員和保護成員B共有成員C私有成員D保護成員 本題考查的是繼承的類型。 類的繼承方式有公有繼承、保護繼承和私有繼承三種方式。對于公有繼承基類中的成員訪問屬性不變,對于保護和私有繼承基類中的成員轉(zhuǎn)換為相應(yīng)的訪問類型。但是如果基類成員的訪問屬性為private的,則不能被繼承。故本題答案為C。4按照“后進先出”原則組織數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu)是BA隊列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是個類,value是Value的一個對象。下列以非成員函數(shù)形式重載的運算符函數(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)造函數(shù)的個數(shù)是:AA1個B2個C3個D4個還有默認拷貝構(gòu)造函數(shù), 應(yīng)該選B8有如下類的定義:class Constantspublic:static double GetPI(void)return 3.14159;Constants constants;下列各組語句中,能輸出3.14159的是:BAcoutGetPI();和coutConstants:GetPI();Bcoutconstants.GetPI();和coutConstants.GetPI();CcoutGetPI();和coutGetPI();Dcoutconstants.GetPI();和coutConstants:GetPI();9有如下程序:#include using namespace std;class VACpublic:int f() constreturn 3;int f()return 5;int main()VAC v1;const VAC v2;coutv1.f()v2.f();return 0;運行時的輸出結(jié)果是:AA53B35C55D3310有如下類聲明: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是一個Derived對象,則下列表達式中正確的是:DBAx.value + x.getAmount();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+程序,請計算sizeof的值。char str = “Hello” ;char *p = str ;int n = 10;請計算sizeof (str ) = _5_sizeof ( p ) = _4_sizeof ( n ) = _4_void Func ( char str100)/請計算sizeof( str ) =_4_void *p = malloc( 100 );/請計算sizeof ( p ) =_4_ 3. 補充完整下面的類定義: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(x.a)+1; strcpy(a,x.a); _; XCH()delete a;_ return *this _4. 請寫出下面程序的輸出結(jié)果#includeusing namespace std;class Animal public: virtual char* getType() const return Animal; virtual char* getVoice() const return Voice;class Dog:public Animalpublic: char* getType() const return Dog; char* getVoice() const return Woof;void type(Animal& a) couta.getType();void speak(Animal a) couta.getVoice();int main() Dog d;type(d);cout speak ;speak(d);coutendl; return 0; _ Dog speak Voice _三、問答題(5*10+9+15=74)1.編寫類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; / 用于保存字符串; 請編寫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) 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ù) String: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 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)返回本對象的引用 / 3分 return *this; 2 不調(diào)用C+/C的字符串庫函數(shù),請編寫函數(shù)strcmp的實現(xiàn)(10)。int strcmp ( const char * src, const char * dst )char *strcpy(char *strDest, const char *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 = 0; i 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 從冒泡排序、直接插入排序、二分插入排序和選擇排序四種排序算法中,選擇其中一種算法,寫出它的實現(xiàn)?(10)#include#include#include#include void BubbleSort(int *L,int N) /冒泡 int i,j; int t; for(i=1;ii;j-) if(LjLj-1) t=Lj; Lj=Lj-1; Lj-1=t; int SelectMinKey(int *L,int N,int n) int i,min=n; for(i=n+1;i=N;i+) if(LiLmin) min=i; return min; void SelectSort(int *L,int N) /選擇 int i,j; int t; for(i=1;iN;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(LiLi-1) L0=Li; Li=Li-1; for(j=i-2;L0Lj;j-) Lj+1=Lj; Lj+1=L0; void ShellInsert(int *L,int N, int dk) / 對順序表L作一趟希爾插入排序。本算法對算法10.1作了以下修改: / 1. 前后記錄位置的增量是dk,而不是1; / 2. r0只是暫存單元,不是哨兵。當j=0時,插入位置已找到。 int i,j; for(i=dk+1;i=N;+i) if(Li0&L0Lj);j-=dk) Lj+dk=Lj; / 記錄后移,查找插入位置 Lj+dk=L0; / 插入 / ShellInsertvoid ShellSt(int *L,int N, int dlta, int t) / 算法10.5 / 按增量序列dlta0.t-1對順序表L作希爾排序。 for(int k=0;kt;+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(k=0;kt;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(長度t|冒泡t|選擇t|插入t|希爾n); printf(-+-); for(j=0;N100000;j+) L=(int *)malloc(N+1)*4); t=0; for(i=1;i=N;i+) Li=rand(); tit+=clock(); 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;k4;k+) printf(| %dt,(ti2*k+1-ti2*k); N*=5; printf(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)中,如果想修改Max的內(nèi)容,只需要:const int Max=you want;即可! (4)可以保

溫馨提示

  • 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)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論