




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實用文檔華僑大學 面向對象程序設計(二) 試卷b 班 級 學 號 姓 名 成 績 注意:請將所有答案寫在答卷上!一、填空題(15分,每小題1分)1. 如果用戶自定義的函數在調用點之后再定義,則在調用點之前,必須用(1)對被調函數進行說明。2. 函數的遞歸調用可以有直接遞歸調用和(2)遞歸調用。3. 靜態(tài)全局變量的作用域是(3) 。4. 若將某函數定義為靜態(tài)型(static),則該函數只能被(4)所調用。5. main(int argc,char *argv)函數中的參數argv用于(5) 。6. (6)提供了一種在程序內和程序之間組織和使用名稱的技術。7. 用“const 類型標識符&
2、 引用名=目標變量名;”聲明引用,此處const的含義是(7) 。8. c+語言中,(8)定義一個結構體類型,該結構類型中包含指向自身結構類型的指針成員。(填“可以”或“不可以”)9. 若有類型定義“typedef char string80; string s;”,則s的類型為(9) 。10. 類的對象可以直接訪問類中的(10)成員。(private/protected/public)11. 在定義類時,若沒有為類提供析構函數,則編譯系統(tǒng)會自動為該類創(chuàng)建一個默認的析構函數,其形式為(11) 。12. 拷貝構造函數的功能是實現(xiàn)對象值的拷貝,通過將一個同類對象的值拷貝給一個新對象,來完成對新對象
3、的初始化。定義一個拷貝構造函數的方式是(12) 。13. 當希望一個類(如a)可以存取另一個類(如b)的私有成員時,可以將該類(a)聲明為另一類(b)的友元類,友元類(a)的所有成員函數都可視為該類(b)的(13)函數。14. 在靜態(tài)成員函數的實現(xiàn)中不能直接引用類中說明的非靜態(tài)成員,但可以引用類中說明的(14)成員。15. c+支持兩種不同類型的多態(tài):一種是編譯時的多態(tài),另一種是(15)時的多態(tài)。二、選擇題(18分,每小題2分)1. 進行文件操作時,需要包含(1)_文件。 a) iostream b) fstream c) stdio.h d) stdlib.h 2. 若有定義:enum we
4、ekday sun=7,mon=1,tue,wed,thu,fri,sat day;,以下正確的語句是(2) 。a) mon+; b)int k=mon+1; c) day=7; d)day+;3. 若有定義:int (*p)(int,int); ,則錯誤的敘述是(3) 。a) p是一個指向返回值類型為int,帶有兩個int型參數的函數的指針b) 該類型的變量p可以作為另外一個函數的參數c) 若已定義了函數int sum(int x,int y),則p可以指向函數sum。d) p可以作自增運算p+4. 若已有定義:struct student char *name,int age,float
5、mark;s8; student* p=s;,以下語句(組),正確的是(4) 。a) cin>>>>s0.age>>s0.mark; b) cin>>p->age>>p->mark;p->name="liuhua" c) =new char10;strcpy(p->name, "liuhua"); d) p->name=new char10; strcpy(p->name, "liuhua");5若有定義:int
6、 a,b;,下面引用變量的聲明中,(5)是正確的。a) int& ra=a; int& &rra=ra; b) int& y=10; c) int& z=int; d) int &x=a; int &y=b;6 對于下面定義的類myclass,在函數f中可以將對象obj的成員data值修改為50的語句是(6)。class myclass public: myclass(int x) data=x; void setnum(int value) data=value; private: int data;void f(void) mycla
7、ss obj(45),*ptr=&obj; (6) a) obj.data=50; b)obj.myclass(50); c)ptr->setnum(50); d)ptr->data=50;7.下面敘述正確的是(7) 。a) 基類的protected成員不能被派生類的成員函數訪問b) 基類的public成員能被派生類的對象直接訪問c) 基類的protected成員在派生類中可能是privated的d) 基類的private成員不能被基類的成員函數訪問8. 構造函數不具備的特征的是 (8) 。 a) 構造函數的函數名與類名相同 b)構造函數可以重載 c) 構造函數必須有返回值
8、類型 d)構造函數可以設置默認參數9. 下面敘述錯誤的是(9) 。a) 基類指針可以指向派生類對象 b) 基類指針可以指向基類對象c) 派生類指針可以指向派生類對象 d) 派生類指針可以指向基類對象 三、閱讀程序題(45分)1.閱讀下面程序,給出程序的運行結果。(15分,每小題5分)1) #include<iostream>using namespace std;void fun(int x,int *py,int& z) x+; (*py)-; z*=2; cout<<"x="<<x<<","&l
9、t;<"*py="<<*py<<","<<"z="<<z<<endl;void main(void) int a=1,b=2,c=3; fun(a,&b,c); cout<<"a="<<a<<","<<"b="<<b<<","<<"c="<<c<<endl;
10、2) #include<iostream>using namespace std;void p(int w) if (w=0) return; p(w-1); cout<<w; p(w-1); void main(void) p(3); 3)#include<iostream>using namespace std;void print(int,int,int (*p_fun)(int,int);int add(int,int);int substract(int,int);void main(void) int a=10,b=-20,choice; int
11、(*p)(int,int); cout<<"enter your choice:" cin>>choice; switch(choice) case 1: p=add; break; default: p=substract; print(a,b,p);int add(int x,int y) return x+y; int substract(int x,int y) return x-y; void print(int i,int j,int (*p_fun)(int,int) cout<<"the result is &q
12、uot;<<p_fun(i,j)<<endl; 程序運行時:enter your choice:12.閱讀下面程序,回答相關問題。(30分)1)(7分)#include <iostream>using namespace std;struct studentint num;char name20;char sex;int age;void main(void) student* fun(student *,int);student stu= 11,"lilin",'m',18,3,"sunny",
13、9;m',24, 2,"zhao",'m',20, 4,"liu yanhong",'f',30 ;int size=sizeof(stu)/sizeof(student);student *p=fun(stu,size);cout<<p->num<<'t'<<p->name<<'t'<<p->age<<endl;student* fun(student *s, int n) /問題a和bint
14、 index=0,i;for(i=1;i<n;i+)if(si.num>sindex.num) index=i;return s+index;a) (3分)給出函數fun()的功能;b) (4分)若將函數student* fun(student *s, int n)的返回值類型改為student& ,要完成相同的功能,給出源程序中其它需要修改的語句并作相應的修改。2)(6分)#include<iostream>using namespace std;class student char* name; int age; float score; public: s
15、tudent(char* n,int a,float s):age(a),score(s) cout<<"constructing."<<n<<endl; name=new charstrlen(n)+1; strcpy(name,n); student() cout<<"destructing."<<name<<endl; delete name;void main(void) student s("liu",18,86.5); student *p_st1=n
16、ew student("chen",18,86.5); delete p_st1; /問題b)a)(4分)給出程序的運行結果; b)(2分)若去掉語句delete p_st1;,程序運行結果會不同嗎?3)(9分)#include<iostream>using namespace std;class point private: char *name; float x,y;public: point(char* s,float a,float b) x=a;y=b;name=new charstrlen(s)+1;strcpy(name,s); cout<&
17、lt;"initializing point: "<<name<<", x="<<x<<" ,y="<<y<<endl; point(point &p); float getx() return x; float gety() return y; void show() cout<<"name:"<<name<<", x="<<x<<",y=&qu
18、ot;<<y<<endl; point() cout<<"deleting point: "<<name<<", "<<x<<","<<y<<endl; delete name; ;class linepoint p1,p2;public:line(char* p1name,float x1,float y1,char* p2name,float x2,float y2):p1(p1name,x1,y1),p2(p2name,x
19、2,y2)cout<<"line constructor."<<endl;line()cout<<"line destructor."<<endl;point:point(point &p) /問題b name=new charstrlen()+1; strcpy(name,); x=p.x; y=p.y; cout<<"coping point: "<<<<","<<x<
20、;<","<<y<<endl;void fun(point p) p.show(); void main(void) point a("obja",12,24); fun(a); line l1("p1",1,2,"p2",3,4);a)(6分)給出程序的運行結果;b)(3分)給出拷貝構造函數point(point &p)的功能。是否可以去掉該函數?4)(8分)#include<iostream>using namespace std;class basepubli
21、c:int bvalue;base(int v)bvalue=v;cout<<"base constructor."<<endl;int getbvalue() return bvalue; ;class d1:public basepublic:int d1value;d1(int v,int b):base(v)d1value=b;cout<<"d1 constructor."<<endl;int getd1value() return d1value; ;class d:public d1public
22、 :int dvalue;int getdvalue() return dvalue; d(int v1,int v2,int a):d1(v1,v2)dvalue=a;cout<<"d constructor."<<endl;void main(void)d obj(1,2,3);cout<<obj.getdvalue()<<endl; cout<<obj.getbvalue()<<endl; /問題b) cout<<obj.getd1value()<<endl;a)(6分)給
23、出程序的運行結果;b)(2分)能否將表達式obj.getbvalue()改為obj.bvalue?四、編程題(22分)1. 一個簡單分數類的結構如下,請補充相應代碼,使之完成程序功能。#include<iostream.h>int gcd(int m,int n) /(1)完成求兩個數的最大公因數(5分) ?class fractionalnumber /分數類的定義int numerator; /分子int denominator; /分母public:fractionalnumber(int n=0,int d=1) /完成構造函數的實現(xiàn)(5分)?fractionalnumbe
24、r() cout<<"destructor is called."<<endl;fractionalnumber(const fractionalnumber& x)/拷貝構造函數numerator=x.numerator;denominator=x.denominator;simplify();cout<<"copy constructor is called."<<endl;void simplify(void) /對分數的分子和分母進行化簡int g=gcd(denominator,numer
25、ator);numerator/=g;denominator/=g;void output() cout<<numerator<<"/"<<denominator<<endl; /分數的輸出void add(const fractionalnumber& x) /分數的加法(5分)?friend fractionalnumber operator+(fractionalnumber& num1, fractionalnumber& num2); /友元函數的聲明;/類定義結束 fractionalnum
26、ber operator+(fractionalnumber& num1,fractionalnumber& num2)int nresult,dresult;dresult=num1.denominator*num2.denominator;nresult=num1.numerator*num2.denominator+num1.denominator*num2.numerator;return fractionalnumber(nresult,dresult); void main(void)/設計測試程序,創(chuàng)建類對象,使用output()、add()和運算符+(7分) ?
27、/end of main()華僑大學 面向對象程序設計(二) 答卷姓 名 學號 成績 一、填空題(15分,每小題1分)(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12)(13) (14) (15)二、選擇題(18分,每小題2分)(1) (2) (3) (4) (5) (6) (7) (8) (9) 三、閱讀程序題(45分)1.閱讀下面程序,給出程序的運行結果。(15分,每小題5分)1) 運行結果:2)運行結果:3)運行結果:2.閱讀下面程序,回答相關問題。(30分)1)(7分)a) (3分)函數fun()的功能:b) (4分)需要修改:2)(6
28、分)a)(4分)運行結果:b)(2分)3)(9分)a)(6分)運行結果:b) (3分)4)(8分)1)(6分)運行結果:2)四、編程題(22分)1. 一個簡單分數類的結構如下,請補充相應代碼,使之完成程序功能。int gcd(int m,int n) /(1)完成求兩個數的最大公因數(5分) fractionalnumber(int n=0,int d=1) /完成構造函數的實現(xiàn)(5分)void add(const fractionalnumber& x) /分數的加法(5分)void main(void)/設計測試程序,創(chuàng)建類對象,使用output()、add()和運算符+(7分)
29、/end of main()參考答案一、填空題(15分,每小題1分)(1)函數原型 (2)間接 (3)定義它所在的源程序文件 (4)定義該函數的源程序所在文件的其他函數 (5)指針數組中各元素分別指向命令行中的各個字符串(6)命名空間(7)引用的目標變量是常量 (8)可以 9.字符數組 10.public 11.類名 12類名(const 類名& 對象名) 13友元函數 14.靜態(tài) 15.運行二、選擇題(18分,每小題2分)(1)b (2)b (3)d (4)d (5)d (6)c (7)c (8)c (9)d三、閱讀程序題(45分)1.閱讀下面程序,給出程序的運行結果。(15分,每小題5分)1) 2) 1 2 1 3 1 2 13) 2.閱讀下面程序,回答相關問題。(30分)1)(7分)a)函數返回最大學號的學生指針;b)需
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權】 ISO 11901-1:2025 EN Tools for pressing - Gas springs - Part 1: General specifications
- 【正版授權】 ISO 9466:2025 EN Railway applications - Coating of passenger rail vehicles
- 【正版授權】 ISO/IEC 15045-4-2:2024 EN Information technology - Home Electronic System (HES) gateway - Part 4-2: Structure - Simple gateway
- 2025年物流運輸項目融資擔保合同匯編
- 2025年度綠色能源項目融資居間合作協(xié)議范本
- 幼兒園健康教育教學計劃
- 林業(yè)行業(yè)會計個人工作計劃
- 財務信息化建設方案計劃
- 學習成果與展示活動計劃
- 前臺工作中的情緒管理技巧計劃
- 2024機械買賣協(xié)議
- 2024-2030年中國汽車安全氣囊行業(yè)發(fā)展形勢分析及投資規(guī)劃分析報告
- DZ∕T 0289-2015 區(qū)域生態(tài)地球化學評價規(guī)范(正式版)
- 譯林版六年級下冊英語Unit-1《The-lion-and-the-mouse》教學課件
- 睡眠中心管理系統(tǒng)技術要求
- 學校校園禁煙處罰管理方案
- 少兒美術教育知識講座
- 外科學教學課件:頸、腰椎退行性疾病
- 2023-2024屆高考語文復習小說訓練(含答案)-孫犁《風云初記》
- 中醫(yī)培訓課件:《拔罐技術》
- 取節(jié)育環(huán)之后的護理
評論
0/150
提交評論