In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand the classes and objects in C++, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail, people with this need can come to learn, I hope you can gain something.
one。 Constructor 1. Definition of constructor:
The constructor is a special member function with the same name as the class name, which is automatically called by the compiler when the class type object is created, ensuring that each data member has an appropriate initial value and is called only once in the object's life cycle. In fact, the function of the constructor is to complete the initialization of member variables, but unlike the initialization constructor of c language, it can complete the initialization of member variables at the same time of creating objects.
two。 Characteristics of the constructor:
1. The function name is the same as the class name.
two。 No return value.
3. The compiler automatically calls the corresponding constructor when the object is instantiated.
4. The constructor can be overloaded.
3. Implementation of the constructor:
There are three main implementations of constructors.
1. When the user does not implement the constructor, the system will create one by default, and the system will assign random values to the member variables of the built-in type, and their constructors will be called for the member variables of the custom type. (note: built-in types generally refer to defined types such as int char double float, and custom types refer to types such as struct and the class class).
two。 Of course, users can also implement the constructor themselves, one of which is no parameter construction.
3. Class one is a parameterized structure, but we use a full default construct in the parameterized construction. Let's show it in code:
3.1. System default constructor
We can see that when we do not construct a function in the Data class, the system will create its own constructor by default, assign random values to the built-in type variables, and the custom type will call its own constructor (if the custom type does not define the constructor, then _ A1 and _ a2 in this example will also be given random values)
3.2 No-parameter structure
3.3 with parameter structure
Here's a question about the style of the code: what is the final result of the member variable year?
Class A {public:A (int year) {year = year;} private:int year;}; int main () {A (20);}
The answer is: random value. So why random values? The main thing here is that it adopts the principle of proximity between variables, so the year on the left side of the equation will directly find the nearest variable, so it will assign the year on the right side of the equation directly to itself, so the final value of year is a random value.
Let's move on to the parameterized constructor, which is generally recommended to use the full default constructor (note:
Both the no-parameter constructor and the full default constructor are called default constructors, and there can only be one default constructor. The no-parameter constructor, the all-default constructor, and the constructor that we did not write by default generated by the compiler can all be considered default member functions.
)
Two destructor function
The initialization of the object is completed during the constructor, so how is an object destroyed?
1. The definition of destructor
Contrary to the function of the constructor, the destructor does not destroy the object, but the compiler does the destruction of the local object. When the object is destroyed, the destructor is automatically called to complete some cleaning of the class.
two。 Characteristics of destructor
1. The name of the destructor is preceded by the character ~.
two。 No parameters, no return value.
3. A class has one and only one destructor. If it is not explicitly defined, the default destructor is automatically generated.
4. At the end of the object's life cycle, the C++ compiler system automatically calls the destructor.
Here we use the stack example to illustrate the implementation and function of the destructor.
Class Stack {public: Stack (int capacity = 4) {_ a = (int*) malloc (sizeof (int) * capacity); if (_ a = = nullptr) {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.