Child object
When a member of a class is an object of a certain class, the object is a child object. The child object is actually a member of the object. Such as:
Class A
{
PUBLIC:
...
Private:
...
}
Class B
{
PUBLIC:
...
Private:
A a a;
...
}
Among them, member A in the B class is a child object, which is an object of Class A as a member of Class B.
When there is a child object or a member member member, the constructor of this class is to include the initialization of the sub-object, usually using the method of the member initialization table to initialize the child object. In the member initialization table contains the initialization of the child object and initialization of other members in the class. An example of an example will be described below to explain the construct of member initialization.
#include
Class A
{
PUBLIC:
A (INT I, INT J) {A1 = I; A2 = J;}
Void Print () {cout INT A1, A2; } Class B { PUBLIC: B (INT I, INT J, INT K): A (I, J), B (K) { } Void print (); Private: A a a; file: // Subject INT B; } Void B :: Print () { a.print (); Cout <} void main () { B B (6, 7, 8); B.Print (); } The output result of the program is: 6,7 8 Among them, A (i, j), b (k) is a member initialization table, which has two, the previous one is initialized to the child object A, which is equipped with the following: The latter one is initialized to the data member B of class B. This can also be written in a function of constructor, using assignment expressions B = K; Initialize the data member of class B. Heap object The so-called heap object refers to an object that can be established or deleted at any time during the program operation. Such a heap object is created in a memory cell that is idle, and these storage units are referred to as a heap. They can be created, and they can also be released by deleting a reactor object. When you create or delete a stack object, you need two operators as follows: New Delete These two operators are also known as dynamic allocation memory space operators. The New is equivalent to the malloc () function in the C language, and the Delete is equivalent to the free () function in the C language. 1. Usage of the operator NEW The function of the operator is used to create a stack object, or that it is used to create an object. The format of the New operator is as follows: NEW It shows an object that is created in a heap, and the initial value of the object being created is given by When you create an object using the New operator, it can select the appropriate constructor based on its parameters, which does not use SizeOf to calculate the number of bytes of the object, and can calculate its size. The New operator returns a pointer, the pointer type matches the allocated object assigned to the New, and if you do not match the method of mating the type, you will appear. If the New operator cannot be assigned to the required memory, it will return 0, and the pointer is an empty pointer. Operators New can also be used to create an array type object, ie an array of objects. The format is as follows: NEW Among them, the value of A * PTR; PTR = New a [5]; NEW can also be used to create an array of general types. Such as: INT * P; P = new int [10]; When using the object array or a general array of new [], the initial value cannot be specified for the array, whose initial value is the default. 2. Usage of operator Delete The function of the operator is to delete objects or general type of pointer created using NEW. The format is as follows: Delete E.g: A * PTR; PTR = New A (5, 6); Delete PTR; The operator DELETE can also be used to delete an array of objects to create an object using NEW, and its usage format is as follows: DELETE [] Similarly, DELETE can also delete an array of general types created by NEW. Such as: INT * P; P = new int [10]; Delete [] P; When using the operator DELETE, pay attention to the following: (1) It must be used by the pointer returned by the operator NEW; (2) The operator also applies to the empty pointer (ie, a pointer to which is 0); (3) The pointer is only used in front of the brackets and ignores any numbers in square brackets regardless of the dimension of the array. The following example will illustrate the use of the New operator and the Delete operator. #include Class aa { PUBLIC: AA (Int I, Int J) { A = i; b = j; Cout << "Constructor ./n"; } ~ Aa () {cout << "Destructor ./n"; Void print (); Private: INT A, B; } Void aa :: print () { Cout <} void main () { AA * A1, * A2; A1 = New AA (1, 2); A2 = New AA (5, 6); A1-> Print (); A2-> Print (); Delete A1; Delete A2; } The output result of the program is: Constructor. Constructor. 1, 2 5, 6 Constructor. Constructor. As you can see from the program: When you create an object with New, you want to call the constructor. When you use the Delete to delete an object, you want to call the destructor. If the target array is created or deleted, the object array has a number of constructors or constructors. In practical applications, the pointers returned to the New operator are often verified to see if effective memory space is assigned. In combination with this example, the test method is given as follows: IF (! A1) { COUT << "Heap erroe! / n"; Exit (1); } Let's take an example using the New and Delete operators on the general pointers and arrays. #include #include Void fun () { INT * P; IF (p = new int) { * p = 5; COUT << * P } Else Cout << "Heap Error! / N"; } void main () { Fun (); INT * PA; PA = new int [5]; IF (! pa) { Cout << "Heap Error! / N"; Exit (1); } For (int i = 0; i <5; i ) PA [i] = i 1; For (i = 0; i <5; i ) COUT << PA [i] << " Cout })