"Primer C ++ 3RD" reading note 2

xiaoxiao2021-03-06  70

Chapter two

This chapter uses an array to introduce standard C .

Built-in Data Type Built-in Data Types -> Compliant Type Compound Types -> Standard Library Type Standard Library Class Types -> Quasi-Standard Library / Third Spending Type -> Customized Type Array Index, 0 to represent An index of an element. Be careful not to have an offset error OFF-BY-one.

The array is the inheritance of C to C, which reflects the characteristics of the algorithm of C and data separation. It is not a first-class data type.

This is lost in this: does not support the negative value between the array, cannot be compared, and the array cannot be compared. It is easy to index over by cyclic initialization.

Dynamic and static memory allocation: The Two Primary Differences Between Static and Dynamic Memory Allocation Area Follows.

Static Objects Are Named Variables That We Manipulate Directly, WHEREAS DYNAMIC Objects Are Unnamed Variables We Manipulate Indirectly Through Pointers. We'll See An Example of this in A Moment.

The allocation and deallocation of static objects is handled automatically by the compiler; the programmer needs to understand it but need not do anything about it The allocation and deallocation of dynamic objects, in contrast, must be managed explicitly by the programmer and, in practice. IS considerably more error-prane. it is accountplashed through the use of the new and delete expression.

Dynamic allocation of memory, New only provides a pointer to allocate an array, but cannot be negative when allocating an array. You must use Delete / Delete [] in time to release memory.

(c) int * pi2 = new int (1024); (d) int * pi3 = new int [1024]; See clearly, these two can be done, be careful when playing code.

The new class represents a new data type, so that it is very convenient, so it can define the object of the class and the pointer to the object to this class. A class name represents a new data type. We can use it to define Objects of our class type in The Same Way We Use the Built-in Types To Define Those Objects. For example:

// a Single IntArray Class ObjectIntArray MyArray;

// a Pointer to a single Intarray Class ObjectIntarray * Parray = New INTARRAY

Class definitions To include Class keywords, class names, and categories, the former two are used to declare classes.

Use advantage of information hiding: This division between the public interface and private implementation of a class is referred to as information hiding Information hiding is an important software engineering concept that we look at in more detail in the later chapters of this text Briefly,.. it provides two primary benefits for our programs.If the private implementation of the class needs to be modified or extended, only the relatively small number of member functions requiring access to that implementation needs to be modified; the many user programs making use of our class Generally Do Not NEED To Be Modified, Althought (We Illustrate this in section 6.18).

If there is an error in the state of the private class implementation, the amount of program code we need to examine in general is localized to the relatively small number of member functions requiring access to that implementation; the entire program need not be examined.

Since C does not allow member functions and data members to picked up, when you need to use a common member function to package private data members, it is usually used to distinguish before the data member is used.

The method of using the Get / set package private data for C object-oriented programming is directly used, which will not cause performance to decrease, that is, doing this face when declaring.

initialize array_size to the dimension of the array. The first instance, however, appears to require a function call, whereas the second involves only a direct memory access. Generally, a function call is significantly more expensive than a direct memory access. So does information Hiding Impose A Significant Expense 梡 Erhaps a Prohibitive Expense 梠 n The Run-Time Efficiency of a program? Happily, In General, The Answer is no.

The solution provided by the language is the inline function mechanism. An inline function is expanded in place at its point of call. In general, that is, an inline function does not involve any function call at all. [1] For example, the call of size () within the condition clause of the for loopfor (int index = 0; index

// general inline expansion of array.size () for (int index = 0; index

The constructor is not named with the class name and does not return the value and the return type declaration.

The C compiler is uniquely distinguished by the original shape, that is, the function name, the number of returns, the number of parameters, and the parameter type.

The data is shared between all objects of the class via Static.

Class IntaRray {public: // ... private: void init (int SZ, int * array); // ...};

VoidintArray :: INIT (INT SZ, INT * Array) {_size = SZ; IA = new int = 0. Else IA [ix] = array [ix];

Our Three Rewritten Constructors Are As Follows:

INTARRAY :: INTARRAY (INT SZ) {INIT (SZ, 0);} INTARRAY :: INTARRAY (INT * Array, INT SZ) {INIT (SZ, Array);} INTARRAY :: INTARRAY (Const IntArray & rhs) {init RHS.SIZE, RHS.IA);} Note this paragray :: INTARRAY (INT SZ) {INIT (SZ, 0);} Use 0 to pay the value to the pointer Array to complete the conditional judgment. The pointer can pay 0 values ​​to make special conditions.

B inherit from A, then b makes special A. So the following code can be. #include Void Swap (INTARRAY & IA, INT I, INT J) {INT TMP = IA [I]; IA [i] = IA [J]; IA [J] = TMP;} here is through Invocations of swap ():

INTARRAY IA; INTARRAYRC IARC; INTSORTEDARRAY IAS;

// OK: IA is an Intarayswap (IA, 0, 10);

// OK: IARC IS A Subtype of IntarraySwap (IARC, 0, 10);

// OK: IAS IS Also a Subtype of IntarraySwap (IAS, 0, 10);

// error: String is not a subtype ... String Str ("Not An IntArray!"); SWAP (STR, 0, 10);

Note:::: A :: b, a is class, b is a function, the front includes the back; the latter is used when defining inheritance and initialization table, A: B is B is the root of A, the latter contains the former. . . This old book is unclear. . . Class Intarrayrc: Public INTARRAYINLINE INTARRAYRC :: INTARRAYRC (Const INT * IAR, INT SZ): INTARRAY (IAR, SZ) {}

When defining an inherited object, the memory of the base class will be allocated, and then the accumulated constructor is initialized, and then assigns inherited memory memory, call the inheritance class initialization table. Subclasses do not inherit the constructor of the parent class, so even if the subclass is not added to the data object more than the parent class, it is also necessary to pass the data initial value of the object to the parent class by the initialization list. inline IntArrayRC :: IntArrayRC (const int * iar, int sz): IntArray (iar, sz) {} The portion of the constructor marked off by the colon is referred to as a member initialization list It provides the mechanism by which the IntArray. constructor is passed its argument. The bodies of both IntArrayRC constructors are null, because the job of the constructors is only to pass their arguments to the associated IntArray constructor.

If a Class requires a Copy Constructor, or a Copy Assignment Operator, or a Destructor, then it may learn the other three of them.

EXERCISE 2.13GIVEN The FOLLOWING TYPE DECLARATIONS

Template class array; enum status {...}; typedef string * pstring;

Which, if any, of the following object definitions are in error?

(a) Array Pri (1024); (b) Array > AAI (1024); (c) Array > ACD (1024); (d) Array AS (1024); (e) Array APS (1024); This question is to look carefully after a few questions behind this chapter. Really, how to make this kind of mention in the second chapter, the head is big, staying in the future. Exercise 2.15given the following class template

template class Example2 {public: explicit Example2 (elemType val = 0): _val (val) {} bool min (elemType value) {return _val

Template Ostream & Operator <(Ostream & OS, Const Example2 & EX) {EX.PRINT (OS); Return OS;

What happens when we write the folowing?

(a) Example2 *> EX1; (B) EX1.MIN (& EX1); (C) Example2 SA (1024), Sb; (d) Sa = Sb; (e) Example2 EXS ("Walden"); (f) cout <"EXS:"

Abnormal handling mechanism: 1. An abnormal place in the program. Throw out, wait for processing. The program is suspended. 2, the abnormality in the program is processed. Implement with the try ... catch group and statement. By jumping from the Program Callback Stack until you find the corresponding catch position, the function will always hang to main (), and then call Terminate () in chapter standard.

Namespace is used to prevent contamination of the identifier. Use an alias to simplify longer Namespace and replace it.

depressed! It is easy to manually play the official answer of the last question in VS6SP6 and CBX1, so that I didn't think it was Terminate after compiling. . . I don't know where there is a problem. . . After all, it is still a newbie. . . However, I have been right to use my eyes to see different. The so-called eyes is low, I have found a lot of low-level errors. . . This program is applied here, used as later studies. Exercise 2.23 # include #include Using Namespace Std;

Template ELMTYPE min1 (const vector & vec) {ELEMTYPE minimum; if (vec.size ()> - 1) minimum = vec [0]; else throw "empty vector -index."; for (int i = 1; i

Template ELMTYPE min2 (constor :: const_iterator it = vec.begin (); elemType minimum; if (iter

转载请注明原文地址:https://www.9cbs.com/read-110488.html

New Post(0)