【正文】
hem to perform one of their operations. Objects are responsible for maintaining the state of their data. Only the object may modify its internal data. Objects may themselves be implemented via other subobjects. Implementing an object involves a recursive process of breaking it into subobjects until at some level 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. One of the most important aspects of the objectoriented 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 carrying out their own operations. Each object is conceived and implemented as selfcontained. Such a model facilitates software design (and later implementation) because objects often model conceptual realworld entities. Designing systems using the objectoriented paradigm results in software systems that behave and appear more like their reallife counterparts. The ObjectOriented Characteristics of C++ ENCAPSULATION in C++. C++ extends C with a facility for defining new 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 manipulated from outside the class. 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 PUBLIC 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. 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 private. 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. 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 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 cleanup for the object when the object goes out of scope and is destroyed. 10 Note that we didn39。), and the arithmetic if operator (`?:39。) and subtraction (`39。)不能重載。運算符有一個或兩個參數分別叫做單目或雙目運算符。 另一個多態(tài)的形式是函數 重載。靜態(tài)方式的束定也叫做早期束定。多態(tài),就是說有多個形式,提供一個基本的軟件接口以建立不同類型的對象操作的統(tǒng)一的方式。當在不必要時使用繼承,會使系統(tǒng)更加難以理解。繼承的另一種方式,也是很少使用的一種方式就是保護繼承。我們可以使用堆棧作為一個數據成員 ,或者使用私有繼承,從數組類中繼承堆棧類。 ISA繼承指兩個類,其中一個類描述了對對象的說明,對對象的設置說明由另一個類來說明。有些類之間可能不是完全的相關??梢栽谠O計的過程中找到對象間的繼承關系,甚至可以使用傳統(tǒng)的,非面向對象的語言設計系統(tǒng)。保護成員既可以被類的成員函數訪問,也可以被派生類的成員函數訪問。當一個數據對象被創(chuàng)建并且具有變量聲明時才分配內存。用戶只可以通過一個公共的接口來訪問這個對象。系統(tǒng)被認為是由多個單一獨立的個體組成,其中每個個體只負責對其自身的操作的運行。信息的傳遞是對象間最基本的相互作用機制。多態(tài)可以使不同類型的的對象對相同的信息執(zhí)行相同的操作。對象同抽象數據類型很相似,聯(lián)系著數據和運算。 當我們設計一個算法時,需要一個特定的數據類型執(zhí)行算法的操作。還包括重復,選擇等特征?;旌险Z言,如 C++,綜合了兩到三種范例。怎樣去設計一個模式?程序范例提供 了許多不同的方法去設計和思考軟件系統(tǒng)。程序語言作為一種工具幫助我們解決這些問題。 1 面向對象和 C++ C++是目前所使用的眾多編程語言中的一種。然而,并不是一種程序語言可以最好的解決所有問題。一個范例可以被認為是一個模式或者一個框架來設計和描述軟件結構。 C++包括了命令和程序范例的特性,例如,其前身 — C,和面向對象范例。許多主流程序還是這種語言。如果可以定義變量的數據類型,而不影響到實際數據類型的運行,就可以很容易的制訂出算法。 面向對象范例具有三種主要特性,第一種,壓縮,其機制是為了實施數據抽象。例如,我們有一部分對象它們可以執(zhí)行一類操作,但是只有在運行時我們才知道對象的類型。信息(從其他的對象或程序)傳遞給對象,以便通知對象運行下一個操作。每一個對象的設置和運行都是自身所包含的。一個 C++的類既包括共有的部分,又包括私有的部分。構造函數和析構函數提供了對象的初始化和釋放功能。和私有成員相同的一點,保護成員不可以被用戶程序訪問。但是,使用面向對象語言,如 C++,由于語言支持對象間的繼承關系,從而可以更方便的了解設計執(zhí)行。大部分的類之間可以歸納為繼承的關系。 ISA 的關系是傳統(tǒng)的繼承關系,也就是派生類型。 可以在兩個類之間使用繼承創(chuàng)建一個互相包含的關系。保護繼承也是一種可以使用 HASA 關系的繼承。 5 總之,一個類有兩個主要的接口:一個是給用戶所使用的(公共接口),另一個派生類所使用(公有部分和保護部分的聯(lián)合)。 C++提供了三種類型的多態(tài):虛函數,函數重載和運算符重載。 虛函數機制可以通過一個基類的參數或指針調用。當一個函數被使用在多個程序時就是重載。在 C++中,一個運算符函數的定義就像一個成員函數的定義,在其名字的前面加上關鍵字 operator。 在這后兩點的面向對象分析中,我們可以看到 C++很好的支持了面向對象的范例。) operators work for plex objects just as they do for ints and floats. Operators are defined in much the same was as normal C++ functions and can be members or nonmembers of a class. Operators take one or two arguments and are called unary and binary operators accordingly. In C++, a member operator function is defined like an ordinary member function, but the name is prefixed with the keyword operator. C++ places a number of restrictions on operator overloading. Only the predefined set of C++ operators may be overloaded. It is illegal to define a new operator and then overload it. You cannot turn a unary operator into a binary operator or vice versa. Also, the following operators cannot be overloaded: scope operator (`::39。), class object selector operator (`.39。s structure. The model helps us think about and formulate solutions. We can use the mental model of a paradigm independently from the programming language chosen for implementation. However, when the chosen language provides constructs and mechanisms that are similar to those that are found in the paradigm, the 7 implementation will be more straightforward. Usually, there are several languages that belong to a paradigm. For this reason, a programming paradigm is als