【正文】
d 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 anize 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 class relationships dictate that inheritance be used. C++ provides 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, let39。),成員運(yùn)算(`.*39。一個(gè)復(fù)數(shù)的類(lèi)型要有加減的運(yùn)算。一個(gè)類(lèi)當(dāng)它只有虛函數(shù)的定義而沒(méi)有實(shí)現(xiàn)時(shí)是抽象類(lèi)。它只有一個(gè)對(duì)成員函數(shù)的 說(shuō)明使用于類(lèi)中所有的例子。最后,當(dāng)在基類(lèi)中的公共部分和保護(hù) 部分使用私有繼承,基類(lèi)可以調(diào)用,但用戶(hù)和派生類(lèi)不可以使用。 如果可以的話,最好使用數(shù)據(jù) 成員。但是派生類(lèi) 中的成員函數(shù)可以自由的訪問(wèn)或調(diào)用基類(lèi)中的公共部分和私有部分。不是所有對(duì)象它都是其他對(duì)象的一個(gè)專(zhuān)門(mén)說(shuō)明和繼承,可能這些對(duì)象有些是其他對(duì)象的一部分或是包含在其他的對(duì)象中。這些不同的方式應(yīng)用在不同的對(duì)象間關(guān)系。繼承可以從與原有的類(lèi)型擴(kuò)展到派生類(lèi)型。 C++通過(guò)調(diào)用函數(shù)模擬信息的傳遞 。 C++無(wú)法提供一個(gè)使所有細(xì)節(jié)完全排除在基本操作之外的方法,因此一個(gè)類(lèi)的私 有部分必須這個(gè)類(lèi)的定義,從而有效的訪問(wèn)這個(gè)類(lèi)的變量,并可以繼承。公共的接口部分標(biāo)有關(guān)鍵字public。一個(gè)類(lèi)就像 C 的結(jié)構(gòu),但不同的是同時(shí)包括了數(shù)據(jù)和方法。一個(gè)對(duì)象的執(zhí)行是一個(gè)循環(huán)遞歸的過(guò)程,當(dāng)定義這個(gè)對(duì)象和 方法的初始值是,可以跳出這個(gè)循環(huán)遞歸的過(guò)程。Smalltalk, C++, ObjectiveC, 和 Lisp with CLOS (the Common Lisp Object System)這些程序語(yǔ)言都是面向?qū)ο笳Z(yǔ)言的例子,它們都可以提供對(duì)壓縮,繼承和多態(tài)的支持。新對(duì)象的不同在于只需要提供方法或數(shù)據(jù)。近來(lái),程序語(yǔ)言擴(kuò)展到支持新的數(shù)據(jù)類(lèi)型的定義和提供便利給數(shù)據(jù)抽象。其他部分調(diào)用時(shí),只是正確有效的執(zhí)行 ,但不清楚過(guò)程的執(zhí)行。命令可以使機(jī)器找到解決方法,使用指定命令改變存儲(chǔ),變量讀取,算術(shù)和邏輯表達(dá)式,條件分枝控制執(zhí)行流。一般來(lái)說(shuō),幾種語(yǔ)言可能屬于同一種范例。使用多種多樣的語(yǔ)言建立不同的設(shè)計(jì)方案。只有硬件設(shè)計(jì)師了解如何在其他成分之外建立計(jì)算機(jī)體系,語(yǔ)言設(shè)計(jì)者同樣認(rèn)識(shí)到程序可以在一個(gè)高水平的環(huán)境編寫(xiě),因而是程序員可以不必了解機(jī)器的細(xì)節(jié)。這時(shí)程序的細(xì)節(jié)很笨重。每個(gè)程序語(yǔ)言固定一個(gè)特定的程序體系或設(shè)計(jì)程序原理。當(dāng)所選擇的語(yǔ)言提供的結(jié)構(gòu)和機(jī)制符合范例時(shí),就很容易完成。計(jì)算命令,由一系列的命令組成,以代碼形式存儲(chǔ)。過(guò)程是抽象的表格,完成一些任務(wù)或功能。當(dāng)算法設(shè)計(jì)完成 時(shí),實(shí)際的數(shù)據(jù)類(lèi)型被執(zhí)行。這個(gè)新創(chuàng)建的對(duì)象是原對(duì)象的具體說(shuō)明。一般來(lái)說(shuō),語(yǔ)言通過(guò)不同的途徑支持這些特征的實(shí)現(xiàn)。對(duì)象本身可以完全的調(diào)用它的子對(duì)象。 C++面向?qū)ο蟮奶匦? 壓縮: C++繼承了 C 的發(fā)展,并且定義的新的方便的數(shù)據(jù)類(lèi)型。不可見(jiàn)的成員在其定義的部分標(biāo)明了關(guān)鍵字 private。 我們不可能對(duì)用戶(hù)完全的隱藏操作的細(xì)節(jié)。實(shí)際上,這里并沒(méi)有真的傳遞信息。 4 C++面向?qū)ο筇卣? C++繼承:任何面向?qū)ο蟪绦蛘Z(yǔ)言其最主要的作用就是可以從原有的類(lèi)中 創(chuàng)建新的類(lèi),從而可以再次使用代碼。 C++提供了三種方式的繼承:公有繼承,私有繼承,保護(hù)繼承。在基類(lèi)中的公共部分使用公有繼承,在派生類(lèi)中也是公有的部分,在基類(lèi)中的保護(hù)部分使用公有繼承,在派生類(lèi)中也變成保護(hù)的部分 有時(shí)在兩個(gè)對(duì)象之間的關(guān)系也許是包含關(guān)系,也可以是部分整體之間的關(guān)系。派生類(lèi)的使用者不可以訪問(wèn)基類(lèi)中的接口。一個(gè)保護(hù)的基類(lèi)類(lèi)似于一個(gè)私有的基類(lèi),不同在于繼承的派生類(lèi)可以知道接口部分。在基類(lèi)中的公共部分和保護(hù)部分使用保護(hù)繼承,在派生類(lèi)中可以使用成員函數(shù),但并不對(duì)派生類(lèi)的所有用戶(hù)。一個(gè)類(lèi)的普通的成員函數(shù)是靜態(tài)的單一的過(guò)程,在編譯時(shí)調(diào)用。一個(gè)類(lèi)是抽象的類(lèi)當(dāng)這個(gè)類(lèi)是繼承的根節(jié)點(diǎn)時(shí)。 當(dāng)我們定義新的數(shù)據(jù)類(lèi)型時(shí),需要定義相似類(lèi)型的標(biāo)準(zhǔn)的操作。同時(shí),范圍運(yùn)算 (`::39。s operations. The 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 8 implemented. We describe the data type by its properties and operations and assume that whatever implementation is chosen, the operations will work correctly and efficiently. Types defined in this way are called ABSTRACT DATA TYPES (ADTs). The use of abstract data types makes the 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 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. THE OBJECTORIENTED PARADIGM. The object oriented paradigm retains much of the characteristics of the procedural paradigm, since procedures are still the primary form for posing putations. However, rather than operate on abstract values, programs in the objectoriented paradigm operate on objects. An object is very similar to an abstract data type and contains data as well as procedures. There are three primary characteristics of the objectoriented 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 created from existing, more general ones. The new object bees 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 created (or derived) from another object, it is said to inherit the methods and data of the parent object, and includes any new representations and new or revised methods added to it. The third and final characteristic of objectoriented programming is POLYMORPHISM. Polymorphism allows many different types of objects to perform the same operation by 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 runtime. Objectoriented languages contain mechanisms for ensuring that each sort message is sent to the right object. Encapsulation, inheritance, and polymorphism are considered the fundamental characteristics of objectoriented programming and all objectoriented languages must provide these characteristics in some way. Not surprisingly, languages support these characteristics in very different ways. Smalltalk, C++, ObjectiveC, and Lisp with CLOS (the Common Lisp Object System) are all examples of objectoriented languages, and each provides support for encapsulation, inheritance, and polymorphism. Constructing an objectoriented program involves determining the objects that are needed to solve the problem. The objects are then used to construct putations that define the behavior of the software system. Message passing is the fundamental interaction 9 mechanism among objects. Messages (from other objects or programs) are sent to objects to inform t