C ++ constructor and destructor

Source: Internet
Author: User

C ++ constructor and destructor

Although I have learned the C language, I still don't understand some basics in C ++, and I still need to master it. Old Fan also began to talk about the C ++ design model. He had to read it quickly, or he would have to drop money in the white nest.

For memory leaks, my personal understanding is that the program opened up its own space during the running process, but it was not released after it was used up. I made such a low-level error tonight, causing the program to fail to run. Check the code first:

# Include <iostream>/* run this program using the console pauser or add your own getch, system ("pause") or input loop */using namespace std; class person {public: person () {cout <"the base class constructor is being executed ..... \ n ";}~ The person () {cout <"base class destructor is being executed ..... \ n ";}}; class DS: public person {public: DS () {cout <" execution of the derived class constructor ..... \ n ";}~ DS () {cout <"The destructor of the derived class is being executed ..... \ n ";}}; int main (int argc, char ** argv) {DS p; return 0 ;}

There is no problem with this Code. The program starts to run from the main function and instantiates the derived class DS, an object p. In any case, the derived class DS first calls the constructors of the base class person, then the derived class DS calls its own constructor, followed by its own destructor, and finally the base class person constructor. The running result is shown in:

Actually, when can I call the base class structure (analysis structure) and the derived class structure (analysis structure)? My personal understanding can be expressed by a simple graph, the base class structure and structure are like a large framework that contains the structure and structure of the derived class:

 

1. I will continue to modify the above program in the main function.If it is new, but there is no delete, (matching and compatibility are used, and the results are the same)

int main(int argc, char** argv) {DS *p=new DS();
        // person *p= new DS();
return 0;}

At this time, the problem occurs. If it is in C ++, the following result will appear:


The reason is that it is new, but there is no delete, causing memory leakage. The Destructor will not be called during the program running, and the system will automatically release the memory until the entire program ends.

2. I will continue to modify the above program in the main function,When delete p is added this time, the operation is now matched, that is, the pointer of the derived class points to the derived object:

int main(int argc, char** argv) {DS *p=new DS(); delete  p;return 0;}


You should also guess the running results. There are four

 

3. I will continue to modify the above program in the main function. This time, compatibility means that the base class Pointer Points to the object of the derived class:

<strong>int main(int argc, char** argv) { person *p=new DS(); delete  p;return 0;}</strong>

 

However, this problem occurs again. The running result does not contain the destructor of the derived class. This is a disaster caused by compatibility, because the base class pointer can only point to the part where the derived class inherits its own, for the DS part of the derived class, the base class pointer cannot be mapped, so it is not called. The demonstration result is as follows:

It is not difficult to solve this problem. At this time, the base class pointer should be the base class pointer, regardless of his or her own, at this time, I only need to add a virtual (virtual feature) in the destructor of the base class person, although in the main function, it is still the third case. I can easily output the following results:

In fact, this involves inheritance (compatible rules) and polymorphism knowledge. In the C ++ involved mode, 95% all use polymorphism, which is undoubtedly the focus of C ++, be sure to study this part well.

Let's talk about inheritance and polymorphism. The next blog is about to write close-up. Haha, that is an interesting example-daughter-in-law cooking and factory model, slowly, I entered the core of C ++ ..

Next, we need to talk about the value of deleting the pointer delete in C ++:

Let's talk about the code first.

# Include <iostream>/* run this program using the console pauser or add your own getch, system ("pause") or input loop */using namespace std; int main (int argc, char ** argv) {int * p; p = new int; * p = 3; cout <"output value * p =" <* p <endl; cout <"output address p =" <p <endl; delete p; // cout after delete p <"delete p output value * p =" <* p <endl; cout <"delete p after the output address p =" <p <endl; return 0 ;}


I have defined an integer pointer p, A new integer unit of the int type, and assigned a value of 3 to * p. Then, I output the addresses of * p and p, then I am in delete p. In fact, each delete operation actually deletes the value that p points to that space and does not delete its address. Therefore, the following result is displayed:

Well, I can only write it here with limited capabilities. I will be able to improve my skills in the future, and I will modify my skills in the wrong places. I am sleeping, and I am sleepy ,,,,

 


What are constructor and destructor? What is the function? Why?

Constructor is a special method used to initialize an object when creating an object, that is, assigning an initial value to the object member variable, the new operator is used together with the new operator. In the statement for creating an object, a special class can have multiple constructors to distinguish them (constructors) based on the number of parameters or different parameter types..
Destructor is opposite to constructor. when an object is out of its scope (for example, the function where the object is located has been called), the system automatically executes the destructor. Destructor are often used to clean up the aftermath (for example, a piece of memory space is opened up with new when an object is created, and should be released with delete in the Destructor before exiting ).

Similarities and differences between constructor and destructor in C Language

No constructor or destructor in C Language
 

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.