C ++ class (7)-destructor

Source: Internet
Author: User

You are welcome to repost the post, but please mark the author as "nine days Yan Ling". Of course, the link to this post is better.

Without knowing it, I have written six lectures. It is true that this learning can force me to really debug the code in every book, think about something that you can tell you about in a new book. This is really learning. After reading the book, I thought I had mastered it, it doesn't seem to work here in the class, because my c basics are not suitable here .... Well, let's leave it alone. This describe the destructor, relative to the constructor. Destructor are the functions that run at the end of the class declaration cycle. They are generally used to release the resources of a class. As far as I know, Java does not seem to need such a job. They have garbage collectors. I think a rational programmer has advantages and disadvantages in evaluating this situation, similar simplification makes Java the best business model for software development, but it also leaves Java programmers too far away from the underlying layer and lacks their knowledge, because programming JAVA does not require that much knowledge, in addition, Java loses many underlying applications. In addition, such garbage collection consumes resources. At that time, Bjarne strooustrup considered adding such a feature to C ++, but he said, as a system development-level and commonly used language to develop drivers, he cannot accept such a loss of efficiency. Therefore, C ++ does not add this feature. Let's talk more about it. Let's look at the destructor.

Example 7.0:

# Include <string>
# Include <iostream>
Using namespace STD;
Bool being = true; // defines a global bool variable.
Class fruit // defines a class named fruit
{
String name; // defines a name Member.
String color; // defines a color member.
Public:
Void print () // defines the print () member of an output name ()
{
Cout <color <"" <name <Endl;
}
Fruit (const string & NST = "apple", const string & CST = "green"): Name (NST), color (CST)
{
Being = true; // indicates that this object exists.
} // Constructor
~ Fruit () // This is the legendary destructor.
{
Being = false; // It indicates that it does not exist.
}
};
Int main ()
{
{
Fruit Apple ("apple"); // defines a fruit object Apple
Cout <"apple being? : "<Being <Endl;
}
Cout <"apple being? : "<Being <Endl;
Return 0;
}

First of all, don't be surprised. :) what are the constructor and destructor doing. I have said that constructor is to construct a function that will run on a Class Object. destructor are the functions that run at the end of the class lifecycle, not just our general understanding, logically, they can do everything. First, you need to know what they can do. :) also, you need to know when they will work, because we enclose Apple's definition in braces, apple needs to disappear when the braces disappear, so at this time, the Destructor is called. Next, let's take a look at examples of what we can do.

Example 7.1:

# Include <string>
# Include <iostream>
Using namespace STD;
Bool being = true;
Class fruit // defines a class named fruit
{
String name; // defines a name Member.
String color; // defines a color member.
Public:
Void print () // defines the print () member of an output name ()
{
Cout <color <"" <name <Endl;
}
Fruit (const string & NST = "apple", const string & CST = "green"): Name (NST), color (CST)
{
Being = true;
Cout <"Aha, I'm" <name <". I have created! "<Endl;
} // Constructor
~ Fruit ()
{
Being = false;
Cout <"dame it! "<" I'm "<name <". And who killed me? "<Endl;
}
};
Int main ()
{
{
Fruit Apple ("apple"); // defines a fruit object Apple
Cout <"apple being? : "<Being <Endl;
}
Cout <"apple being? : "<Being <Endl;
Return 0;
}

You can run it to see it. :) when an object is defined, he will shout that he has been created. When it disappears, he will announce his death. :) OK, fruit objects seem to have known when they are alive. Let's see when.

Example 7.2:

# Include <string>
# Include <iostream>
Using namespace STD;
Bool being = true;
Class fruit // defines a class named fruit
{
String name; // defines a name Member.
String color; // defines a color member.
Public:
Void print () // defines the print () member of an output name ()
{
Cout <color <"" <name <Endl;
}
Fruit (const string & NST = "apple", const string & CST = "green"): Name (NST), color (CST)
{
Being = true;
Cout <"Aha, I'm" <name <". I have created! "<Endl;
} // Constructor
~ Fruit ()
{
Being = false;
Cout <"dame it! "<" I'm "<name <". And who killed me? "<Endl;
}
};
Fruit banana ("banana ");
Void FB ()
{
Cout <"I am the beginning of a function" <Endl;
Fruit pear ("Pear ");
Cout <"I am the end of a function" <Endl;
}

Int main ()
{
Cout <"I am the beginning of the program" <Endl;
FB ();
Cout <"I am the beginning of the For Loop" <Endl;
For (bool Bi = true; Bi = false)
{
Fruit orange ("orange ");
Cout <"I Am The End Of The for loop" <Endl;
}
{
Cout <"I am the beginning of the statement block" <Endl;
Fruit apple; // In the first case, create a statement block.
Cout <"I am the end of the statement block" <Endl;
}
Cout <"I am the end of the program" <Endl;
Return 0;
}

The life cycle of a class is similar to that of a common variable. A global variable is created first, and the program ends at the end. It is created when a variable in the function is called, when the function ends, the variables in the for loop end at the end of the for loop, and the variables in the statement block end at the end of the statement block. Bjarne stroustrup declared that he wanted the class to be used just like the built-in type. It seems that he is not talking about fun :) It should be noted that even if you didn't define the destructor, the system will also help you define one like defining the default constructor. Let's see when the Destructor is needed?

Example 7.3:

# Include <string>
# Include <iostream>
Using namespace STD;
Class fruit // defines a class named fruit
{
String name; // defines a name Member.
String color; // defines a color member.
Public:
Void print () // defines the print () member of an output name ()
{
Cout <color <"" <name <Endl;
}
Fruit (const string & NST = "apple", const string & CST = "green"): Name (NST), color (CST)
{
Cout <"Aha, I'm" <name <". I have created! "<Endl;
} // Constructor
Fruit (Fruit & AF) // do you still remember me? I'm a replication constructor.
{
Name = "another" + Af. Name;
}
~ Fruit ()
{
Cout <"dame it! "<" I'm "<name <". And who killed me? "<Endl;
}
};

Int main ()
{
Cout <"Main begin" <Endl;
Cout <"created * P" <Endl;
{
Fruit * P = new fruit;
Cout <"created another apple" <Endl;
Fruit Apple (* P );
}

 
Cout <"main end" <Endl;
Return 0;
}

What did you find when you run this program? Yes. First, run the copy constructor and the constructor will not run because another apple did not announce its birth. Second, when the statement block disappears, another apple automatically calls the destructor, he declared that he was "dead", but although the dynamically created object directed by * P announced that he was born, he did not announce that he was dead, this is true even if the program is over !! I don't know if Vc has any measures to recycle the memory. Otherwise, I even suspect that if you debug this program repeatedly, your machine will crash. Of course, if you can, you will not know how many times it will take, but theoretically, it does. This is memory leakage! As a C ++ programmer, you need to know more things than a Java programmer. In return, you can do more things! This is the one you need to remember. Remember to manually undo the dynamically created object. Just like the example below.

Example 7.4:

# Include <string>
# Include <iostream>
Using namespace STD;
Class fruit // defines a class named fruit
{
String name; // defines a name Member.
String color; // defines a color member.
Public:
Void print () // defines the print () member of an output name ()
{
Cout <color <"" <name <Endl;
}
Fruit (const string & NST = "apple", const string & CST = "green"): Name (NST), color (CST)
{
Cout <"Aha, I'm" <name <". I have created! "<Endl;
} // Constructor
Fruit (Fruit & AF) // do you still remember me? I'm a replication constructor.
{
Name = "another" + Af. Name;
}
~ Fruit ()
{
Cout <"dame it! "<" I'm "<name <". And who killed me? "<Endl;
}
};

Int main ()
{
Cout <"Main begin" <Endl;
Cout <"created * P" <Endl;
{
Fruit * P = new fruit;
Cout <"created another apple" <Endl;
Fruit Apple (* P );
Cout <"Delete P" <Endl;
Delete P;
}

 
Cout <"main end" <Endl;
Return 0;
}

In this way, your machine will not crash. When you delete the pointer, the system automatically calls the object's destructor. If the above example cannot destroy your confidence in your memory, let's look at the example below;

Example 7.5:

# Include <string>
# Include <iostream>
Using namespace STD;
Class fruit // defines a class named fruit
{
String name; // defines a name Member.
String color; // defines a color member.
Public:
Void print () // defines the print () member of an output name ()
{
Cout <color <"" <name <Endl;
}
Fruit (const string & NST = "apple", const string & CST = "green"): Name (NST), color (CST)
{
Cout <"Aha, I'm" <name <". I have created! "<Endl;
} // Constructor
Fruit (Fruit & AF) // do you still remember me? I'm a replication constructor.
{
Name = "another" + Af. Name;
}
~ Fruit ()
{
Cout <"dame it! "<" I'm "<name <". And who killed me? "<Endl;
}
};

Int main ()
{
Cout <"Main begin" <Endl;
Cout <"created * P" <Endl;
{
Fruit * P = new fruit [10];
Cout <"created another apple" <Endl;
Fruit Apple (* P );
Cout <"Delete P" <Endl;
Delete [] P;
}

 
Cout <"main end" <Endl;
Return 0;
}

You will find that the constructor is called for each object when you create an array of objects. When you delete a dynamic array object, the system automatically calls the Destructor for each object, not that good, but don't forget that [] in front of P indicates that this is an array, let alone delete it. You can change 10 to a larger number without deleting it. Here is the destructor.

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.