3-21 (Supplement of classes and objects)

2. The 6 default member functions of the class:

If there are no members in a class, referred to as an empty class, the empty class does not have nothing, but automatically generates 6 default functions.



Constructor:

It is a special member function with the same name as the class. It is automatically called by the compiler when creating a class type object to ensure that each data member has a suitable initial value and is called only once during the life cycle of the object.

characteristic:

The constructor is a special member function, the main task is not to open space to create an object, but to initialize the object .

The function name is the same as the class name;

No return value;

The compiler automatically calls the corresponding constructor when the object is instantiated .

The constructor can be overloaded;

If the class does not show the definition of the constructor, the c++ compiler will automatically generate a default constructor with no parameters ,

The constructor without parameters, the full default constructor, and the constructor generated by the compiler by default are all called the default constructor, and there can only be one default constructor .

The default constructor generated by the compiler initializes built-in type variables to random values, and calls its default member functions for custom type members .



Destructor:

Contrary to the constructor, his main task is to clean up some of the resources of the class.

characteristic:

The name of the destructor is prefixed with the character ~;

No parameters and no return value.

A class has one and only one destructor. If the definition is not displayed, the compiler will automatically generate the default one.

When the object life cycle ends, the C++ compilation system will automatically call the destructor.

The destructor generated by the compiler will call its destructor on members of the custom type .





Copy constructor:

Only a single parameter, the parameter is a type of class of the object reference (typically modified by const), with an existing object class type when creating a new object is called automatically by the compiler.


characteristic:

The copy function is an overloaded form of the constructor. The constructor passes variables through parameters, and the copy constructor passes objects.

The copy constructor has only one parameter, and it must be passed by reference . Passing by value will cause endless recursive calls.

If the defined constructor is not displayed, the system generates a default constructor, and the default copy constructor object is copied in byte order according to the memory storage , which is called shallow copy.

A shallow copy will cause the destructor to release the same block of addresses twice.



Assignment operator overloading

Operator overloading:

C++ introduces operator overloading in order to increase the readability of the code. Operator overloading is a function with a special function name.

The function name is: operator symbols that need to be overloaded directly after operator.

Function prototype: return value type operator operator (parameter list)

Note : overloaded operators must have a type or class of d enum type operands .

The meaning of operators used for built-in types cannot be changed.

As an overloaded function of a class member, its formal parameter seems to be 1 less than the number of operations, because the member function operator has a default formal parameter this, which is limited to the first formal parameter. (That is to say, when calling a class member function, a this pointer will be hidden to point to the caller).

Note: .* :: sizeof ?:. These 5 operators cannot be overloaded.


Assignment operator overloading:

The return value is returned by reference;

What is returned is *this

If the definition is not displayed, the compiler will generate one, and the completed object will be copied in endianness .










As long as c++ is a custom type, if you want to use an operator, you have to overload the operator.


Guess you like

Origin blog.51cto.com/15085121/2667863