【正文】
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 interface。我們可以使用運(yùn)算符重載加減操作就像整形和浮點(diǎn)型數(shù)據(jù)。 當(dāng)定義一個(gè)成員函數(shù)時(shí),在它前面加上關(guān)鍵字 virtual,實(shí)際過(guò)程的調(diào)用依賴于對(duì)象實(shí)例的類型,通過(guò)這樣來(lái)判斷如何調(diào)用。這時(shí)當(dāng)你必須重復(fù)使用基類中的函數(shù)時(shí),可以使用其繼承。 在 C++中使用 HASA 關(guān)系可以使用成員或是私有繼承。 面向?qū)ο笤O(shè)計(jì)的一個(gè)主要操作就是要確認(rèn)真實(shí)世界的個(gè)體以及各個(gè)個(gè)體之間的聯(lián)系。建造一個(gè)類可以使它方便的訪問(wèn)它父類的私有成員。除此之外, C++還提供了類中各個(gè)成員訪問(wèn)權(quán)限的不同,以此方便的控制即使是在不同的類,也可以訪問(wèn)類中的成員。 構(gòu)建一個(gè)面向?qū)ο蟮某绦蛐枰獩Q定解決問(wèn)題所需的對(duì)象。 面向?qū)ο蟮姆独核匀槐A袅嗽S多程序范例的特征,過(guò)程仍然是計(jì)算的主要形式。 程序范例:它包括了命令范例,還有對(duì)概念命令和表達(dá)的抽象機(jī)制。這些方案被叫做程序范例,幫助我們思考問(wèn)題,規(guī)范解決。第一個(gè)編程語(yǔ)言對(duì)基本的機(jī)器結(jié)構(gòu)有很大的依賴性。我們可以選擇一種語(yǔ)言,使用范例獨(dú)立的完成。抽象可以把動(dòng)作和結(jié)果隔離。繼承允許從已存在的對(duì)象中創(chuàng)建新的對(duì)象。只有對(duì)象本身才可以改變它內(nèi)部的數(shù)據(jù)值。 C++提供了關(guān)鍵字來(lái)說(shuō)明類中哪些 成員是不可見(jiàn)的,哪 些部分是其公共接口。例如一個(gè)語(yǔ)句 i (),我們可以有效的把信息傳給 a2 的對(duì)象,以確定組的大小并返回其值。但不是所有的類之間都可以建立繼承的關(guān)系。在私有繼承的情況下,原來(lái)基類中的公有部分和保護(hù)部分,在派生類中都變?yōu)樗接胁糠?。在基類中的公共接口使用公有繼承,在派生類中隊(duì)用戶也是公有接口。虛函數(shù)也可以用在邏輯的運(yùn)算上。你不能把單目運(yùn)算符返回到雙目運(yùn)算或多目運(yùn)算中。), class object selector operator (`.39。 在這后兩點(diǎn)的面向?qū)ο蠓治鲋校覀兛梢钥吹?C++很好的支持了面向?qū)ο蟮姆独?。?dāng)一個(gè)函數(shù)被使用在多個(gè)程序時(shí)就是重載。 C++提供了三種類型的多態(tài):虛函數(shù),函數(shù)重載和運(yùn)算符重載。保護(hù)繼承也是一種可以使用 HASA 關(guān)系的繼承。 ISA 的關(guān)系是傳統(tǒng)的繼承關(guān)系,也就是派生類型。但是,使用面向?qū)ο笳Z(yǔ)言,如 C++,由于語(yǔ)言支持對(duì)象間的繼承關(guān)系,從而可以更方便的了解設(shè)計(jì)執(zhí)行。構(gòu)造函數(shù)和析構(gòu)函數(shù)提供了對(duì)象的初始化和釋放功能。每一個(gè)對(duì)象的設(shè)置和運(yùn)行都是自身所包含的。例如,我們有一部分對(duì)象它們可以執(zhí)行一類操作,但是只有在運(yùn)行時(shí)我們才知道對(duì)象的類型。如果可以定義變量的數(shù)據(jù)類型,而不影響到實(shí)際數(shù)據(jù)類型的運(yùn)行,就可以很容易的制訂出算法。 C++包括了命令和程序范例的特性,例如,其前身 — C,和面向?qū)ο蠓独H欢?,并不是一種程序語(yǔ)言可以最好的解決所有問(wèn)題。程序語(yǔ)言作為一種工具幫助我們解決這些問(wèn)題?;旌险Z(yǔ)言,如 C++,綜合了兩到三種范例。 當(dāng)我們?cè)O(shè)計(jì)一個(gè)算法時(shí),需要一個(gè)特定的數(shù)據(jù)類型執(zhí)行算法的操作。多態(tài)可以使不同類型的的對(duì)象對(duì)相同的信息執(zhí)行相同的操作。系統(tǒng)被認(rèn)為是由多個(gè)單一獨(dú)立的個(gè)體組成,其中每個(gè)個(gè)體只負(fù)責(zé)對(duì)其自身的操作的運(yùn)行。當(dāng)一個(gè)數(shù)據(jù)對(duì)象被創(chuàng)建并且具有變量聲明時(shí)才分配內(nèi)存??梢栽谠O(shè)計(jì)的過(guò)程中找到對(duì)象間的繼承關(guān)系,甚至可以使用傳統(tǒng)的,非面向?qū)ο蟮恼Z(yǔ)言設(shè)計(jì)系統(tǒng)。 ISA繼承指兩個(gè)類,其中一個(gè)類描述了對(duì)對(duì)象的說(shuō)明,對(duì)對(duì)象的設(shè)置說(shuō)明由另一個(gè)類來(lái)說(shuō)明。繼承的另一種方式,也是很少使用的一種方式就是保護(hù)繼承。多態(tài),就是說(shuō)有多個(gè)形式,提供一個(gè)基本的軟件接口以建立不同類型的對(duì)象操作的統(tǒng)一的方式。 另一個(gè)多態(tài)的形式是函數(shù) 重載。)不能重載。), and the arithmetic if operator (`?:39。同時(shí),范圍運(yùn)算 (`::39。一個(gè)類是抽象的類當(dāng)這個(gè)類是繼承的根節(jié)點(diǎn)時(shí)。在基類中的公共部分和保護(hù)部分使用保護(hù)繼承,在派生類中可以使用成員函數(shù),但并不對(duì)派生類的所有用戶。派生類的使用者不可以訪問(wèn)基類中的接口。 C++提供了三種方式的繼承:公有繼承,私有繼承,保護(hù)繼承。實(shí)際上,這里并沒(méi)有真的傳遞信息。不可見(jiàn)的成員在其定義的部分標(biāo)明了關(guān)鍵字 private。對(duì)象本身可以完全的調(diào)用它的子對(duì)象。這個(gè)新創(chuàng)建的對(duì)象是原對(duì)象的具體說(shuō)明。過(guò)程是抽象的表格,完成一些任務(wù)或功能。當(dāng)所選擇的語(yǔ)言提供的結(jié)構(gòu)和機(jī)制符合范例時(shí),就很容易完成。這時(shí)程序的細(xì)節(jié)很笨重。使用多種多樣的語(yǔ)言建立不同的設(shè)計(jì)方案。命令可以使機(jī)器找到解決方法,使用指定命令改變存儲(chǔ),變量讀取,算術(shù)和邏輯表達(dá)式,條件分枝控制執(zhí)行流。近來(lái),程序語(yǔ)言擴(kuò)展到支持新的數(shù)據(jù)類型的定義和提供便利給數(shù)據(jù)抽象。Smalltalk, C++, ObjectiveC, 和 Lisp with CLOS (the Common Lisp Object System)這些程序語(yǔ)言都是面向?qū)ο笳Z(yǔ)言的例子,它們都可以提供對(duì)壓縮,繼承和多態(tài)的支持。一個(gè)類就像 C 的結(jié)構(gòu),但不同的是同時(shí)包括了數(shù)據(jù)和方法。 C++無(wú)法提供一個(gè)使所有細(xì)節(jié)完全排除在基本操作之外的方法,因此一個(gè)類的私 有部分必須這個(gè)類的定義,從而有效的訪問(wèn)這個(gè)類的變量,并可以繼承。繼承可以從與原有的類型擴(kuò)展到派生類型。不是所有對(duì)象它都是其他對(duì)象的一個(gè)專門說(shuō)明和繼承,可能這些對(duì)象有些是其他對(duì)象的一部分或是包含在其他的對(duì)象中。 如果可以的話,最好使用數(shù)據(jù) 成員。它只有一個(gè)對(duì)成員函數(shù)的 說(shuō)明使用于類中所有的例子。一個(gè)復(fù)數(shù)的類型要有加減的運(yùn)算。t really hide the implementation details from the user. C++ does not provide a way to pletely exclude all of the details of the underlying implementation, since the private 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 their parent classes. C++ defines the keyword protected for this purpose. Protected members can be accessed by the member functions of a class as well as by member functions of derived classes. However, like private members, protected members cannot be accessed by user programs. One final note about objects. Recall that message passing is the fundamental means for munication among objects. When we write i () 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 through the use of function calls. The piler ensures us that the correct function will be called for the desired object. So, in C++ you can think of message passing as function calls. Objectorientation has bee 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 objectoriented techniques to design software systems. But it is more than a design methodology, it is a whole new way of thinking about problems. Objectoriented design allows us to think about the actual realworld entities of the problem we are attempting to provide a solution for. Beginning the design with concepts from the real world problem domain allows the same concepts to be carried over to implementation, making the design and implementation cycle more seamless. O