【正文】
o considered a class of languages. A language does not have to fit into just one paradigm. More often, languages provide features or characteristics from several paradigms. Hybrid languages, such as C++, bine characteristics from two or more paradigms. C++ includes characteristics from the imperative and procedural paradigms just like its predecessor language, C and the objectoriented paradigm. THE IMPERATIVE PARADIGM. The imperative paradigm is characterized by an abstract model of a puter with a large memory store. This is the classic von Neumann model of puter architecture. Computations, which consist of a sequence of mands, are stored as encoding within the store. Commands enable the machine to find solutions using assignment to modify the store, variables to read the store, arithmetic and logic to evaluate expressions, and conditional branching to control the flow of execution. THE PROCEDURAL PARADIGM. The procedural paradigm includes the imperative paradigm, but extends it with an abstraction mechanism for generalizing mands and expressions into procedures. Parameters, which are essentially aliases for a portion of the store, were also introduced by this paradigm. Other features include iteration, recursion, and selection. Most mainstreams programming today is done in a procedural language. The procedural paradigm was 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 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. 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, and 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 how the stack is implemented, only that it performs the above operations correctly and efficiently. Because the underlying implementation of the data object is hidden from its users, the implementation can easily be changed without affecting the programs that use it. When we design algorithms, we often need a particular data type to use in order to carry out the algorithm39。你不能把單目運(yùn)算符返回到雙目運(yùn)算或多目運(yùn)算中。當(dāng)有相同函數(shù)時(shí),編譯器通過檢查它的返回值和參數(shù)決定哪個(gè)被調(diào)用。虛函數(shù)也可以用在邏輯的運(yùn)算上。當(dāng)所有的函數(shù)傳遞同樣的信息值時(shí),虛函數(shù)的機(jī)制確保在動(dòng)態(tài)運(yùn)行時(shí)了調(diào)用正確的函數(shù)。在基類中的公共接口使用公有繼承,在派生類中隊(duì)用戶也是公有接口。當(dāng)繼承是接口的部分時(shí),派生類要使用保護(hù)繼承,但是不是接口的所有部分都可以被用戶使用。在私有繼承的情況下,原來基類中的公有部分和保護(hù)部分,在派生類中都變?yōu)樗接胁糠帧? 在 C++中執(zhí)行 ISA 關(guān)系需要使用公有繼承。但不是所有的類之間都可以建立繼承的關(guān)系。 C++可以認(rèn)為是 C 語言的擴(kuò)展的過程語言或面向?qū)ο笳Z言。例如一個(gè)語句 i (),我們可以有效的把信息傳給 a2 的對(duì)象,以確定組的大小并返回其值。當(dāng)對(duì)象離開它所在的范圍時(shí),調(diào)用析構(gòu)函數(shù)釋放對(duì)象。 C++提供了關(guān)鍵字來說明類中哪些 成員是不可見的,哪 些部分是其公共接口。設(shè)計(jì) 3 一個(gè)系統(tǒng)使用面向?qū)ο蟮姆独?,從而使得系統(tǒng)的操作和運(yùn)行更類似于真實(shí)世界中所對(duì)應(yīng)的真實(shí)個(gè)體。只有對(duì)象本身才可以改變它內(nèi)部的數(shù)據(jù)值。 壓縮,繼承和多態(tài)被認(rèn)為是面向?qū)ο蟪绦虻幕咎卣?,所有的面向?qū)ο蟪绦蛘Z言必須提供這些特征。繼承允許從已存在的對(duì)象中創(chuàng)建新的對(duì)象。抽象數(shù)據(jù)類型的使用使得算法的設(shè)計(jì)得到更大的推廣,使得我們?cè)谒惴ㄔO(shè)計(jì)時(shí),注重了算法的全面,而不會(huì)拘泥于運(yùn)行的細(xì)節(jié)。抽象可以把動(dòng)作和結(jié)果隔離。這是計(jì)算機(jī)體系的 von Neumann 模型。我們可以選擇一種語言,使用范例獨(dú)立的完成。其他語言可以用來解決不同的問題領(lǐng)域和多方面的用途。第一個(gè)編程語言對(duì)基本的機(jī)器結(jié)構(gòu)有很大的依賴性。 為什么有這么多高水平的編程語言?有很多語言可以訪問大型數(shù)據(jù)庫,格式化金融報(bào)告,控制機(jī)器人在工廠工作,處理機(jī)件,控制衛(wèi)星實(shí)時(shí)模擬核反應(yīng)堆,預(yù)測(cè)變化 的大氣層狀況,下棋和繪制電路圖。這些方案被叫做程序范例,幫助我們思考問題,規(guī)范解決。因此,一種范例可以被看作一種語言的類。 程序范例:它包括了命令范例,還有對(duì)概念命令和表達(dá)的抽象機(jī)制。 2 程序范例和 ADT:數(shù)據(jù)抽象使一個(gè)數(shù)據(jù)對(duì)象的行為和它的描述或執(zhí)行相分離。 面向?qū)ο蟮姆独核匀槐A袅嗽S多程序范例的特征,過程仍然是計(jì)算的主要形式。當(dāng)一個(gè)對(duì)象從另一個(gè)對(duì)象中被創(chuàng)建或取得時(shí) ,就說新對(duì)象繼承了它父對(duì)象的方法和數(shù)據(jù),并增加了一些新的描述和說明。 構(gòu)建一個(gè)面向?qū)ο蟮某绦蛐枰獩Q定解決問題所需的對(duì)象。這時(shí),這個(gè)方法和數(shù)據(jù)所組成的元素可以使用程序語言所提供的基本的構(gòu)造函數(shù)。除此之外, C++還提供了類中各個(gè)成員訪問權(quán)限的不同,以此方便的控制即使是在不同的類,也可以訪問類中的成員。當(dāng)一個(gè)類是私有的類時(shí),意味著只有這個(gè)類的函數(shù)或友元可以使用它。建造一個(gè)類可以使它方便的訪問它父類的私有成員。編譯器確保對(duì)所需要的對(duì)象調(diào)用正確的函數(shù),因此,在 C++中,可以把信息的傳遞當(dāng)作函數(shù)的調(diào)用。 面向?qū)ο笤O(shè)計(jì)的一個(gè)主要操作就是要確認(rèn)真實(shí)世界的個(gè)體以及各個(gè)個(gè)體之間的聯(lián)系。 第一種關(guān)系是 ISA 關(guān)系。 在 C++中使用 HASA 關(guān)系可以使用成員或是私有繼承。當(dāng)這樣使用基類中的成員時(shí),用戶不可以只依靠繼承來編寫代碼。這時(shí)當(dāng)你必須重復(fù)使用基類中的函數(shù)時(shí),可以使用其繼承。 C++多態(tài):多態(tài)是面向?qū)ο蟪绦虻娜齻€(gè)特性中最重要的部分。 當(dāng)定義一個(gè)成員函數(shù)時(shí),在它前面加上關(guān)鍵字 virtual,實(shí)際過程的調(diào)用依賴于對(duì)象實(shí)例的類型,通過這樣來判斷如何調(diào)用。這些函數(shù)在類定義時(shí)被賦予 0 值。我們可以使用運(yùn)算符重載加減操作就像整形和浮點(diǎn)型數(shù)據(jù)。),類運(yùn)算 (`.39。s look at several different class relationships. The first relationship is the ISA relationship. This type of relationship represents a specialization between types or classes. ISA inheritance holds for two classes if the objects described by one class belongs to the set of objects described by the other more general class. The ISA relationship is the traditional form of inheritance called subtyping. The subtype is a specialization of some more general type known as the supertype. In C++, the supertype is called the base class and the subtype the derived class. To implement the ISA relationship in C++ we use public inheritance. When public inheritance is used the public parts of the base class bee public in the derived class and the protected parts of the base class bee protected in the derived class. To implement the HASA relationship in C++ we use either position or private inheritance. For example, a stack can be implemented using an array. We can either use the stack as a data member (position) or derive the stack class from the array class using private inheritance. It is also possible to use inheritance to achieve a containership relationship between two classes. Private inheritance is used when the inheritance is not part of the inter