C++ Chapter2_A_Tour_of_C++_第1頁(yè)
C++ Chapter2_A_Tour_of_C++_第2頁(yè)
C++ Chapter2_A_Tour_of_C++_第3頁(yè)
C++ Chapter2_A_Tour_of_C++_第4頁(yè)
C++ Chapter2_A_Tour_of_C++_第5頁(yè)
已閱讀5頁(yè),還剩20頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、lThe Built-In Array Data TypelDynamic Memory Allocation and PointerslAn Object-Based DesignlAn Object-Oriented DesignlA Generic DesignlAn Exception-Based DesignlAn Array by Any Other NamelThe Standard Array Is a Vector An array is an ordered container of elements of a single type. One unintuitive as

2、pect of array subscripting under C+ is that the element positions begin at 0 and not 1. Typically, we step through the elements of an array using a for loop. Ex1 C+ does not support the abstraction of an array; there is no support for the operations one might wish to perform on an array, such as the

3、 assignment of one array to another, the comparison of two arrays for equality, or asking an array its size. If we wish to assign one array to another, we must program that ourselves, copying each element in turn: for ( int index = 0; index 10; +index ) array0 index = array1 index ; Under C+, object

4、s can be allocated either statically-that is, by the compiler as it processes our program source code-or dynamically-that is, by a run-time library invoked during program execution. static memory allocation is considerably more efficient to allocate because it is done prior to program start-up. It i

5、s less flexible, however, because it requires that we know prior to the execution of the program the amount and type of memory that we need. C+ supports a pointer type to hold the memory address values of objects. C+ predefines a special address-of operator (&) that, when applied to an object, r

6、eturns that objects address value. To access the actual object pint addresses, we must first dereference pint using the dereference operator (*). lObjects are allocated dynamically through one of two versions of the new expression. The first instance allocates a single object of a specific type. For

7、 example, int *pint = new int( 1024 ); lA second version of the new expression allocates an array of elements of a specified type and dimension. For example, int *pia = new int 4 ; lThe general form of a class supporting an object-based design looks like this: class classname public: / the public se

8、t of operations private: / the private implementation ; A class definition consists of two parts: the class head, composed of the keyword class and an associated class name, and the class body, enclosed by braces and terminated with a semicolon. The class head, by itself, serves as a declaration of

9、the class. Members that occur in a public section of the class body can be accessed from anywhere within the general program. Members that occur in the private section can be accessed only by the member functions (and friends) of the class. This division between the public interface and private impl

10、ementation of a class is referred to as information hiding. An inline function is expanded in place at its point of call. A member function defined within the class definition, such as size(), is automatically treated as an inline function. Alternatively, the inline keyword can be used to request th

11、at a function be treated as inline. A constructor is a special class member function used for initialization. A constructor is defined by giving it the same name as the class. In addition, no return type can be specified for a constructor. Function overloading allows the same name to be used for two

12、 or more functions-with the constraint that each must have a unique parameter list, either by the number or the types of the parameters. The double colon (:) operator is referred to as the scope operator. A destructor is automatically invoked on each class object after the final use of the object in

13、 our program. We identify a destructor by giving it the name of the class preceded by a tilde (). The destructor in general frees resources acquired during the construction and use of the class object. In C+, the class being inherited from, is spoken of as a base class. The new class is said to be d

14、erived from the base class. We call it the derived class, or the subtype, of the base class. An additional access level, protected, is provided. The data members and member functions of a protected section of a class, while still unavailable to the general program, are available to the derived class

15、. C+ supports two additional forms of inheritance: multiple inheritance, in which a class is derived from two or more base classes, and virtual inheritance, in which a single instance of a base class is shared among multiple derived classes. Exceptions are run-time program anomalies such as an out-o

16、f-bounds array index, the inability to open a specified file, the exhaustion of available program dynamic memory, and so on. Exception handling provides a standard language-level facility for responding to run-time program anomalies. It supports a uniform syntax and style that supports fine-tuning b

17、y individual programmers. The primary components of the exception handling facility are the following:l The point within the program where the exception occurs. In C+, the raising of an exception is carried out by a throw expression. lThe point within the program where the exception is handled. In C

18、+, the handling of an exception is carried out by a catch clause. Catch clauses are associated with try blocks. A try block groups one or more program statements with one or more catch clauses. A catch clause is selected based on a matching of the type of the exception with the type associated with

19、the catch clause. If the exception mechanism rolls the sequence of function invocations all the way back to main() and no handler is found, the standard library terminate() function is invoked. By default, terminate() ends the program. A special catch clause capable of handling raised exceptions of

20、all types is the following: catch( . ) / handles all exceptions, although it cannot / directly access the exception object The namespace mechanism allows us to encapsulate names that otherwise pollute the global namespace. For example, here is how we might encapsulate our Array class: namespace Cplu

21、splus_Primer_3E class Array . ; / . The name following the namespace keyword identifies a namespace separate from the global namespace within which we can place entities we wish to declare outside a function or class. The namespace does not change the meaning of the declarations within it; it changes only their visibility. We use a qualified name notation of the form namespace_identifier:entity

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論