版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、<p><b> 面向?qū)ο蠛虲++</b></p><p> C++是目前所使用的眾多編程語言中的一種。為什么會(huì)有那么多的語言?為什么總會(huì)有新的語言不斷出現(xiàn)?編程語言可以幫助程序員方便實(shí)現(xiàn)從設(shè)計(jì)到實(shí)現(xiàn)。第一個(gè)編程語言對(duì)基本的機(jī)器結(jié)構(gòu)有很大的依賴性。這時(shí)程序的細(xì)節(jié)很笨重。只有硬件設(shè)計(jì)師了解如何在其他成分之外建立計(jì)算機(jī)體系,語言設(shè)計(jì)者同樣認(rèn)識(shí)到程序可以在一個(gè)高水平的環(huán)境編寫,因而是
2、程序員可以不必了解機(jī)器的細(xì)節(jié)。</p><p> 為什么有這么多高水平的編程語言?有很多語言可以訪問大型數(shù)據(jù)庫,格式化金融報(bào)告,控制機(jī)器人在工廠工作,處理機(jī)件,控制衛(wèi)星實(shí)時(shí)模擬核反應(yīng)堆,預(yù)測(cè)變化的大氣層狀況,下棋和繪制電路圖。每一個(gè)問題都需要不同的數(shù)據(jù)結(jié)構(gòu)和算法。程序語言作為一種工具幫助我們解決這些問題。然而,并不是一種程序語言可以最好的解決所有問題。新的語言被開發(fā)來更好的解決一類特定的問題。其他語言可以用來解
3、決不同的問題領(lǐng)域和多方面的用途。每個(gè)程序語言固定一個(gè)特定的程序體系或設(shè)計(jì)程序原理。使用多種多樣的語言建立不同的設(shè)計(jì)方案。這些方案被叫做程序范例,幫助我們思考問題,規(guī)范解決。</p><p><b> 通過范例設(shè)計(jì)軟件</b></p><p> 當(dāng)設(shè)計(jì)一個(gè)小的計(jì)算機(jī)程序或大的軟件系統(tǒng)是,我們要思考問題的解決模式。怎樣去設(shè)計(jì)一個(gè)模式?程序范例提供了許多不同的方法去設(shè)計(jì)
4、和思考軟件系統(tǒng)。一個(gè)范例可以被認(rèn)為是一個(gè)模式或者一個(gè)框架來設(shè)計(jì)和描述軟件結(jié)構(gòu)。這個(gè)模式幫助我們?nèi)ニ伎己鸵?guī)范解決。我們可以選擇一種語言,使用范例獨(dú)立的完成。當(dāng)所選擇的語言提供的結(jié)構(gòu)和機(jī)制符合范例時(shí),就很容易完成。一般來說,幾種語言可能屬于同一種范例。因此,一種范例可以被看作一種語言的類。</p><p> 一種語言不僅要符合一種范例,而且能夠使用多種范例提供的特性和特征?;旌险Z言,如C++,綜合了兩到三種范例。C
5、++包括了命令和程序范例的特性,例如,其前身—C,和面向?qū)ο蠓独?lt;/p><p> 命令范例:命令范例的特性是計(jì)算機(jī)的抽象模型和巨大的內(nèi)存存儲(chǔ)。這是計(jì)算機(jī)體系的von Neumann模型。計(jì)算命令,由一系列的命令組成,以代碼形式存儲(chǔ)。命令可以使機(jī)器找到解決方法,使用指定命令改變存儲(chǔ),變量讀取,算術(shù)和邏輯表達(dá)式,條件分枝控制執(zhí)行流。</p><p> 程序范例:它包括了命令范例,還有對(duì)
6、概念命令和表達(dá)的抽象機(jī)制。參數(shù),即存儲(chǔ)的一部分,被引進(jìn)在范例中。還包括重復(fù),選擇等特征。許多主流程序還是這種語言。程序范例在程序設(shè)計(jì)中首次引進(jìn)了抽象的概念。抽象可以把動(dòng)作和結(jié)果隔離。過程是抽象的表格,完成一些任務(wù)或功能。其他部分調(diào)用時(shí),只是正確有效的執(zhí)行,但不清楚過程的執(zhí)行。</p><p> 程序范例和ADT:數(shù)據(jù)抽象使一個(gè)數(shù)據(jù)對(duì)象的行為和它的描述或執(zhí)行相分離。用戶無法看到數(shù)據(jù)的基本操作,執(zhí)行可以方便的更改而
7、不影響程序的運(yùn)行。</p><p> 當(dāng)我們?cè)O(shè)計(jì)一個(gè)算法時(shí),需要一個(gè)特定的數(shù)據(jù)類型執(zhí)行算法的操作。如果可以定義變量的數(shù)據(jù)類型,而不影響到實(shí)際數(shù)據(jù)類型的運(yùn)行,就可以很容易的制訂出算法。通過定義數(shù)據(jù)的用法和操作,假定可以選擇任何一種運(yùn)行,這種定義就叫做抽象數(shù)據(jù)類型。抽象數(shù)據(jù)類型的使用使得算法的設(shè)計(jì)得到更大的推廣,使得我們?cè)谒惴ㄔO(shè)計(jì)時(shí),注重了算法的全面,而不會(huì)拘泥于運(yùn)行的細(xì)節(jié)。當(dāng)算法設(shè)計(jì)完成時(shí),實(shí)際的數(shù)據(jù)類型被執(zhí)行。
8、近來,程序語言擴(kuò)展到支持新的數(shù)據(jù)類型的定義和提供便利給數(shù)據(jù)抽象。</p><p> 面向?qū)ο蟮姆独核匀槐A袅嗽S多程序范例的特征,過程仍然是計(jì)算的主要形式。但是,程序不僅僅是抽象值的運(yùn)算,在面向?qū)ο蠓独N還有對(duì)對(duì)象的運(yùn)算。對(duì)象同抽象數(shù)據(jù)類型很相似,聯(lián)系著數(shù)據(jù)和運(yùn)算。</p><p> 面向?qū)ο蠓独哂腥N主要特性,第一種,壓縮,其機(jī)制是為了實(shí)施數(shù)據(jù)抽象。第二種,繼承。繼承允許從已存在
9、的對(duì)象中創(chuàng)建新的對(duì)象。這個(gè)新創(chuàng)建的對(duì)象是原對(duì)象的具體說明。新對(duì)象的不同在于只需要提供方法或數(shù)據(jù)。當(dāng)一個(gè)對(duì)象從另一個(gè)對(duì)象中被創(chuàng)建或取得時(shí),就說新對(duì)象繼承了它父對(duì)象的方法和數(shù)據(jù),并增加了一些新的描述和說明。面向?qū)ο蟮牡谌N特性是多態(tài)。多態(tài)可以使不同類型的的對(duì)象對(duì)相同的信息執(zhí)行相同的操作。例如,我們有一部分對(duì)象它們可以執(zhí)行一類操作,但是只有在運(yùn)行時(shí)我們才知道對(duì)象的類型。面向?qū)ο笳Z言包含的機(jī)制確保了每一類信息傳遞給正確的對(duì)象。</p>
10、;<p> 壓縮,繼承和多態(tài)被認(rèn)為是面向?qū)ο蟪绦虻幕咎卣鳎械拿嫦驅(qū)ο蟪绦蛘Z言必須提供這些特征。一般來說,語言通過不同的途徑支持這些特征的實(shí)現(xiàn)。Smalltalk, C++, Objective-C, 和 Lisp with CLOS (the Common Lisp Object System)這些程序語言都是面向?qū)ο笳Z言的例子,它們都可以提供對(duì)壓縮,繼承和多態(tài)的支持。</p><p>
11、構(gòu)建一個(gè)面向?qū)ο蟮某绦蛐枰獩Q定解決問題所需的對(duì)象。這些對(duì)象被用來構(gòu)建計(jì)算,定義軟件系統(tǒng)的操作運(yùn)行。信息的傳遞是對(duì)象間最基本的相互作用機(jī)制。信息(從其他的對(duì)象或程序)傳遞給對(duì)象,以便通知對(duì)象運(yùn)行下一個(gè)操作。對(duì)象需要負(fù)責(zé)維護(hù)它所相關(guān)的數(shù)據(jù)的狀態(tài)。只有對(duì)象本身才可以改變它內(nèi)部的數(shù)據(jù)值。對(duì)象本身可以完全的調(diào)用它的子對(duì)象。一個(gè)對(duì)象的執(zhí)行是一個(gè)循環(huán)遞歸的過程,當(dāng)定義這個(gè)對(duì)象和方法的初始值是,可以跳出這個(gè)循環(huán)遞歸的過程。這時(shí),這個(gè)方法和數(shù)據(jù)所組成的元
12、素可以使用程序語言所提供的基本的構(gòu)造函數(shù)。</p><p> 學(xué)習(xí)面向?qū)ο蠓独钪匾囊稽c(diǎn)是如何改變我們思考建造軟件體系的思路。系統(tǒng)被認(rèn)為是由多個(gè)單一獨(dú)立的個(gè)體組成,其中每個(gè)個(gè)體只負(fù)責(zé)對(duì)其自身的操作的運(yùn)行。每一個(gè)對(duì)象的設(shè)置和運(yùn)行都是自身所包含的。由于對(duì)象常常模仿真實(shí)世界的個(gè)體的概念,因而這樣的一個(gè)模型推動(dòng)了軟件方面的設(shè)計(jì)(以及后來的實(shí)行)。設(shè)計(jì)一個(gè)系統(tǒng)使用面向?qū)ο蟮姆独瑥亩沟孟到y(tǒng)的操作和運(yùn)行更類似于真實(shí)世界
13、中所對(duì)應(yīng)的真實(shí)個(gè)體。</p><p> C++面向?qū)ο蟮奶匦?lt;/p><p> 壓縮:C++繼承了C的發(fā)展,并且定義的新的方便的數(shù)據(jù)類型。一個(gè)類就像C的結(jié)構(gòu),但不同的是同時(shí)包括了數(shù)據(jù)和方法。除此之外,C++還提供了類中各個(gè)成員訪問權(quán)限的不同,以此方便的控制即使是在不同的類,也可以訪問類中的成員。</p><p> 重復(fù)的調(diào)用一個(gè)抽象的數(shù)據(jù)可以對(duì)用戶隱藏對(duì)一個(gè)數(shù)
14、據(jù)對(duì)象的操作細(xì)節(jié)。用戶只可以通過一個(gè)公共的接口來訪問這個(gè)對(duì)象。一個(gè)C++的類既包括共有的部分,又包括私有的部分。公有的部分提供給用戶關(guān)于這個(gè)類的接口,私有的部分只有構(gòu)造這個(gè)類的函數(shù)才可以訪問。</p><p> C++提供了關(guān)鍵字來說明類中哪些 成員是不可見的,哪些部分是其公共接口。不可見的成員在其定義的部分標(biāo)明了關(guān)鍵字private。公共的接口部分標(biāo)有關(guān)鍵字public。當(dāng)一個(gè)類是私有的類時(shí),意味著只有這個(gè)類
15、的函數(shù)或友元可以使用它。</p><p> 一個(gè)類的定義并不分配內(nèi)存。當(dāng)一個(gè)數(shù)據(jù)對(duì)象被創(chuàng)建并且具有變量聲明時(shí)才分配內(nèi)存。構(gòu)造函數(shù)和析構(gòu)函數(shù)提供了對(duì)象的初始化和釋放功能。當(dāng)一個(gè)對(duì)象被聲明時(shí),它的構(gòu)造函數(shù)初始化其在內(nèi)存的值。當(dāng)對(duì)象離開它所在的范圍時(shí),調(diào)用析構(gòu)函數(shù)釋放對(duì)象。</p><p> 我們不可能對(duì)用戶完全的隱藏操作的細(xì)節(jié)。C++無法提供一個(gè)使所有細(xì)節(jié)完全排除在基本操作之外的方法,因此
16、一個(gè)類的私有部分必須這個(gè)類的定義,從而有效的訪問這個(gè)類的變量,并可以繼承。建造一個(gè)類可以使它方便的訪問它父類的私有成員。C++定義了關(guān)鍵字protected來完成繼承的實(shí)現(xiàn)。保護(hù)成員既可以被類的成員函數(shù)訪問,也可以被派生類的成員函數(shù)訪問。和私有成員相同的一點(diǎn),保護(hù)成員不可以被用戶程序訪問。</p><p> 對(duì)象所需要注意的最后一點(diǎn),信息的重復(fù)傳遞時(shí)對(duì)象間交流的基本功能。例如一個(gè)語句i < a2.Size
17、(),我們可以有效的把信息傳給a2的對(duì)象,以確定組的大小并返回其值。實(shí)際上,這里并沒有真的傳遞信息。C++通過調(diào)用函數(shù)模擬信息的傳遞。編譯器確保對(duì)所需要的對(duì)象調(diào)用正確的函數(shù),因此,在C++中,可以把信息的傳遞當(dāng)作函數(shù)的調(diào)用。</p><p> 當(dāng)設(shè)計(jì)了一種方案,一種程序語言就可以被選擇執(zhí)行??梢栽谠O(shè)計(jì)的過程中找到對(duì)象間的繼承關(guān)系,甚至可以使用傳統(tǒng)的,非面向?qū)ο蟮恼Z言設(shè)計(jì)系統(tǒng)。但是,使用面向?qū)ο笳Z言,如C++,由
18、于語言支持對(duì)象間的繼承關(guān)系,從而可以更方便的了解設(shè)計(jì)執(zhí)行。</p><p> C++語言是混合型的語言,它屬于多范例的語言。C++可以認(rèn)為是C語言的擴(kuò)展的過程語言或面向?qū)ο笳Z言。</p><p><b> C++面向?qū)ο筇卣?lt;/b></p><p> C++繼承:任何面向?qū)ο蟪绦蛘Z言其最主要的作用就是可以從原有的類中創(chuàng)建新的類,從而可以再
19、次使用代碼。繼承可以從與原有的類型擴(kuò)展到派生類型。</p><p> 面向?qū)ο笤O(shè)計(jì)的一個(gè)主要操作就是要確認(rèn)真實(shí)世界的個(gè)體以及各個(gè)個(gè)體之間的聯(lián)系。當(dāng)設(shè)計(jì)一個(gè)軟件系統(tǒng)時(shí),需要建立一系列的對(duì)象,并且需要互相關(guān)聯(lián)的一系列對(duì)象。有些類之間可能不是完全的相關(guān)。大部分的類之間可以歸納為繼承的關(guān)系。將一系列的類歸為一個(gè)類的層次需要明白各個(gè)類之間的關(guān)系和他們之間的細(xì)節(jié)。但不是所有的類之間都可以建立繼承的關(guān)系。</p>
20、<p> C++提供了三種方式的繼承:公有繼承,私有繼承,保護(hù)繼承。這些不同的方式應(yīng)用在不同的對(duì)象間關(guān)系。</p><p> 第一種關(guān)系是IS-A關(guān)系。這種關(guān)系是類型與類型,類與類之間特有的關(guān)系。IS-A繼承指兩個(gè)類,其中一個(gè)類描述了對(duì)對(duì)象的說明,對(duì)對(duì)象的設(shè)置說明由另一個(gè)類來說明。IS-A的關(guān)系是傳統(tǒng)的繼承關(guān)系,也就是派生類型。派生類型是一些基本類型即父類型的專門化,在C++中,超級(jí)類型就是基類
21、,派生類型就是派生類。</p><p> 在C++中執(zhí)行IS-A關(guān)系需要使用公有繼承。在基類中的公共部分使用公有繼承,在派生類中也是公有的部分,在基類中的保護(hù)部分使用公有繼承,在派生類中也變成保護(hù)的部分</p><p> 有時(shí)在兩個(gè)對(duì)象之間的關(guān)系也許是包含關(guān)系,也可以是部分整體之間的關(guān)系。不是所有對(duì)象它都是其他對(duì)象的一個(gè)專門說明和繼承,可能這些對(duì)象有些是其他對(duì)象的一部分或是包含在其他的
22、對(duì)象中。</p><p> 在C++中使用HAS-A關(guān)系可以使用成員或是私有繼承。例如,一個(gè)堆棧可以被當(dāng)作一個(gè)數(shù)組使用。我們可以使用堆棧作為一個(gè)數(shù)據(jù)成員,或者使用私有繼承,從數(shù)組類中繼承堆棧類。</p><p> 可以在兩個(gè)類之間使用繼承創(chuàng)建一個(gè)互相包含的關(guān)系。私有繼承只有在不是接口的部分可以被使用,基類是一個(gè)細(xì)節(jié)的執(zhí)行。在私有繼承的情況下,原來基類中的公有部分和保護(hù)部分,在派生類中都
23、變?yōu)樗接胁糠?。派生類的使用者不可以訪問基類中的接口。但是派生類中的成員函數(shù)可以自由的訪問或調(diào)用基類中的公共部分和私有部分。當(dāng)這樣使用基類中的成員時(shí),用戶不可以只依靠繼承來編寫代碼。這是一個(gè)修改為不同的基類的運(yùn)行。繼承的另一種方式,也是很少使用的一種方式就是保護(hù)繼承。保護(hù)繼承也是一種可以使用HAS-A關(guān)系的繼承。當(dāng)使用保護(hù)繼承時(shí),在基類中的公有部分和保護(hù)部分,在派生類中就都變?yōu)楸Wo(hù)部分。當(dāng)繼承是接口的部分時(shí),派生類要使用保護(hù)繼承,但是不是
24、接口的所有部分都可以被用戶使用。一個(gè)保護(hù)的基類類似于一個(gè)私有的基類,不同在于繼承的派生類可以知道接口部分。</p><p> 如果可以的話,最好使用數(shù)據(jù)成員。這時(shí)當(dāng)你必須重復(fù)使用基類中的函數(shù)時(shí),可以使用其繼承。如果你所有的派生類是基類的一個(gè)說明構(gòu)造時(shí),只有使用公有繼承,否則,要使用私有繼承。當(dāng)在不必要時(shí)使用繼承,會(huì)使系統(tǒng)更加難以理解。</p><p> 總之,一個(gè)類有兩個(gè)主要的接口:一
25、個(gè)是給用戶所使用的(公共接口),另一個(gè)派生類所使用(公有部分和保護(hù)部分的聯(lián)合)。繼承的部分是基本相同的。在基類中的公共接口使用公有繼承,在派生類中隊(duì)用戶也是公有接口。在基類中的公共部分和保護(hù)部分使用保護(hù)繼承,在派生類中可以使用成員函數(shù),但并不對(duì)派生類的所有用戶。最后,當(dāng)在基類中的公共部分和保護(hù)部分使用私有繼承,基類可以調(diào)用,但用戶和派生類不可以使用。</p><p> C++多態(tài):多態(tài)是面向?qū)ο蟪绦虻娜齻€(gè)特性中
26、最重要的部分。多態(tài),和繼承一起,對(duì)面向?qū)ο蟪绦蛴泻艽蟮挠猛?。多態(tài),就是說有多個(gè)形式,提供一個(gè)基本的軟件接口以建立不同類型的對(duì)象操作的統(tǒng)一的方式。C++提供了三種類型的多態(tài):虛函數(shù),函數(shù)重載和運(yùn)算符重載。</p><p> 虛函數(shù)可以使繼承的同層相關(guān)的對(duì)象用同一種方式操作。當(dāng)所有的函數(shù)傳遞同樣的信息值時(shí),虛函數(shù)的機(jī)制確保在動(dòng)態(tài)運(yùn)行時(shí)了調(diào)用正確的函數(shù)。一個(gè)類的普通的成員函數(shù)是靜態(tài)的單一的過程,在編譯時(shí)調(diào)用。它只有一
27、個(gè)對(duì)成員函數(shù)的說明使用于類中所有的例子。</p><p> 當(dāng)定義一個(gè)成員函數(shù)時(shí),在它前面加上關(guān)鍵字virtual,實(shí)際過程的調(diào)用依賴于對(duì)象實(shí)例的類型,通過這樣來判斷如何調(diào)用。這是一個(gè)動(dòng)態(tài)的約束。靜態(tài)方式的束定也叫做早期束定。</p><p> 虛函數(shù)機(jī)制可以通過一個(gè)基類的參數(shù)或指針調(diào)用。一個(gè)基類的指針可以指向一個(gè)基本類型的對(duì)象或任何從基類派生出的類型的對(duì)象。虛函數(shù)也可以用在邏輯的運(yùn)算
28、上。一個(gè)類是抽象的類當(dāng)這個(gè)類是繼承的根節(jié)點(diǎn)時(shí)。一個(gè)類當(dāng)它只有虛函數(shù)的定義而沒有實(shí)現(xiàn)時(shí)是抽象類。這些函數(shù)在類定義時(shí)被賦予0值。派生類必須給出他們的實(shí)現(xiàn)。</p><p> 另一個(gè)多態(tài)的形式是函數(shù)重載。當(dāng)一個(gè)函數(shù)被使用在多個(gè)程序時(shí)就是重載。重載允許在同樣的名稱下執(zhí)行一系列相似的功能。當(dāng)有相同函數(shù)時(shí),編譯器通過檢查它的返回值和參數(shù)決定哪個(gè)被調(diào)用。</p><p> 當(dāng)我們定義新的數(shù)據(jù)類型時(shí),
29、需要定義相似類型的標(biāo)準(zhǔn)的操作。一個(gè)復(fù)數(shù)的類型要有加減的運(yùn)算。我們可以使用運(yùn)算符重載加減操作就像整形和浮點(diǎn)型數(shù)據(jù)。運(yùn)算符的定義和C++函數(shù)的定義相似,可以作為類的成員或非成員。運(yùn)算符有一個(gè)或兩個(gè)參數(shù)分別叫做單目或雙目運(yùn)算符。在C++中,一個(gè)運(yùn)算符函數(shù)的定義就像一個(gè)成員函數(shù)的定義,在其名字的前面加上關(guān)鍵字operator。C++在運(yùn)算符的重載上有一些限制,只有先定義運(yùn)算符的操作才可以被重載,只定義了運(yùn)算符就重載是錯(cuò)誤的。你不能把單目運(yùn)算符返
30、回到雙目運(yùn)算或多目運(yùn)算中。同時(shí),范圍運(yùn)算(`::'),成員運(yùn)算(`.*'),類運(yùn)算(`.'),選擇運(yùn)算(`?:')不能重載。</p><p> 在這后兩點(diǎn)的面向?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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 計(jì)算機(jī)畢業(yè)論文外文翻譯
- c++畢業(yè)論文外文翻譯
- 計(jì)算機(jī)英語畢業(yè)論文外文翻譯
- 計(jì)算機(jī)畢業(yè)論文外文翻譯10
- 計(jì)算機(jī)外文資料翻譯 --隱蔽方法在c++
- 計(jì)算機(jī)專業(yè)畢業(yè)論文外文翻譯7
- 計(jì)算機(jī)畢業(yè)論文外文翻譯--asp概述
- 計(jì)算機(jī)專業(yè)畢業(yè)論文外文翻譯15
- 計(jì)算機(jī)專業(yè)畢業(yè)論文外文翻譯--計(jì)算機(jī)病毒介紹
- 計(jì)算機(jī)外文翻譯--c++設(shè)計(jì)基本原理
- 計(jì)算機(jī)專業(yè)畢業(yè)論文外文翻譯2篇
- 計(jì)算機(jī)專業(yè)畢業(yè)論文外文翻譯--數(shù)據(jù)類型和值域
- 計(jì)算機(jī)專業(yè)畢業(yè)論文翻譯
- 鍋爐的計(jì)算機(jī)控制畢業(yè)論文外文翻譯
- 無線局域網(wǎng)-計(jì)算機(jī)畢業(yè)論文外文翻譯
- 計(jì)算機(jī)畢業(yè)論文范文畢業(yè)論文計(jì)算機(jī)專業(yè)
- 計(jì)算機(jī)專業(yè)畢業(yè)設(shè)計(jì)外文翻譯--jsp內(nèi)置對(duì)象
- c++面向?qū)ο?oop)
- 計(jì)算機(jī)畢業(yè)論文
- 計(jì)算機(jī)系畢業(yè)設(shè)計(jì)外文翻譯---面向?qū)ο髷?shù)據(jù)庫系統(tǒng)
評(píng)論
0/150
提交評(píng)論