2023年全國碩士研究生考試考研英語一試題真題(含答案詳解+作文范文)_第1頁
已閱讀1頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、<p><b>  面向?qū)ο蠛虲++</b></p><p>  C++是目前所使用的眾多編程語言中的一種。為什么會有那么多的語言?為什么總會有新的語言不斷出現(xiàn)?編程語言可以幫助程序員方便實現(xiàn)從設(shè)計到實現(xiàn)。第一個編程語言對基本的機器結(jié)構(gòu)有很大的依賴性。這時程序的細(xì)節(jié)很笨重。只有硬件設(shè)計師了解如何在其他成分之外建立計算機體系,語言設(shè)計者同樣認(rèn)識到程序可以在一個高水平的環(huán)境編寫,因而是

2、程序員可以不必了解機器的細(xì)節(jié)。</p><p>  為什么有這么多高水平的編程語言?有很多語言可以訪問大型數(shù)據(jù)庫,格式化金融報告,控制機器人在工廠工作,處理機件,控制衛(wèi)星實時模擬核反應(yīng)堆,預(yù)測變化的大氣層狀況,下棋和繪制電路圖。每一個問題都需要不同的數(shù)據(jù)結(jié)構(gòu)和算法。程序語言作為一種工具幫助我們解決這些問題。然而,并不是一種程序語言可以最好的解決所有問題。新的語言被開發(fā)來更好的解決一類特定的問題。其他語言可以用來解

3、決不同的問題領(lǐng)域和多方面的用途。每個程序語言固定一個特定的程序體系或設(shè)計程序原理。使用多種多樣的語言建立不同的設(shè)計方案。這些方案被叫做程序范例,幫助我們思考問題,規(guī)范解決。</p><p><b>  通過范例設(shè)計軟件</b></p><p>  當(dāng)設(shè)計一個小的計算機程序或大的軟件系統(tǒng)是,我們要思考問題的解決模式。怎樣去設(shè)計一個模式?程序范例提供了許多不同的方法去設(shè)計

4、和思考軟件系統(tǒng)。一個范例可以被認(rèn)為是一個模式或者一個框架來設(shè)計和描述軟件結(jié)構(gòu)。這個模式幫助我們?nèi)ニ伎己鸵?guī)范解決。我們可以選擇一種語言,使用范例獨立的完成。當(dāng)所選擇的語言提供的結(jié)構(gòu)和機制符合范例時,就很容易完成。一般來說,幾種語言可能屬于同一種范例。因此,一種范例可以被看作一種語言的類。</p><p>  一種語言不僅要符合一種范例,而且能夠使用多種范例提供的特性和特征?;旌险Z言,如C++,綜合了兩到三種范例。C

5、++包括了命令和程序范例的特性,例如,其前身—C,和面向?qū)ο蠓独?lt;/p><p>  命令范例:命令范例的特性是計算機的抽象模型和巨大的內(nèi)存存儲。這是計算機體系的von Neumann模型。計算命令,由一系列的命令組成,以代碼形式存儲。命令可以使機器找到解決方法,使用指定命令改變存儲,變量讀取,算術(shù)和邏輯表達(dá)式,條件分枝控制執(zhí)行流。</p><p>  程序范例:它包括了命令范例,還有對

6、概念命令和表達(dá)的抽象機制。參數(shù),即存儲的一部分,被引進在范例中。還包括重復(fù),選擇等特征。許多主流程序還是這種語言。程序范例在程序設(shè)計中首次引進了抽象的概念。抽象可以把動作和結(jié)果隔離。過程是抽象的表格,完成一些任務(wù)或功能。其他部分調(diào)用時,只是正確有效的執(zhí)行,但不清楚過程的執(zhí)行。</p><p>  程序范例和ADT:數(shù)據(jù)抽象使一個數(shù)據(jù)對象的行為和它的描述或執(zhí)行相分離。用戶無法看到數(shù)據(jù)的基本操作,執(zhí)行可以方便的更改而

7、不影響程序的運行。</p><p>  當(dāng)我們設(shè)計一個算法時,需要一個特定的數(shù)據(jù)類型執(zhí)行算法的操作。如果可以定義變量的數(shù)據(jù)類型,而不影響到實際數(shù)據(jù)類型的運行,就可以很容易的制訂出算法。通過定義數(shù)據(jù)的用法和操作,假定可以選擇任何一種運行,這種定義就叫做抽象數(shù)據(jù)類型。抽象數(shù)據(jù)類型的使用使得算法的設(shè)計得到更大的推廣,使得我們在算法設(shè)計時,注重了算法的全面,而不會拘泥于運行的細(xì)節(jié)。當(dāng)算法設(shè)計完成時,實際的數(shù)據(jù)類型被執(zhí)行。

8、近來,程序語言擴展到支持新的數(shù)據(jù)類型的定義和提供便利給數(shù)據(jù)抽象。</p><p>  面向?qū)ο蟮姆独核匀槐A袅嗽S多程序范例的特征,過程仍然是計算的主要形式。但是,程序不僅僅是抽象值的運算,在面向?qū)ο蠓独N還有對對象的運算。對象同抽象數(shù)據(jù)類型很相似,聯(lián)系著數(shù)據(jù)和運算。</p><p>  面向?qū)ο蠓独哂腥N主要特性,第一種,壓縮,其機制是為了實施數(shù)據(jù)抽象。第二種,繼承。繼承允許從已存在

9、的對象中創(chuàng)建新的對象。這個新創(chuàng)建的對象是原對象的具體說明。新對象的不同在于只需要提供方法或數(shù)據(jù)。當(dāng)一個對象從另一個對象中被創(chuàng)建或取得時,就說新對象繼承了它父對象的方法和數(shù)據(jù),并增加了一些新的描述和說明。面向?qū)ο蟮牡谌N特性是多態(tài)。多態(tài)可以使不同類型的的對象對相同的信息執(zhí)行相同的操作。例如,我們有一部分對象它們可以執(zhí)行一類操作,但是只有在運行時我們才知道對象的類型。面向?qū)ο笳Z言包含的機制確保了每一類信息傳遞給正確的對象。</p>

10、;<p>  壓縮,繼承和多態(tài)被認(rèn)為是面向?qū)ο蟪绦虻幕咎卣?,所有的面向?qū)ο蟪绦蛘Z言必須提供這些特征。一般來說,語言通過不同的途徑支持這些特征的實現(xiàn)。Smalltalk, C++, Objective-C, 和 Lisp with CLOS (the Common Lisp Object System)這些程序語言都是面向?qū)ο笳Z言的例子,它們都可以提供對壓縮,繼承和多態(tài)的支持。</p><p>  

11、構(gòu)建一個面向?qū)ο蟮某绦蛐枰獩Q定解決問題所需的對象。這些對象被用來構(gòu)建計算,定義軟件系統(tǒng)的操作運行。信息的傳遞是對象間最基本的相互作用機制。信息(從其他的對象或程序)傳遞給對象,以便通知對象運行下一個操作。對象需要負(fù)責(zé)維護它所相關(guān)的數(shù)據(jù)的狀態(tài)。只有對象本身才可以改變它內(nèi)部的數(shù)據(jù)值。對象本身可以完全的調(diào)用它的子對象。一個對象的執(zhí)行是一個循環(huán)遞歸的過程,當(dāng)定義這個對象和方法的初始值是,可以跳出這個循環(huán)遞歸的過程。這時,這個方法和數(shù)據(jù)所組成的元

12、素可以使用程序語言所提供的基本的構(gòu)造函數(shù)。</p><p>  學(xué)習(xí)面向?qū)ο蠓独钪匾囊稽c是如何改變我們思考建造軟件體系的思路。系統(tǒng)被認(rèn)為是由多個單一獨立的個體組成,其中每個個體只負(fù)責(zé)對其自身的操作的運行。每一個對象的設(shè)置和運行都是自身所包含的。由于對象常常模仿真實世界的個體的概念,因而這樣的一個模型推動了軟件方面的設(shè)計(以及后來的實行)。設(shè)計一個系統(tǒng)使用面向?qū)ο蟮姆独?,從而使得系統(tǒng)的操作和運行更類似于真實世界

13、中所對應(yīng)的真實個體。</p><p>  C++面向?qū)ο蟮奶匦?lt;/p><p>  壓縮:C++繼承了C的發(fā)展,并且定義的新的方便的數(shù)據(jù)類型。一個類就像C的結(jié)構(gòu),但不同的是同時包括了數(shù)據(jù)和方法。除此之外,C++還提供了類中各個成員訪問權(quán)限的不同,以此方便的控制即使是在不同的類,也可以訪問類中的成員。</p><p>  重復(fù)的調(diào)用一個抽象的數(shù)據(jù)可以對用戶隱藏對一個數(shù)

14、據(jù)對象的操作細(xì)節(jié)。用戶只可以通過一個公共的接口來訪問這個對象。一個C++的類既包括共有的部分,又包括私有的部分。公有的部分提供給用戶關(guān)于這個類的接口,私有的部分只有構(gòu)造這個類的函數(shù)才可以訪問。</p><p>  C++提供了關(guān)鍵字來說明類中哪些 成員是不可見的,哪些部分是其公共接口。不可見的成員在其定義的部分標(biāo)明了關(guān)鍵字private。公共的接口部分標(biāo)有關(guān)鍵字public。當(dāng)一個類是私有的類時,意味著只有這個類

15、的函數(shù)或友元可以使用它。</p><p>  一個類的定義并不分配內(nèi)存。當(dāng)一個數(shù)據(jù)對象被創(chuàng)建并且具有變量聲明時才分配內(nèi)存。構(gòu)造函數(shù)和析構(gòu)函數(shù)提供了對象的初始化和釋放功能。當(dāng)一個對象被聲明時,它的構(gòu)造函數(shù)初始化其在內(nèi)存的值。當(dāng)對象離開它所在的范圍時,調(diào)用析構(gòu)函數(shù)釋放對象。</p><p>  我們不可能對用戶完全的隱藏操作的細(xì)節(jié)。C++無法提供一個使所有細(xì)節(jié)完全排除在基本操作之外的方法,因此

16、一個類的私有部分必須這個類的定義,從而有效的訪問這個類的變量,并可以繼承。建造一個類可以使它方便的訪問它父類的私有成員。C++定義了關(guān)鍵字protected來完成繼承的實現(xiàn)。保護成員既可以被類的成員函數(shù)訪問,也可以被派生類的成員函數(shù)訪問。和私有成員相同的一點,保護成員不可以被用戶程序訪問。</p><p>  對象所需要注意的最后一點,信息的重復(fù)傳遞時對象間交流的基本功能。例如一個語句i < a2.Size

17、(),我們可以有效的把信息傳給a2的對象,以確定組的大小并返回其值。實際上,這里并沒有真的傳遞信息。C++通過調(diào)用函數(shù)模擬信息的傳遞。編譯器確保對所需要的對象調(diào)用正確的函數(shù),因此,在C++中,可以把信息的傳遞當(dāng)作函數(shù)的調(diào)用。</p><p>  當(dāng)設(shè)計了一種方案,一種程序語言就可以被選擇執(zhí)行??梢栽谠O(shè)計的過程中找到對象間的繼承關(guān)系,甚至可以使用傳統(tǒng)的,非面向?qū)ο蟮恼Z言設(shè)計系統(tǒng)。但是,使用面向?qū)ο笳Z言,如C++,由

18、于語言支持對象間的繼承關(guān)系,從而可以更方便的了解設(shè)計執(zhí)行。</p><p>  C++語言是混合型的語言,它屬于多范例的語言。C++可以認(rèn)為是C語言的擴展的過程語言或面向?qū)ο笳Z言。</p><p><b>  C++面向?qū)ο筇卣?lt;/b></p><p>  C++繼承:任何面向?qū)ο蟪绦蛘Z言其最主要的作用就是可以從原有的類中創(chuàng)建新的類,從而可以再

19、次使用代碼。繼承可以從與原有的類型擴展到派生類型。</p><p>  面向?qū)ο笤O(shè)計的一個主要操作就是要確認(rèn)真實世界的個體以及各個個體之間的聯(lián)系。當(dāng)設(shè)計一個軟件系統(tǒng)時,需要建立一系列的對象,并且需要互相關(guān)聯(lián)的一系列對象。有些類之間可能不是完全的相關(guān)。大部分的類之間可以歸納為繼承的關(guān)系。將一系列的類歸為一個類的層次需要明白各個類之間的關(guān)系和他們之間的細(xì)節(jié)。但不是所有的類之間都可以建立繼承的關(guān)系。</p>

20、<p>  C++提供了三種方式的繼承:公有繼承,私有繼承,保護繼承。這些不同的方式應(yīng)用在不同的對象間關(guān)系。</p><p>  第一種關(guān)系是IS-A關(guān)系。這種關(guān)系是類型與類型,類與類之間特有的關(guān)系。IS-A繼承指兩個類,其中一個類描述了對對象的說明,對對象的設(shè)置說明由另一個類來說明。IS-A的關(guān)系是傳統(tǒng)的繼承關(guān)系,也就是派生類型。派生類型是一些基本類型即父類型的專門化,在C++中,超級類型就是基類

21、,派生類型就是派生類。</p><p>  在C++中執(zhí)行IS-A關(guān)系需要使用公有繼承。在基類中的公共部分使用公有繼承,在派生類中也是公有的部分,在基類中的保護部分使用公有繼承,在派生類中也變成保護的部分</p><p>  有時在兩個對象之間的關(guān)系也許是包含關(guān)系,也可以是部分整體之間的關(guān)系。不是所有對象它都是其他對象的一個專門說明和繼承,可能這些對象有些是其他對象的一部分或是包含在其他的

22、對象中。</p><p>  在C++中使用HAS-A關(guān)系可以使用成員或是私有繼承。例如,一個堆??梢员划?dāng)作一個數(shù)組使用。我們可以使用堆棧作為一個數(shù)據(jù)成員,或者使用私有繼承,從數(shù)組類中繼承堆棧類。</p><p>  可以在兩個類之間使用繼承創(chuàng)建一個互相包含的關(guān)系。私有繼承只有在不是接口的部分可以被使用,基類是一個細(xì)節(jié)的執(zhí)行。在私有繼承的情況下,原來基類中的公有部分和保護部分,在派生類中都

23、變?yōu)樗接胁糠帧E缮惖氖褂谜卟豢梢栽L問基類中的接口。但是派生類中的成員函數(shù)可以自由的訪問或調(diào)用基類中的公共部分和私有部分。當(dāng)這樣使用基類中的成員時,用戶不可以只依靠繼承來編寫代碼。這是一個修改為不同的基類的運行。繼承的另一種方式,也是很少使用的一種方式就是保護繼承。保護繼承也是一種可以使用HAS-A關(guān)系的繼承。當(dāng)使用保護繼承時,在基類中的公有部分和保護部分,在派生類中就都變?yōu)楸Wo部分。當(dāng)繼承是接口的部分時,派生類要使用保護繼承,但是不是

24、接口的所有部分都可以被用戶使用。一個保護的基類類似于一個私有的基類,不同在于繼承的派生類可以知道接口部分。</p><p>  如果可以的話,最好使用數(shù)據(jù)成員。這時當(dāng)你必須重復(fù)使用基類中的函數(shù)時,可以使用其繼承。如果你所有的派生類是基類的一個說明構(gòu)造時,只有使用公有繼承,否則,要使用私有繼承。當(dāng)在不必要時使用繼承,會使系統(tǒng)更加難以理解。</p><p>  總之,一個類有兩個主要的接口:一

25、個是給用戶所使用的(公共接口),另一個派生類所使用(公有部分和保護部分的聯(lián)合)。繼承的部分是基本相同的。在基類中的公共接口使用公有繼承,在派生類中隊用戶也是公有接口。在基類中的公共部分和保護部分使用保護繼承,在派生類中可以使用成員函數(shù),但并不對派生類的所有用戶。最后,當(dāng)在基類中的公共部分和保護部分使用私有繼承,基類可以調(diào)用,但用戶和派生類不可以使用。</p><p>  C++多態(tài):多態(tài)是面向?qū)ο蟪绦虻娜齻€特性中

26、最重要的部分。多態(tài),和繼承一起,對面向?qū)ο蟪绦蛴泻艽蟮挠猛?。多態(tài),就是說有多個形式,提供一個基本的軟件接口以建立不同類型的對象操作的統(tǒng)一的方式。C++提供了三種類型的多態(tài):虛函數(shù),函數(shù)重載和運算符重載。</p><p>  虛函數(shù)可以使繼承的同層相關(guān)的對象用同一種方式操作。當(dāng)所有的函數(shù)傳遞同樣的信息值時,虛函數(shù)的機制確保在動態(tài)運行時了調(diào)用正確的函數(shù)。一個類的普通的成員函數(shù)是靜態(tài)的單一的過程,在編譯時調(diào)用。它只有一

27、個對成員函數(shù)的說明使用于類中所有的例子。</p><p>  當(dāng)定義一個成員函數(shù)時,在它前面加上關(guān)鍵字virtual,實際過程的調(diào)用依賴于對象實例的類型,通過這樣來判斷如何調(diào)用。這是一個動態(tài)的約束。靜態(tài)方式的束定也叫做早期束定。</p><p>  虛函數(shù)機制可以通過一個基類的參數(shù)或指針調(diào)用。一個基類的指針可以指向一個基本類型的對象或任何從基類派生出的類型的對象。虛函數(shù)也可以用在邏輯的運算

28、上。一個類是抽象的類當(dāng)這個類是繼承的根節(jié)點時。一個類當(dāng)它只有虛函數(shù)的定義而沒有實現(xiàn)時是抽象類。這些函數(shù)在類定義時被賦予0值。派生類必須給出他們的實現(xiàn)。</p><p>  另一個多態(tài)的形式是函數(shù)重載。當(dāng)一個函數(shù)被使用在多個程序時就是重載。重載允許在同樣的名稱下執(zhí)行一系列相似的功能。當(dāng)有相同函數(shù)時,編譯器通過檢查它的返回值和參數(shù)決定哪個被調(diào)用。</p><p>  當(dāng)我們定義新的數(shù)據(jù)類型時,

29、需要定義相似類型的標(biāo)準(zhǔn)的操作。一個復(fù)數(shù)的類型要有加減的運算。我們可以使用運算符重載加減操作就像整形和浮點型數(shù)據(jù)。運算符的定義和C++函數(shù)的定義相似,可以作為類的成員或非成員。運算符有一個或兩個參數(shù)分別叫做單目或雙目運算符。在C++中,一個運算符函數(shù)的定義就像一個成員函數(shù)的定義,在其名字的前面加上關(guān)鍵字operator。C++在運算符的重載上有一些限制,只有先定義運算符的操作才可以被重載,只定義了運算符就重載是錯誤的。你不能把單目運算符返

30、回到雙目運算或多目運算中。同時,范圍運算(`::'),成員運算(`.*'),類運算(`.'),選擇運算(`?:')不能重載。</p><p>  在這后兩點的面向?qū)ο蠓治鲋?,我們可以看到C++很好的支持了面向?qū)ο蟮姆独?lt;/p><p>  Object-Orientation and C++</p><p>  C++ is jus

31、t one of many programming languages in use today. Why are there so many languages? Why do new ones appear every few years? Programming languages have evolved to help programmers ease the transition from design to impleme

32、ntation. </p><p>  The first programming languages were very dependent on the underlying machine architecture. Writing programs at this level of detail is very cumbersome. Just as hardware engineers learned

33、how to build computer systems out of other components, language designers also realized that programs could be written at a much higher level, thereby shielding the programmer from the details of the underlying machine.

34、</p><p>  Why are there such a large number of high-level programming languages? There are languages for accessing large inventory databases, formatting financial reports, controlling robots on the factory f

35、loor, processing lists, controlling satellites in real time, simulating a nuclear reactor, predicting changing atmospheric conditions, playing chess, and drawing circuit boards. Each of these problems requires different

36、sets of data structures and algorithms. Programming languages are tools to help us </p><p>  Each programming language imparts a particular programming style or design philosophy on its programmers. With the

37、 multitude of programming languages available today, a number of such design philosophies have emerged. These design philosophies, called programming paradigms, help us to think about problems and formulate solutions.<

38、;/p><p>  Software Design through Paradigms</p><p>  When designing small computer programs or large software systems, we often have a mental model of the problem we are trying to solve. How do we

39、devise a mental model of a software system? Programming paradigms offer many different ways of designing and thinking about software systems. A paradigm can be thought of as a mental model or as a framework for designing

40、 and describing a software system's structure. The model helps us think about and formulate solutions. </p><p>  We can use the mental model of a paradigm independently from the programming language chos

41、en for implementation. However, when the chosen language provides constructs and mechanisms that are similar to those that are found in the paradigm, the implementation will be more straightforward. Usually, there are se

42、veral languages that belong to a paradigm. For this reason, a programming paradigm is also considered a class of languages. </p><p>  A language does not have to fit into just one paradigm. More often, langu

43、ages provide features or characteristics from several paradigms. Hybrid languages, such as C++, combine characteristics from two or more paradigms. C++ includes characteristics from the imperative and procedural paradigm

44、s -- just like its predecessor language, C -- and the object-oriented paradigm. </p><p>  THE IMPERATIVE PARADIGM. The imperative paradigm is characterized by an abstract model of a computer with a large mem

45、ory store. This is the classic von Neumann model of computer architecture. Computations, which consist of a sequence of commands, are stored as encoding within the store. Commands enable the machine to find solutions usi

46、ng assignment to modify the store, variables to read the store, arithmetic and logic to evaluate expressions, and conditional branching to control the flow of exec</p><p>  THE PROCEDURAL PARADIGM. The proce

47、dural paradigm includes the imperative paradigm, but extends it with an abstraction mechanism for generalizing commands and expressions into procedures. Parameters, which are essentially aliases for a portion of the stor

48、e, were also introduced by this paradigm. Other features include iteration, recursion, and selection. Most mainstreams programming today is done in a procedural language. </p><p>  The procedural paradigm wa

49、s the first paradigm to introduce the notion of abstraction into program design. The purpose of abstraction in programming is to separate behavior from implementation. Procedures are a form of abstraction. The procedure

50、performs some task or function. Other parts of the program call the procedure, knowing that it will perform the task correctly and efficiently, but without knowing exactly how the procedure is implemented. </p>&l

51、t;p>  THE PROCEDURAL PARADIGM WITH ADTs. DATA ABSTRACTION is concerned with separating the behavior of a data object from its representation or implementation. For example, a stack contains the operations Push, Pop, a

52、nd IsEmpty. A stack object provides users with these operations, but does not reveal how the stack is actually implemented. The stack could be implemented using an array or a list. Users of the stack object do not care h

53、ow the stack is implemented, only that it performs the above operatio</p><p>  When we design algorithms, we often need a particular data type to use in order to carry out the algorithm's operations. The

54、 design of an algorithm is easier if we simply specify the data types of the variables, without worrying about how the actual data type is implemented. We describe the data type by its properties and operations and assum

55、e that whatever implementation is chosen, the operations will work correctly and efficiently. Types defined in this way are called ABSTRACT DATA TYPES (ADTs</p><p>  The use of abstract data types makes the

56、design of the algorithm more general, and allows us to concentrate on the algorithm at hand without getting bogged down in implementation details. After the algorithms have been designed, the actual data types will need

57、to be implemented, along with the algorithms. Recently, procedural languages have been extended to support the definition of new data types and provide facilities for data abstraction. </p><p>  THE OBJECT-O

58、RIENTED PARADIGM. The object- oriented paradigm retains much of the characteristics of the procedural paradigm, since procedures are still the primary form for composing computations. However, rather than operate on abst

59、ract values, programs in the object-oriented paradigm operate on objects. An object is very similar to an abstract data type and contains data as well as procedures. </p><p>  There are three primary charact

60、eristics of the object-oriented paradigm. We have already described the first, ENCAPSULATION, the mechanism for enforcing data abstraction. The second characteristic is INHERITANCE. Inheritance allows new objects to be c

61、reated from existing, more general ones. The new object becomes a specialized version of the general object. New objects need only provide the methods or data that differ because of the specialization. When an object is

62、created (or derived) from ano</p><p>  The third and final characteristic of object-oriented programming is POLYMORPHISM. Polymorphism allows many different types of objects to perform the same operation by

63、responding to the same message. For example, we may have a collection of objects which can all perform a sort operation. However, we do not know what types of objects will be created until run-time. Object-oriented langu

64、ages contain mechanisms for ensuring that each sort message is sent to the right object. </p><p>  Encapsulation, inheritance, and polymorphism are considered the fundamental characteristics of object-orient

65、ed programming and all object-oriented languages must provide these characteristics in some way. Not surprisingly, languages support these characteristics in very different ways. Smalltalk, C++, Objective-C, and Lisp wit

66、h CLOS (the Common Lisp Object System) are all examples of object-oriented languages, and each provides support for encapsulation, inheritance, and polymorphism. </p><p>  Constructing an object-oriented pro

67、gram involves determining the objects that are needed to solve the problem. The objects are then used to construct computations that define the behavior of the software system. Message passing is the fundamental interact

68、ion mechanism among objects. Messages (from other objects or programs) are sent to objects to inform them to perform one of their operations. </p><p>  Objects are responsible for maintaining the state of th

69、eir data. Only the object may modify its internal data. Objects may themselves be implemented via other sub-objects. Implementing an object involves a recursive process of breaking it into sub-objects until at some level

70、 the objects and methods defined on them are primitives. At this point, the methods and data consist of elements that can be implemented using the basic constructs provided by the programming language. </p><p&

71、gt;  One of the most important aspects of the object-oriented paradigm is how it changes our way of thinking about software systems. Systems are thought of as consisting of individual entities that are responsible for ca

72、rrying out their own operations. Each object is conceived and implemented as self-contained. Such a model facilitates software design (and later implementation) because objects often model conceptual real-world entities.

73、 Designing systems using the object-oriented paradigm results in </p><p>  The Object-Oriented Characteristics of C++</p><p>  ENCAPSULATION in C++. C++ extends C with a facility for defining ne

74、w data types. A class is like a C struct, but contains data as well as methods. In addition, C++ provides different levels of access to the members of a class in order to control how the members of a class can be manipul

75、ated from outside the class. </p><p>  Recall that the importance of data abstraction is to hide the implementation details of a data object from the user. The user only accesses the object through its PUBLI

76、C INTERFACE. A C++ class consists of a public and private part. The public part provides the interface to the users of the class, while the private part can only be used by the functions that make up the class. </p>

77、;<p>  C++ provides keywords to indicate which members of a class are hidden and which are part of its public interface. The members of the hidden implementation are marked in sections beginning with the keyword p

78、rivate. The public interface part of the class follows the keyword public. By default, the declarations within a class are private, meaning that only the member functions (and friends) of the class have access to them. &

79、lt;/p><p>  A class definition does not allocate any memory. Memory is allocated when an array object is created through a variable declaration. Constructors and destructors provide the initialization and clean

80、 up of an object. When an object is declared, the constructor is called to initialize the memory used by the object. The destructor performs any clean-up for the object when the object goes out of scope and is destroyed.

81、</p><p>  Note that we didn't really hide the implementation details from the user. C++ does not provide a way to completely exclude all of the details of the underlying implementation, since the private

82、 part of the class must be included with the class definition it is useful to relax the access to variables within a class, particularly under inheritance. Often derived classes need easy access to the private members of

83、 their parent classes. C++ defines the keyword protected for this purpose. Protected me</p><p>  One final note about objects. Recall that message passing is the fundamental means for communication among obj

84、ects. When we write i < a2.Size() we are effectively sending a message to the a2 array object to determine the size of the array and return it. In actuality, no message is really sent. C++ emulates message passing thr

85、ough the use of function calls. The compiler ensures us that the correct function will be called for the desired object. So, in C++ you can think of message passing as funct</p><p>  Object-orientation has b

86、ecome a buzzword with many meanings. It is a design methodology, a paradigm (a way of thinking about problems and finding solutions), and a form of programming. As a design methodology, we can use object-oriented techniq

87、ues to design software systems. But it is more than a design methodology, it is a whole new way of thinking about problems. Object-oriented design allows us to think about the actual real-world entities of the problem we

88、 are attempting to provide a solutio</p><p>  Once a design has been conceived, a programming language can be chosen for implementation. By factoring out the inheritance relationships from the object hierarc

89、hies discovered during design, one can even implement the system in a traditional, non- object-oriented language. However, using an object-oriented language, such as C++, makes it easier to realize the design into an imp

90、lementation because the inherent relationships among objects can be directly supported in the language. </p><p>  Languages such as C++ are considered hybrid languages because they are multi-paradigm languag

91、es. C++ is an object- oriented extension of C and can be used as a procedural language or as an object-oriented language. In this issue, we continue our tour of the object-oriented features of C++. </p><p> 

92、 The Object-Oriented Features of C++</p><p>  INHERITANCE in C++. One of the major strengths of any object-oriented programming language is the ability to build other classes from existing classes, thereby r

93、eusing code. Inheritance allows existing types to be extended to an associated collection of sub-types. </p><p>  Recall that one of the key actions of object-oriented design is to identify real-world entiti

94、es and the relationships among them. When a software system is designed, a variety of objects arise, which may be related in one way or another. Some classes may not be related at all. Many times it makes sense to organi

95、ze the object classes into an inheritance hierarchy. Organizing a set of classes into a class hierarchy requires that we understand the relationships among the classes in detail. Not all </p><p>  C++ provid

96、es three forms of inheritance: public, private, and protected. These different forms are used for different relation- ships between objects. To illustrate these different types of inheritance, let's look at several d

97、ifferent class relationships. </p><p>  The first relationship is the IS-A relationship. This type of relationship represents a specialization between types or classes. IS-A inheritance holds for two classes

98、 if the objects described by one class belongs to the set of objects described by the other more general class. The IS-A relationship is the traditional form of inheritance called subtyping. The subtype is a specializati

溫馨提示

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

評論

0/150

提交評論