哈工大秋《C程序設(shè)計(jì)》考試題A_第1頁
哈工大秋《C程序設(shè)計(jì)》考試題A_第2頁
哈工大秋《C程序設(shè)計(jì)》考試題A_第3頁

下載本文檔

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

文檔簡介

1、哈工大 2005年秋季學(xué)期班號C+程序設(shè)計(jì)試 題(A卷)學(xué)號考試時(shí)間120分鐘滿分 70姓名題號-一一-二二-三四五總分實(shí)驗(yàn)(20)平時(shí)(10)總成績分?jǐn)?shù)一、填空題。(共20分,每空1分)C+、Java、C#等)具有三大特性,它1. 面向?qū)ο蟮某绦蛟O(shè)計(jì)方法(可用各種語言實(shí)現(xiàn),如為 2.已知m = 4,執(zhí)行m+;后,表達(dá)式的值是_m的值是_執(zhí)行+m;后,表達(dá)式規(guī)的值是,m的值是。范 3. 已知m = 3, n = 2,則執(zhí)行語句 m > n ? m+ : +n , m+, m+n;后,表達(dá)式的值是。4. 循環(huán)語句while(int i = 0) i-; 執(zhí)行的循環(huán)次數(shù)是次;語句遵宀for(

2、int i(0), j(0); i = j = 1; i+, j-);執(zhí)行的循環(huán)次數(shù)是 次。守與 5.為了使函數(shù)能夠返回兩個(gè)或者更多的值,可將形參設(shè)置為下列兩種方式,它們分別.場, 是_或_,如果希望實(shí)參不被修改,那么需要對形參使用關(guān)鍵字 _限制。旳 6.定義在類體外的函數(shù),如果希望成為內(nèi)聯(lián)函數(shù),則必須在類體內(nèi)的聲明中用關(guān)鍵字_標(biāo)識;運(yùn)算符的重載必須用關(guān)鍵字_標(biāo)識。7.在C+中,類的繼承方式有三種,需要用關(guān)鍵字和來定義。默認(rèn)繼承方式為。&在C+中,定義友元類或者友元函數(shù)必須使用關(guān)鍵字 。二、判斷題,正確的請打",錯(cuò)誤的請打X。(共10分,每題1分)1. 編譯C+源程序的過程中

3、,出現(xiàn)了警告錯(cuò)誤,也可以生成可執(zhí)行文件。()2. C+中,使用#define定義的常量必須給出類型說明,而使用 const給出的常量不用給 出類型說明。()3. 表達(dá)式的值的類型只取決于運(yùn)算符,與操作數(shù)無關(guān)。()4. break語句可以出現(xiàn)在各種不同循環(huán)語句的循環(huán)體中,continue語句只能出現(xiàn)在循環(huán)體中。()5. 函數(shù)沒有返回值時(shí),可以不加任何類型的說明符;任何一個(gè)函數(shù)都可以設(shè)置默認(rèn)參數(shù),但是不允許將一個(gè)函數(shù)的所有參數(shù)設(shè)置為默認(rèn)參數(shù)。()6. 指向函數(shù)的指針可以作為函數(shù)參數(shù);使用數(shù)組名作函數(shù)參數(shù)時(shí),實(shí)參數(shù)組與形參數(shù)組是共享內(nèi)存 單元的。()7. 結(jié)構(gòu)與聯(lián)合的差別在于結(jié)構(gòu)中的每個(gè)成員都有自己

4、的內(nèi)存地址,且都是不相同的,而聯(lián)合中的所有成員是共享內(nèi)存塊的,且內(nèi)存塊的大小與成員中的最大內(nèi)存塊相同。()8. 如果類的一個(gè)成員函數(shù)只存取該類的靜態(tài)數(shù)據(jù)成員,可將該成員函數(shù)說明為靜態(tài)成員函數(shù);友元類的所有成員函數(shù)都可以對該類中的私有成員進(jìn)行存取操作。()9. 類型轉(zhuǎn)換函數(shù)是類的成員函數(shù),它是用來進(jìn)行強(qiáng)制類型轉(zhuǎn)換的,它不能定義為友元函數(shù),也不能定義為靜態(tài)的成員函數(shù);帶有一個(gè)參數(shù)的構(gòu)造函數(shù)具有類型轉(zhuǎn)換的作用。()10. 在公有繼承中,派生類的對象不可以訪問基類中的保護(hù)成員;在私有繼承中,派生類的對象不可以訪問基類中的公有成員;在保護(hù)繼承中,派生類可以訪問基類中的保護(hù)成員。()三、請分析下列程序,并

5、回答問題1給出下列程序的輸出結(jié)果,并說明其功能。=4- !: A X if t x a. fi A x aF 4 * F * *f -!>* .#include <iostream.h>void swap( int&, int& );* >.int main():int i = 5, j = 10;:cout<<"ti= "<<i<<"tj= " <<j<<e ndl;+swap(i, j);:cout<<"ti= "<

6、;<i<<"tj= "<<j<<e ndl;:+*:return 0;+E+*fivoid swap(i nt& v1, int& v2)int tmp = v2;v2 = v1;v1 = tmp;答:1、輸出結(jié)果如下:(共16分)2、給出下列程序的輸出結(jié)果,并說明其功能。#i nclude <iostream.h>-.int fac(i nt);void mai n()¥int s(0);for(i nt i(1); i <= 5; i+)Is += fac(i); cout<&l

7、t;s<<e ndl;int fac(i nt n):static int b = 1;Ib *= n; return b;答:2、輸出結(jié)果如下:該程序的主要功能是:該程序的主要功能是:2 / 7int &fun( char cha, int &n, int &c)if(cha >= 'O' && cha <= 9)return n;elsereturn c;F « » S e F ¥ 4- B I F W B 4 -F V 4 F ¥ 1- H B W I- B -I F

8、V 4 *FV49B«-P*!>4a * B答:3、該程序段的主要功能是:如果輸入數(shù)據(jù)為:abc2R&4W39#,請問輸出結(jié)果是什么?3、閱讀下列程序,回答后面的問題。#in elude <iostream.h>int &fun( char cha, int &n, int &c); void mai n()int tn (0), tc(0), m(0);cout<<"E nter characters:" char ch;cin> >ch;while(ch != '#')m

9、 = fun (ch, tn, tc)+; cin> >ch;cout<<t n<<en dl; cout<<tc<<e ndl;4、i#in clude <iostream.h>int mai n()class base public:derived *pd = new derived;virtual int foo( int ival = 1024 ) jbase *pb = pd;cout < "base:foo() - ival: " < ival < en dl;int va

10、l = pb->foo();return ival;cout << "main(): val through base: "<< val << endl; val = pd->foo();/ . !cout << "ma in() : val through derived: "<< val <<ien dl;class derived : public base 1public:virtual int foo( int ival = 2048 ) 答:4、該程序段的輸出

11、結(jié)果是:cout << "derived:foo() - ival: " << ival << en dl;return ival;11 :/ .;i四、閱讀下列文檔,并回答提出的問題。(共16分)1、根據(jù)下列文檔說明,請簡要地回答后面給出的幾個(gè)問題。(6分)The methods for default assignment and initialization are “ memberwiseassignment a”nd “ memberwise initialization, ” respectively. Memberwise

12、assignment consists of copying one object to the other, a mr aetmbe a time, as if assigning each member individually. Memberwise initialization consists of copying one object to the other, a member at a time, as if initializing each member individually. The primary difference between the two is that

13、 memberwise assignment invokes each member' sassignment operator (operator= ), whereas memberwise initialization invokes each member 's copy constructor.Memberwise assignment is performed only by the assignment operator declared in the form:type& type : operator=( const | volatile type&a

14、mp; )Default assignment operators for memberwise assignment cannot be generated if any of the followingconditions exist: A member class has constmembers. A member class has reference members. A member class or its base class has a private assignment operator ( operator=). A base class or member clas

15、s has no assignment operator (operator=).Default copy constructors for memberwise initialization cannot be generated if the class or one of its base classes has a private copy con structor or if any of the follow ing con diti ons exist: A member class hasconst members. A member class has reference m

16、embers. A member class or its base class has a private copy con structor. A base class or member class has no copy con structor.The default assignment operators and copy constructors for a given class are always declared, but they are not defi ned uni ess both of the follow ing con diti ons are met:

17、 The class does not provide a user-defi nedfun cti on for this copy. The program requires that the fun cti on be prese nt. This requireme nt exists if an assignment or initialization is encountered that requires memberwise copying or if the address of the class operator= function is taken.If both of

18、 these conditions are not met, the compiler is not required to generate code for the default assignment operator and copy constructor functions (elimination of such code is an optimization performed by the Microsoft C+ compiler). Specifically, if the class declares a user-defined operator= that take

19、s an argument of type“referencleastos-name,” no default assignment operator is generated. If the class declaresa copy constructor, no default copy constructor is generated. 在C+的類定義中,請簡要回答默認(rèn)的賦值方法與默認(rèn)的初始化方法之間的差別。 請問在什么情況下,編譯器是不會產(chǎn)生默認(rèn)的賦值方法的? 請問在什么情況下,編譯器是不會產(chǎn)生默認(rèn)的初始化方法的?2、閱讀下列說明文檔,回答后面的問題。(4分)Virtual base

20、classes offer a way to save space and avoid ambiguities in class hierarchies that use multiple inheritance. Each nonvirtual object contains a copy of the data members defined in the base class. This duplication wastes space and requires you to specify which copy of the base class members you wantwhe

21、n ever you access them. When a base class is specified as a virtual base, it can act as an in direct base more tha n once without duplicati on of its data members. A sin gle copy of its data members is shared by all the base classes that use it as a virtual base. The virtual keyword appears in the b

22、ase lists of the derived classes, not the base class.1為什么要提出虛基類的概念,它有什么好處?答:Q如何定義虛基類呢?答:3、閱讀下列說明文檔,回答后面的問題。(6分)“ Virtual fun cti ons” are functions that en sure that the correct fun ctio n is called for an object, regardless(the expressi on used to make the function call. Suppose a base class contai

23、ns a function declared as virtual and a derived class defines the same function. The function from the derived class is invoked for objects of the derived class, even if it is called using a pointer or reference to the base class. Functions in derived classes override virtual fun ctio ns in base cla

24、sses only if their type is the same. A function in a derived class cannot differ from a virtual fun cti on in a base class in its retur n type only; the argume nt list must differ as well.When calli ng a function using poin ters or referen ces, the follow ing rules apply:A call to a virtual function

25、 is resolved according to the underlying type of object for which it is called.A call to a non virtual function is resolved accord ing to the type of the poin ter or reference.virtual functions are called only for objects of class types, you cannot declare global or static functions as virtual. The

26、virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defi ned uni ess they are declared using the pure-specifier. The virtual function-call mecha nism ca

27、n be suppressed by explicitly qualifying the function name using the scope-resolution operator (:).Abstract classes cannot be used for: variables or member data、 argume nt types、function retur n typesand types of explicit con vers ions. Ano ther restrict ion is that if the con structor for an abstra

28、ct class calls a pure virtual fun cti on, either directly or in directly, the result is un defi ned. However, con structors and destructors for abstract classes can call other member fun cti ons.根據(jù)你的理解,給出下列程序的輸出結(jié)果。.:#include <iostream.h>class Base :public:Base() cout<<"C on structor of Base !"<<e ndl; Base() cout<<"Destructor of Base !&qu

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論