C ++ sub-objects and heap objects

Source: Internet
Author: User

Sub-Object

When a class member is a class object, the object is a sub-object. The sub-object is actually an object member. For example:

Class
{
Public:
...
PRIVATE:
...
};
Class B
{
Public:
...
PRIVATE:
A;
...
};

In Class B, member A is the sub-object, and it is the object of Class A as a member of Class B.

When a sub-object or object member exists in a class, the constructor of this class must include the initialization of the sub-object. Generally, the sub-object is initialized using the member initialization table method. The member initialization table contains the initialization of the Child object and the initialization of other Members in the class. The following example describes the structure of member initialization.

# Include

Class
{
Public:
A (int I, Int J) {a1 = I; a2 = J ;}
Void print () {cout <PRIVATE:
Int a1, a2;
};

Class B
{
Public:
B (int I, Int J, int K): A (I, j), B (k)
{
}
Void print ();
PRIVATE:
A A; file: // sub-Object
Int B;
};

Void B: Print ()
{
A. Print ();
Cout <}

Void main ()
{
B (6, 7, 8 );
B. Print ();
}

The output result of this program is:

6, 7
8

Here, a (I, j) and B (k) are member initialization tables, which have two items. The first one is to initialize sub-object A. The format is as follows:

<Sub-Object Name> (<parameter table>)

The next item is to initialize data member B of Class B. This item can also be written in the constructor's function body, using the value assignment expression statement.

B = K;

Initialize the data member of Class B.

 

Heap object

A heap object is an object that can be created or deleted at any time as needed during the program running. These heap objects are created in some idle storage units in the memory, which are called heap. They can be occupied by created heap objects or released by deleting heap objects.

When creating or deleting a heap object, the following two operators are required:

New

Delete

These two operators are also called dynamic memory allocation operators. New is equivalent to the malloc () function in C language, while Delete is equivalent to the free () function in C language.

1. usage of the new operator

This operator is used to create heap objects, or dynamically create objects.

The format of the new operator is as follows:

New <type specifier> (<Initial Value List>)

It indicates that an object of the <type specifier> type is created in the heap, And the <Initial Value List> in the brackets gives the initial value of the object to be created. If the initial values in parentheses and parentheses are omitted, the default value is used for the created object.

When an object is created using the new operator, it can select an appropriate constructor based on its parameters. It does not need sizeof to calculate the number of bytes occupied by the object, but can calculate its size.

The new operator returns a pointer. the pointer type will match the object allocated by the new operator. If it does not match, you can use the forced type method. Otherwise, a compilation error occurs.

If the new operator cannot be allocated to the required memory, it returns 0, and the pointer is a null pointer.

The new operator can also be used to create an array object, that is, an object array. The format is as follows:

New <Class Name> [<arithmetic expression>]

The value of <arithmetic expression> is the size of the created object array. For example:

A * PTR;
PTR = new A [5];

New can also be used to create arrays of common types. For example:

Int * P;
P = new int [10];

When you create an object array or a general Array Using New [], you cannot specify an initial value for this array. Its initial value is the default value.

2. Use of the delete Operator

This operator is used to delete objects created using new or pointers of common types. The format is as follows:

Delete <pointer Name>

For example:

A * PTR;
PTR = new A (5, 6 );
Delete PTR;

The delete operator can also be used to delete the array of objects created using the new operator. The format is as follows:

Delete [] <pointer Name>

Similarly, delete can also delete general types of arrays created by new. For example:

Int * P;
P = new int [10];
Delete [] P;

Note the following when using the delete OPERATOR:

(1) it must use a pointer returned by the new operator;

(2) This operator is also applicable to null pointers (pointers whose values are 0 );

(3) only one pair of square brackets are used before the pointer name. No matter the dimension of the deleted array, any number in square brackets is ignored.

The following example describes how to use the new and delete operators.

# 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 this program is:

Constructor.
Constructor.
1, 2
5, 6
Constructor.
Constructor.

From the program, we can see that when creating an object with new, the constructor must be called. When deleting an object with Delete, The Destructor must be called. If the object array is created or deleted, the number of constructor or constructor is called.

In practical applications, the pointer returned by the new operator is often checked to check whether a valid memory space is allocated. In combination with this example, the test method is as follows:

If (! A1)
{
Cout <"heap erroe! /N ";
Exit (1 );
}

The following is an example of using the new and delete operators for General pointers and arrays.

# Include
# Include

Void fun ()
{
Int * P;
If (P = new INT)
{
* P = 5;
Cout <* P <Delete 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 <Delete [] Pa;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.