Classes and Objects (Medium)
Article directory
1. Six default member functions of the class
If there are no members in a class, it is referred to asempty class.
Is there really nothing in the empty class? No, when nothing is written in any class, the compiler will automatically generate the following 6 default member functions.
Default member function: If the user does not implement it explicitly, the member function generated by the compiler is called the default member function.
2. Constructor
1.Definition
When we implement data structures such as stacks and queues, we may forget to call Init for initialization! A random value appears in the crash.
Can we guarantee that the object must be initialized? -- Constructor (equivalent to an initialization function)
2.Characteristics
The constructor is a special member function. It should be noted that although the name of the constructor is called constructor,
its main task is not Instead of creating an object, it initializes the object.
3. Understanding of characteristics and some precautions
1. When constructing a function, if there are default functions and functions with semi-default parameters, there may be calling ambiguity, there is ambiguity!
2.The default constructor includes:Constructor without parameters + Full default constructor + We did not write the compiler to generate it by default Constructors
Features of these default constructors:Can be called without passing parameters
3. Regarding the default member functions generated by the compiler, many children will have doubts: if the constructor is not implemented, the compiler will generate a default constructor. But it seems that the default constructor is useless? The d object calls the default constructor generated by the compiler, but the d object _year/_month/_day is still a random value. In other words,the default constructor generated by the compiler is of no use? ?
4. Constructors are generated by default, a: built-in type members are not processed; b: custom type members go back and call their default constructors
5.Understanding of point 5
If there is no explicitly defined constructor in the class, the C++ compiler will automatically generate an The default constructor with parameters will no longer be generated by the compiler once
is explicitly defined by the user.
3. Destructor
1.Definition
Destructor: Contrary to the function of the constructor, the destructor does not complete the destruction of the object itself. The local object destruction work is performed by
Completed by the compiler. When the object is destroyed, the destructor will be automatically called to complete the cleanup of the resources in the object
2.Characteristics
3. Understanding of characteristics and some precautions
1. In the third point, the characteristics of the destructor generated by default: similar to the constructor:
a. Built-in types are not processed; b. Custom types call it back Destructor
2. There is nothing to clean up in the destructor in the date class, so you don’t need to write it, just use the destructor automatically generated by the compiler!
3. When we were learning the data structure of a stack before, we did a question: Use two queues to implement a stack!
At that time, we used C language to write, and we could not return directly at the end. Need to Destroy (troublesome and easy to forget)
In C++, we can directlyreturn st.Empty() (out of scope, st automatically calls the destructor)
So how do we write a custom member of Stack and call the Stack destructor?
4. Copy constructor
1. Nature
Copy constructor: There is only a single formal parameter, which is a reference to an object of this class (generally commonly used const modification),
in Automatically called by the compiler when creating a new object from an existing class type object.
2.Characteristics
3. Understanding of characteristics and some precautions
1. Why does the copy constructor use references to pass parameters instead of pointers?
Before understanding this problem, we first understand the difference between passing parameters by value and passing parameters by reference!
Solution:Use references, d is an alias of d1. Or use pointers (try to pass parameters by reference, using pointers will be a bit strange, and you need Date d2(&d2 ))
2. Try to add const when passing parameters to the copy constructor
Solution:Add a const to avoid this situation and reduce the permissions!
3. What does the default copy constructor generated by the compiler do?
Built-in types are copied directly in byte format, while custom types are copied by calling their copy constructor. Copy in byte order according to memory storage, ==memcpy
4. Sometimes shallow copy will cause the program to crash, so we should try our best to implement deep copy ourselves.
5. Assignment operator overloading
To better understand assignment operator overloading, it's best to simulate implementing the date class yourself!
1. Operator overloading
Operator overloading is a function with a special function name. It also has its return value type, function name and parameter list. Its return value type and parameter list are similar to ordinary functions.
The function name is : The keyword operator is followed by the operator symbol that needs to be overloaded.
Function prototype: return value type operator (parameter list)
2. Assignment operator overloading
3. Overloading of address-taking and const address-taking operators
These two default member functions generally do not need to be redefined. The compiler will generate them by default
These two operators generally do not need to be overloaded. Use the default addresses generated by the compiler. Just overload, only in special circumstances, you need to overload, such as if you want others to get the specified content!
Summarize
The above is what I will talk about today. This article introduces the 6 default member functions of classes and objects! Date content will be updated continuously in the future!
If my blog is helpful to you, remember to support it three times in a row. Thank you everyone for your support!