C網(wǎng)絡(luò)作業(yè)6答案_第1頁(yè)
C網(wǎng)絡(luò)作業(yè)6答案_第2頁(yè)
C網(wǎng)絡(luò)作業(yè)6答案_第3頁(yè)
C網(wǎng)絡(luò)作業(yè)6答案_第4頁(yè)
C網(wǎng)絡(luò)作業(yè)6答案_第5頁(yè)
已閱讀5頁(yè),還剩15頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、作業(yè)4一、 選擇題1 下列關(guān)于動(dòng)態(tài)聯(lián)編的描述中,錯(cuò)誤的是_。D A)動(dòng)態(tài)聯(lián)編是以虛函數(shù)為基礎(chǔ)的 B)動(dòng)態(tài)聯(lián)編是在運(yùn)行時(shí)確定所調(diào)用的函數(shù)代碼的 C)動(dòng)態(tài)聯(lián)編調(diào)用函數(shù)操作是指向?qū)ο蟮闹羔樆驅(qū)ο笠肈)動(dòng)態(tài)聯(lián)編是在編譯時(shí)確定操作函數(shù)的注:先期聯(lián)編也稱(chēng)靜態(tài)聯(lián)編,遲后聯(lián)編也稱(chēng)動(dòng)態(tài)聯(lián)編。注釋?zhuān)簞?dòng)態(tài)聯(lián)編一直要到程序運(yùn)行時(shí)才能確定調(diào)用哪個(gè)函數(shù)。 虛函數(shù)是實(shí)現(xiàn)動(dòng)態(tài)聯(lián)編的必要條件之一。沒(méi)有虛函數(shù)一定不能實(shí)現(xiàn)動(dòng)態(tài)聯(lián)編,但 有虛函數(shù)存在時(shí),必須同時(shí)滿(mǎn)足下列條件,才能夠?qū)崿F(xiàn)動(dòng)態(tài)聯(lián)編: 類(lèi)之間滿(mǎn)足子類(lèi)型關(guān)系; 調(diào)用虛函數(shù)操作的是指向?qū)ο蟮闹羔樆蛘邔?duì)象引用:或者是由成員函數(shù)調(diào)用 虛函數(shù)。2 關(guān)于虛函數(shù)的描述中,正確的是_。

2、D A)虛函數(shù)是一個(gè)靜態(tài)成員函數(shù) B)虛函數(shù)是一個(gè)非成員函數(shù) C)虛函數(shù)既可以在函數(shù)說(shuō)明時(shí)定義,也可以在函數(shù)實(shí)現(xiàn)時(shí)定義D)派生類(lèi)的虛函數(shù)與基類(lèi)中對(duì)應(yīng)的虛函數(shù)具有相同的參數(shù)個(gè)數(shù)和類(lèi)型注釋?zhuān)禾摵瘮?shù)是非靜態(tài)的成員函數(shù)。它不能是友元函數(shù),但可以在另一個(gè)類(lèi)中被聲明為友元函數(shù)。 虛函數(shù)聲明只能出現(xiàn)在類(lèi)定義的函數(shù)原型聲明中,而不能在成員函數(shù)的函數(shù)體實(shí)現(xiàn)的時(shí)候。 派生類(lèi)的虛函數(shù)與基類(lèi)中對(duì)應(yīng)的虛函數(shù)必須滿(mǎn)足下列條件,否則派生類(lèi)中的虛函數(shù)將丟失其虛特性,在調(diào)用時(shí)進(jìn)行靜態(tài)聯(lián)編: 派生類(lèi)中的虛函數(shù)與基類(lèi)中的虛函數(shù)具有相同的名稱(chēng): 派生類(lèi)中的虛函數(shù)與基類(lèi)中的虛函數(shù)具有相同的參數(shù)個(gè)數(shù)和相同的對(duì)應(yīng)參數(shù) 類(lèi)型: 派生類(lèi)中的虛

3、函數(shù)與基類(lèi)中的虛函數(shù)的返回值或者相同,或者都返回指針或 引用,并且派生類(lèi)虛函數(shù)所返回的指針或引用的基類(lèi)型是基類(lèi)中的虛函數(shù)所 返回的指針或引用的基類(lèi)型的子類(lèi)型。3 在下面四個(gè)選項(xiàng)中,_是用來(lái)聲明虛函數(shù)的。A A)virtual B)public C)using D)false注釋?zhuān)赫f(shuō)明虛函數(shù)的一般格式如下: virtua1<函數(shù)返回類(lèi)型><函數(shù)名>(<參數(shù)表>)4 對(duì)虛函數(shù)的調(diào)用_。D A)一定使用動(dòng)態(tài)聯(lián)編 B)必須使用動(dòng)態(tài)聯(lián)編C)一定使用靜態(tài)聯(lián)編 D)不一定使用動(dòng)態(tài)聯(lián)編注釋?zhuān)簠⒁?jiàn)第1題的注釋。5 實(shí)現(xiàn)運(yùn)行時(shí)的多態(tài)性要使用_。D A)重載函數(shù) B)構(gòu)造函數(shù) C

4、)析構(gòu)函數(shù) D)虛函數(shù)注釋?zhuān)簠⒁?jiàn)第1題的注釋。6 要實(shí)現(xiàn)動(dòng)態(tài)聯(lián)編,必須通過(guò)_調(diào)用虛函數(shù)。A A)對(duì)象指針 B)成員名限定 C)對(duì)象名 D)派生類(lèi)名注釋?zhuān)簠⒁?jiàn)第1題的注釋。7 在派生類(lèi)中重新定義虛函數(shù)時(shí),除了_方面,其他方面都必須與基類(lèi)中相應(yīng)的 虛函數(shù)保持一致。D A)參數(shù)個(gè)數(shù) B)參數(shù)類(lèi)型 C)函數(shù)名稱(chēng) D)函數(shù)體注釋?zhuān)簠⒁?jiàn)第2題的注釋。8 下面關(guān)于構(gòu)造函數(shù)和析構(gòu)函數(shù)的描述,錯(cuò)誤的是_。D A)析構(gòu)函數(shù)中調(diào)用虛函數(shù)采用靜態(tài)聯(lián)編B)對(duì)虛析構(gòu)函數(shù)的調(diào)用可以采用動(dòng)態(tài)聯(lián)編C)當(dāng)基類(lèi)的析構(gòu)函數(shù)是虛函數(shù)時(shí),其派生類(lèi)的析構(gòu)函數(shù)也一定是虛函數(shù)D)構(gòu)造函數(shù)可以聲明為虛函數(shù)注釋?zhuān)簶?gòu)造函數(shù)不能聲明為虛函數(shù),但析構(gòu)函

5、數(shù)可以聲明為虛函數(shù)。當(dāng)基類(lèi)的析構(gòu)函數(shù)聲明為虛函數(shù)時(shí),無(wú)論派生類(lèi)是否使用virtual關(guān)鍵字說(shuō)明,派生類(lèi)的析構(gòu)函數(shù)一定是虛函數(shù),對(duì)缺省析構(gòu)函數(shù)亦然。而且,如果滿(mǎn)足動(dòng)態(tài)聯(lián)編的其他條件,對(duì)虛析構(gòu)函數(shù)的調(diào)用將采用動(dòng)態(tài)聯(lián)編。 構(gòu)造函數(shù)不能聲明為虛函數(shù),但在構(gòu)造函數(shù)中可以調(diào)用虛函數(shù)。在構(gòu)造函數(shù)或析構(gòu)函數(shù)中調(diào)用虛函數(shù),將采用靜態(tài)聯(lián)編。9 關(guān)于純虛函數(shù)和抽象類(lèi)的描述中,錯(cuò)誤的是_。C A)純虛函數(shù)是一種特殊的虛函數(shù),它沒(méi)有具體的實(shí)現(xiàn)B)抽象類(lèi)是指具有純虛函數(shù)的類(lèi)C)一個(gè)基類(lèi)中說(shuō)明有純虛函數(shù),該基類(lèi)的派生類(lèi)一定不再是抽象類(lèi)D)抽象類(lèi)只能作為基類(lèi)來(lái)使用,其純虛函數(shù)的實(shí)現(xiàn)由派生類(lèi)給出注釋?zhuān)簬в屑兲摵瘮?shù)的類(lèi)稱(chēng)為抽象類(lèi)

6、。抽象類(lèi)中的純虛函數(shù)的實(shí)現(xiàn)由派生類(lèi)給出: 但派生類(lèi)仍可不給出純虛函數(shù)的定義,繼續(xù)作為抽象類(lèi)存在。10 下列描述中,_是抽象類(lèi)的特性。D A)可以說(shuō)明虛函數(shù) B)可以進(jìn)行構(gòu)造函數(shù)重載C)可以定義友元函數(shù) D)不能說(shuō)明其對(duì)象注釋?zhuān)撼橄箢?lèi)區(qū)別于其他類(lèi)的最根本的特征是不能定義對(duì)象。11 _是一個(gè)在基類(lèi)中說(shuō)明的虛函數(shù),它在該基類(lèi)中沒(méi)有定義,但要求任何派生 類(lèi)都必須定義自己的版本。 C A)虛析構(gòu)函數(shù) B)虛構(gòu)造函數(shù) C)純虛函數(shù) D)靜態(tài)成員函數(shù)12 如果一個(gè)類(lèi)至少有一個(gè)純虛函數(shù),那么就稱(chēng)該類(lèi)為_(kāi)。A A)抽象類(lèi) B)虛基類(lèi) C)派生類(lèi) D)以上都不對(duì)13 以下_成員函數(shù)表示純虛函數(shù)。C A)virtu

7、al int vf(int); B)void vf(int)=0;C)virtual void vf()=0; D)virtual void vf(int)()注釋?zhuān)杭兲摵瘮?shù)的聲明格式如下: virtual<函數(shù)返回類(lèi)型><函數(shù)名>(<參數(shù)表>)=0; 注意純虛函數(shù)與虛函數(shù)體為空的區(qū)別。純虛函數(shù)根本就沒(méi)有函數(shù)體,而空的虛 函數(shù)的函數(shù)體為空:前者所在的類(lèi)是抽象類(lèi),不能直接進(jìn)行實(shí)例化,而后者所在的 類(lèi)是可以實(shí)例化的:14 下面的描述中,正確的是_。A A)virtual可以用來(lái)聲明虛函數(shù)B)含有純虛函數(shù)的類(lèi)是不可以用來(lái)創(chuàng)建對(duì)象的,因?yàn)樗翘摶?lèi)C)即使基類(lèi)的構(gòu)造

8、函數(shù)沒(méi)有參數(shù),派生類(lèi)也必須建立構(gòu)造函數(shù)D)靜態(tài)數(shù)據(jù)成員可以通過(guò)成員初始化列表來(lái)初始化注釋?zhuān)簐irtual關(guān)鍵字既可以用來(lái)聲明虛基類(lèi),也可以用來(lái)聲明虛函數(shù)。 含有純虛函數(shù)的類(lèi)是抽象類(lèi),它不能用來(lái)定義對(duì)象。 靜態(tài)數(shù)據(jù)成員的初始化必須在類(lèi)體外進(jìn)行。 如果所有的基類(lèi)和子對(duì)象構(gòu)造函數(shù)都不需要參數(shù),派生類(lèi)也不需要參數(shù)時(shí),派生類(lèi)構(gòu)造函數(shù)可以不定義。15 在下面程序中,A、B、C、D四句編譯時(shí)不會(huì)出現(xiàn)錯(cuò)誤的是_。A #include<iostream.h>class Base public: Base() Base(int c): count(c) virtual void print() con

9、st=0;private: int count;class Derived: public Basepublic: Derived() :Base(0 ) Derived(int c): Base(c) void print()const cout<<"Derived"<<endl; ;void main() Derived d(10); Base *pb; pb=&d; /A Base & cb=d; Derived dd=*pb; Derived &cd=cb; /C Base bb=d; /D注釋?zhuān)築和C處不符合賦值兼容

10、規(guī)則。D處出錯(cuò)是因?yàn)锽ase是含有純虛函數(shù)的抽象類(lèi),不能建立抽象類(lèi)的對(duì)象。 16 在下面程序中,A,B、C、D四句編譯時(shí)出現(xiàn)錯(cuò)誤的是_。C class A /A public: /B A( ) func(); /C virtual void func()=0; /D;注釋?zhuān)簶?gòu)造函數(shù)中不能調(diào)用純虛函數(shù),但可以調(diào)用虛函數(shù)。17 分析下面的程序,正確的輸出結(jié)果是_B#include <iostream.h>#include <string.h>class Basepublic: virtual char *fun() const=0;char* Base:fun() cons

11、t return “Base”; class Derivedl1: virtual public Basepublic:char* fun() const return "Derived11" ;class Derivedl2: virtual public Basepublic: char* fun() const return "Derivedl2" ;class Derived2: public Derivedl1,public Derivedl2public: char* fun() const char *ptr; ptr=new charst

12、rlen(Derivedl1:fun()+strlen(Derivedl2:fun()+l; strcpy(ptr,Derived11:fun(), strcat(ptr,Derived12:fun(); return ptr; ;void main() Base *pb; pb=new Derived11; cout<<pb->fun()<<endl; pb=new Derivedl2; cout<<pb->fun()<<endl; pb=new Derived2; cout<<pb->fun()<<e

13、ndl;A) B)Base Derivedl1Base Derivedl2Base Derivedl1Derivedl2C) D)Derivedl1 Derivedl2Derivedl1 Derivedl2Derivedl1Derivedl2 Derivedl1Derivedl2注釋?zhuān)簠⒁?jiàn)第1題、第2題的注釋。派生類(lèi)Derivedll、Derivedl2和Derived2中的函數(shù)fun與基類(lèi)Base中的虛函數(shù)fun的函數(shù)名、參數(shù)表和返回類(lèi)型都完全相同,因此這三個(gè)類(lèi)中的fun函數(shù)也是虛函數(shù),對(duì)fun函數(shù)的調(diào)用采用動(dòng)態(tài)聯(lián)編。二、填空題1 動(dòng)態(tài)聯(lián)編中直到_程序運(yùn)行_時(shí)才能確定調(diào)用哪個(gè)函數(shù);而靜態(tài)聯(lián)編

14、則是在_程序編譯時(shí)進(jìn)行的。 注釋?zhuān)簞?dòng)態(tài)聯(lián)編與靜態(tài)聯(lián)編的概念。2 靜態(tài)聯(lián)編所支持的多態(tài)性稱(chēng)為_(kāi)編譯時(shí)的_多態(tài)性,動(dòng)態(tài)聯(lián)編所支持的多態(tài)性則稱(chēng)為 _運(yùn)行時(shí)的_多態(tài)性,動(dòng)態(tài)多態(tài)性由_虛函數(shù)_來(lái)支持。 注釋?zhuān)涸贑+中,多態(tài)性的實(shí)現(xiàn)和聯(lián)編有關(guān)。3 對(duì)虛函數(shù)使用對(duì)象指針或引用調(diào)用,系統(tǒng)使用_動(dòng)態(tài)_聯(lián)編;使用對(duì)象調(diào)用時(shí)系統(tǒng)使用_靜態(tài)_聯(lián)編。注釋?zhuān)簠⒁?jiàn)選擇填空第1題、第5題的注釋。4 動(dòng)態(tài)聯(lián)編是在_虛函數(shù)_的支持下實(shí)現(xiàn)的,它通過(guò)_指針或引用來(lái)調(diào)用該函數(shù)操作。5 在一個(gè)成員函數(shù)內(nèi)調(diào)用一個(gè)虛函數(shù)時(shí),對(duì)該虛函數(shù)的調(diào)用進(jìn)行_動(dòng)態(tài)_聯(lián)編。6 在析構(gòu)函數(shù)中調(diào)用虛函數(shù)時(shí),采用_靜態(tài)_聯(lián)編。7 C+中_不支持_虛構(gòu)造函數(shù),但_

15、支持_虛析構(gòu)函數(shù)。 注釋?zhuān)簠⒁?jiàn)選擇填空第8題的注釋。 多態(tài)是不同的對(duì)象對(duì)同一消息有不同的行為特征,虛函數(shù)作為運(yùn)行過(guò)程中多態(tài)的基礎(chǔ),主要是針對(duì)對(duì)象的,而構(gòu)造函數(shù)的調(diào)用意味著要建立一個(gè)對(duì)象,這時(shí)必須確切地知道這個(gè)對(duì)象的類(lèi)型,并且,我們也不會(huì)為一個(gè)已存在的對(duì)象調(diào)用構(gòu)造函數(shù)。因此,虛構(gòu)造函數(shù)沒(méi)有意義。 在C+中可以聲明虛析構(gòu)函數(shù)。析構(gòu)函數(shù)的功能是在該類(lèi)對(duì)象消亡之前進(jìn)行一些必要的清理工作,如果一個(gè)類(lèi)的析構(gòu)函數(shù)是虛函數(shù),那么,由它派生而來(lái)的所有子類(lèi)的析構(gòu)函數(shù)也是虛函數(shù)。析構(gòu)函數(shù)設(shè)置為虛函數(shù)后,在使用指針調(diào)用時(shí)可以進(jìn)行動(dòng)態(tài)聯(lián)編,實(shí)現(xiàn)運(yùn)行時(shí)的多態(tài),從而保證使用基類(lèi)的指針就能夠調(diào)用適當(dāng)?shù)奈鰳?gòu)函數(shù)針對(duì)不同的對(duì)象進(jìn)

16、行清理工作。8 在類(lèi)定義中,將_=0_置于虛函數(shù)的函數(shù)原型的末尾可以將該函數(shù)聲明為純虛函數(shù)9 帶有_純虛函數(shù)_的類(lèi)稱(chēng)為抽象類(lèi),它只能作為_(kāi)基類(lèi)_來(lái)使用。 注釋?zhuān)撼橄箢?lèi)的作用主要有兩個(gè):直接用作基類(lèi)或用作指針或引用的基類(lèi)型。10 抽象類(lèi)不能_定義對(duì)象_,但可以_聲明抽象類(lèi)的指針或引用_作為參數(shù)類(lèi)型,函數(shù)返回類(lèi)型或顯式轉(zhuǎn)換類(lèi)型。 注釋?zhuān)鹤⒁獬橄箢?lèi)不能定義對(duì)象是指不能定義需要分配存儲(chǔ)空間的對(duì)象。因此可以聲明抽象類(lèi)的指針或引用,它們?cè)诔绦蜻\(yùn)行時(shí)可以指向并訪(fǎng)問(wèn)派生類(lèi)對(duì)象。11 下列程序的運(yùn)行結(jié)果如下: Derivel's Print() called.Derive2's Print()

17、called.根據(jù)結(jié)果將程序補(bǔ)充完整。#include <iostream.h>class Base public: Base(int i) b=i; _virtual void Print()=0;_protected: int b;class Derivel :public Basepublic:_Derive1( int i ):Base( i ) _ void Print() cout<<”Derive1s Print() called.”<<endl; ;class Derive2:public Base _public:Derive1(int i

18、 ): Base( i ) void Print( ) cout<<”Derive2s Print( ) called. “<< endl; _;void fun( _Base *obj _) obj->Print();void main() _Derive1 *d1=new Derive1(1);_ Derive2 *d2=new Derive2(2); fun(dl); fun(d2); 注釋?zhuān)号缮?lèi)Derived1和Derived2從基類(lèi)Base公有繼承,它們是Base的子類(lèi)型。 主程序中兩次調(diào)用fun函數(shù),該函數(shù)通過(guò)指針對(duì)象obj調(diào)用了Print函數(shù),得到

19、 了不同的輸出結(jié)果。而同樣的消息被不同類(lèi)型的對(duì)象接收時(shí)導(dǎo)致完全不同的行為, 恰好體現(xiàn)了面向?qū)ο蟮亩鄳B(tài)特性。根據(jù)運(yùn)行時(shí)的多態(tài)必須滿(mǎn)足的條件,Print函數(shù)一 定是一個(gè)虛函數(shù),并且在所有類(lèi)中都必須進(jìn)行定義。由于Base類(lèi)中的Print函數(shù)除 了提供一個(gè)公共的接口外,沒(méi)有其他的作用,所以最好定義為純虛函數(shù)。 12 將下列程序補(bǔ)充完整。#include <iostream.h>class convertpublic: convert(double i) vail=i; _virtual void compute( )=0;_protected: double val1; double va

20、l2;/liters to gallonsclass l_to_g:public convertpublic: _l_to_g(double i ) : convert(i) _ void compute() val2=val1/3.7854; cout<<val1 <<" liters is "<<val2<<" gallons."<<endl;/Fahrenheit to Celsiusclass f_to_c:public convert _public: f_to_c(double i

21、):convert( i ) void compute( ) val2=(val1-32)*5/9;cout<<val1<<”Fahrenheit is “<<val2<<”Celsius.”<<endl; ;void fun(_convert& f _) pute();void main() l_to_g lgobj(4); f_to_c fcobj(70); fun(lgobj); fun(fcobj);13 根據(jù)不同的輸出結(jié)果,在函數(shù)Tone中填入正確的語(yǔ)句。#include <iostream.h>clas

22、s Instrumentpublic: virtual void Print() const cout<<"Instrument:Print"<<endl; ;class Piano: public Instrumentpublic: void Print()const cout<<"Piano:Print"<<endl; ;class Guitar: public Instrumentpublic: void Print() const cout<<"Guitar:Print&quo

23、t;<<endl; ;void Tone(_) _void main()Guitar g; Tone(g); Piano p; Tone(p);(1)輸出結(jié)果為:Instrument: :PrintInstmment:Print(2)輸出結(jié)果為:Guitar: :PrintPiano: :Print(1) Instrument obj obj.Print( ) (2) Instrument& obj obj.Print( ) 參考第3題,第一次的輸出是由靜態(tài)聯(lián)編產(chǎn)生的,第二次的輸出是由動(dòng)態(tài)態(tài)聯(lián)編產(chǎn)生的。14 下列程序的運(yùn)行結(jié)果如下:Base's cons.Deriv

24、ed's cons.Derived's des.Base's des.根據(jù)結(jié)果將程序補(bǔ)充完整。#include <iostream.h>class Basepublic: Base() cout<<"Base's cons."<<endl; _virtual Base( )_ cout<<"Base's des."<<endl; ;class Derived:public Basepublic: Derived() cout<<"De

25、rived's cons."<<endl; Derived() cout<<"Derived's des."<<endl; ;void main() Base *ptr=_new Derived _ delete ptr;三、編程1 在作業(yè)1編程1的Point類(lèi)中完成賦值運(yùn)算符=、插入運(yùn)算符<<、比較運(yùn)算符=、!=和加法運(yùn)算符+、-的重載。#include <math.h>#include <iostream.h>class Point public: Point(float

26、x=0, float y=0, float z=0): x_(x), y_(y), z_(z) Point(const Point& p) : x_(p.x_), y_(p.y_), z_(p.z_) /形參point為常引用,它所引用的對(duì)象不能被更新,即傳給它的實(shí)參不能被更新 。 void negate() x_ *= -1; y_ *= -1; z_ *= -1; double norm() return sqrt(x_*x_ + y_*y_ + z_*z_); void print() cout << '(' << x_ <<

27、"," << y_ << "," << z_ << ")" Point& operator=(const Point& point); bool Point:operator=(const Point& point) const /常成員函數(shù),只有它才有資格操作常量和常對(duì)象 return x_ = point.x_ && y_ = point.y_ && z_ = point.z_;bool Point:operator!=(con

28、st Point& point) const return x_ != point.x_ | y_ != point.y_ | z_ != point.z_;friend Point operator+(const Point& p1, const Point& p2);friend Point operator-(const Point& p1, const Point& p2);friend ostream& operator<<(ostream& ostr, const Point& point); privat

29、e: float x_, y_, z_;Point operator+(const Point& p1, const Point& p2) return Point(p1.x_+p2.x_,p1.y_+p2.y_,p1.z_+p2.z_);Point operator-(const Point& p1, const Point& p2) return Point(p1.x_-p2.x_,p1.y_-p2.y_,p1.z_-p2.z_);ostream& operator<<(ostream& ostr, const Point&

30、; point) return ostr << "(" << point.x_ << "," <<point.y_ << "," <<point.z_ << ")"Point& Point:operator=(const Point& point) x_ = point.x_; y_ = point.y_; z_ = point.z_; return *this;void main() Point p(12,-3,4

31、),q(14,5,12),r1,r2; r1=p+q; cout<<r1<<r1.norm()<<endl; r2=p-q+r1; cout <<r2<<r2.norm()<<endl; if(r1=r2)cout <<"r1=r2"<<endl; else cout <<"r1!=r2"<<endl; 2 假設(shè)Point類(lèi)的坐標(biāo)為整型,對(duì)它重載+(自增)、-(自減)運(yùn)算符(包括前后綴)。#include <math.h>#

32、include <iostream.h>class Point public: Point(int x=0, int y=0, int z=0): x_(x), y_(y), z_(z) Point(const Point& p) : x_(p.x_), y_(p.y_), z_(p.z_) Point& operator +() x_+;y_+;z_+; return *this;/為什么要返回引用,下列代碼行嗎?/Point operator +() / x_+;y_+;z_+;/ return *this;/Point operator +(int) /為什么

33、不必返回引用 Point temp(*this);x_+;y_+;z_+; return temp;Point& operator -() x_-;y_-;z_-; return *this;Point operator -(int) Point temp(*this);x_-;y_-;z_-; return temp; void print() cout << '(' << x_ << "," << y_ << "," << z_ << &quo

34、t;)" private: int x_, y_, z_;void main() Point p(12,-3,4); cout << "p = " p.print(); Point q(p+); cout << "q = " q.print(); cout << "p = " p.print(); +(+p); cout << "p = " p.print(); p-; cout << "p = " p.print();3 定

35、義一個(gè)Shape基類(lèi),由它派生出Rectangle和Circle類(lèi),二者都有GetArea( )函數(shù)計(jì)算對(duì)象的面積。使用Rectangle 類(lèi)創(chuàng)建一個(gè)派生類(lèi)Square。#include <iostream.h>class Shape public:/Shape() /Shape() virtual float GetArea()return -1;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)/Circle()float GetArea()return 3.14*itsRadius*it

36、sRadius;private:float itsRadius;class Rectangle : public Shapepublic:Rectangle(float len,float width): itsLength(len),itsWidth(width);/Rectangle();float GetArea()return itsLength*itsWidth;float GetLength()return itsLength;float GetWidth()return itsWidth;private:float itsWidth;float itsLength;class S

37、quare:public Rectanglepublic:Square(float len);/Square(); Square:Square(float len): Rectangle(len,len) void main()Shape *sp;sp=new Circle(5);cout << "The area of the Circle is "<<sp->GetArea()<<endl;delete sp;sp=new Rectangle(4,6); cout << "The area of the R

38、ectangle is "<<sp->GetArea()<<endl;delete sp;sp=new Square(5); cout << "The area of the Square is "<<sp->GetArea()<<endl;delete sp;4 定義一個(gè)Shape抽象類(lèi),由它派生出Rectanglr和Circle類(lèi),二者都有GetArea( )函數(shù)計(jì)算對(duì)象的面積,GetPerim( ) 函數(shù)計(jì)算對(duì)象的周長(zhǎng)。#include <iostream.h>class S

39、hape public:/Shape() /Shape() virtual float GetArea()=0;virtual float GetPerim()=0;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)/Circle()float GetArea()return 3.14*itsRadius*itsRadius;float GetPerim()return 6.28*itsRadius;private:float itsRadius;class Rectangle : public Shap

40、epublic:Rectangle(float len,float width): itsLength(len),itsWidth(width);/Rectangle();virtual float GetArea()return itsLength*itsWidth;float GetPerim()return 2*(itsLength+itsWidth);private:float GetLength()return itsLength;float GetWidth()return itsWidth;private:float itsWidth;float itsLength;void m

41、ain()Shape *sp;sp=new Circle(5);cout << "The area of the Circle is "<<sp->GetArea()<<endl;cout << "The Perimeter of the Circle is "<<sp->GetPerim()<<endl;delete sp;sp=new Rectangle(4,6); cout << "The area of the Rectangle is &q

42、uot;<<sp->GetArea()<<endl;cout << "The Perimeter of the Rectangle is "<<sp->GetPerim()<<endl;delete sp;作業(yè)6答案:一,選擇題:1 D 2 D 3 A 4 D 5 D 6 A 7 D 8 D 9 C 10 D11 C 12 A 13 C 14 A 15 A 16 C 17 B二,填空題:2 程序運(yùn)行程序編譯2 編譯時(shí)的運(yùn)行時(shí)的虛函數(shù)3 動(dòng)態(tài)靜態(tài)4 虛函數(shù)指針或引用5 動(dòng)態(tài)6 靜態(tài)7不支持支持8 =09

43、純虛函數(shù)基類(lèi)10定義對(duì)象聲明抽象類(lèi)的指針或引用11 virtual void Print()=0;Derive1( int i ):Base( i ) public:Derive1(int i ): Base( i ) void Print( ) cout<<”Derive2s Print( ) called. “<< endl; Base *objDerive1 *d1=new Derive1(1);_ Derive2 *d2=new Derive2(2);12 virtual void compute( )=0;l_to_g(double i ) : convert

44、(i) public: f_to_c(double i ):convert( i ) void compute( ) val2=(val1-32)*5/9;cout<<val1<<”Fahrenheit is “<<val2<<”Celsius.”<<endl; convert& f 13 (1) Instrument obj obj.Print( ) (2) Instrument& obj obj.Print( )14virtual Base( )new Derived 三、編程1,#include <math

45、.h>#include <iostream.h>class Point public: Point(float x=0, float y=0, float z=0): x_(x), y_(y), z_(z) Point(const Point& p) : x_(p.x_), y_(p.y_), z_(p.z_) void negate() x_ *= -1; y_ *= -1; z_ *= -1; double norm() return sqrt(x_*x_ + y_*y_ + z_*z_); void print() cout << '(

46、9; << x_ << "," << y_ << "," << z_ << ")" Point& operator=(const Point& point); bool Point:operator=(const Point& point) const return x_ = point.x_ && y_ = point.y_ && z_ = point.z_;bool Point:operator!=(co

47、nst Point& point) const return x_ != point.x_ | y_ != point.y_ | z_ != point.z_;friend Point operator+(const Point& p1, const Point& p2);friend Point operator-(const Point& p1, const Point& p2);friend ostream& operator<<(ostream& ostr, const Point& point); priva

48、te: float x_, y_, z_;Point operator+(const Point& p1, const Point& p2) return Point(p1.x_+p2.x_,p1.y_+p2.y_,p1.z_+p2.z_);Point operator-(const Point& p1, const Point& p2) return Point(p1.x_-p2.x_,p1.y_-p2.y_,p1.z_-p2.z_);ostream& operator<<(ostream& ostr, const Point&am

49、p; point) return ostr << "(" << point.x_ << "," <<point.y_ << "," <<point.z_ << ")"Point& Point:operator=(const Point& point) x_ = point.x_; y_ = point.y_; z_ = point.z_; return *this;void main() Point p(12,-3,

50、4),q(14,5,12),r1,r2; r1=p+q; cout<<r1<<r1.norm()<<endl; r2=p-q+r1; cout <<r2<<r2.norm()<<endl; if(r1=r2)cout <<"r1=r2"<<endl; else cout <<"r1!=r2"<<endl; 2,#include <math.h>#include <iostream.h>class Point pu

51、blic: Point(int x=0, int y=0, int z=0): x_(x), y_(y), z_(z) Point(const Point& p) : x_(p.x_), y_(p.y_), z_(p.z_) Point& operator +() x_+;y_+;z_+; return *this;Point operator +(int) Point temp(*this);x_+;y_+;z_+; return temp;Point& operator -() x_-;y_-;z_-; return *this;Point operator -(i

52、nt) Point temp(*this);x_-;y_-;z_-; return temp; void print() cout << '(' << x_ << "," << y_ << "," << z_ << ")" private: int x_, y_, z_;void main() Point p(12,-3,4); cout << "p = " p.print(); Point q(p+);

53、 cout << "q = " q.print(); cout << "p = " p.print(); +(+p); cout << "p = " p.print(); p-; cout << "p = " p.print();3,#include <iostream.h>class Shape public:/Shape() /Shape() virtual float GetArea()return -1;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)/Circle()float GetArea()return 3.14*itsRadius*itsRadius;private:float itsRadius;class Rectangle : public Shapepublic:Rectangle(fl

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論