C + + constructors and destructors

Source: Internet
Author: User

With this piece of code,

Class person{private:char* Name;char age;public:void SetName (char* name) {this->name = name;} int Setage (char age) {this->age = Age;return 1;}}; Person Person;person. SetName ("Chentong");p Erson. Setage (20);

I want to initialize the member variable, so I have to do it every time through a class variable call member function, which is certainly possible, but rather troublesome, in order to solve this problem, C + + introduced a concept such as constructor function. What is a constructor function? A function that is the same as the class name, called a constructor. Constructors can have multiple, how to differentiate? Distinguished by the difference in the argument list. If we do not write our own constructor, then the default constructor is called automatically by the system. However, if we implement the constructor ourselves, the system will no longer invoke the default constructor. Code as follows,

Class person{private:char* Name;char age;public:void SetName (char* name) {this->name = name;} int Setage (char age) {this->age = Age;return 1;} void person () {}void person (char* name, char-age) {This->name = Name;this->age = Age;}};

Then we want to initialize the variable, as long as,

Person person ("Chentong", 20);

This allows all members to be set whenever a constructor is called.

Then, call the constructor with no arguments, as follows:

Person Person1;

This is called a constructor without arguments and cannot be written like this,

Person Person1 ();

Because, the meaning of this writing is to declare a function. Because, in this case, it is equivalent to an int FAC (); The Declaration of the function. Incidentally, after you rewrite the constructor, be sure to write a constructor without parameters, otherwise the compilation cannot pass.

Access by pointer

person* person1 = new person; person* Person2 = new Person (); Person1 and Person2 function exactly the same, they call the parameterless constructor person* Person3 = new person [2]; person* person4 = new Person ("Chentong", 20);

Freeing memory

Delete Person1;delete person2;delete []person3;delete person4;

The following code is available:

class person{private:char* name;char age;char* job;public:void setname  (  char* name ) {this->name = name;} int setage  ( char age ) {this->age = age;return 1;} void setjob  ( char* job ) {this->job = job;} Void person () {}void person ( char* name, char age, char* job ) { This->name = new char[strlen (name) +1];strcpy  ( this->name, name ); This->age = age;this->job = new char[stlren (Job)  + 1];strcpy  (  this->job, job );} void person  ( char* name, char age, char* job =  "None"   ) {//this->name = name;this->name = new char[strlen[name + 1];strcpy   ( this->name, name ); this->age = age;//this->job = job;this->job = new char[strlen[name  + 1];strcpy  (  this->job, job );}};

This code stores the data through the new dynamic request space. In C + +, new is used to dynamically request space, and delete is used to free up space for dynamic requests. So what's the difference between malloc () in C and new in C + +? They can be used for dynamic application space, malloc application space, if not to actively release, then the application of space will not be released, which will cause memory waste. The space of the new dynamic application will not be released when the program executes, and the space of the dynamic request will not be released until the execution of the program is completed. If you want to release the space after you've finished using it, you can do so with the delete.

Obviously, we can write the code that frees the space as a function, and when the space is used, the free function is called and the space can be freed. If there is more space for a dynamic request, there may be a case of missing releases, so in order to avoid this, C + + introduces a concept such as a destructor. Once the dynamic space has been used, the destructor is automatically called, so that the space is freed. Destructors are similar to constructors in that they are identical to the class name, except that a wavy line is added to the class name. Incidentally, both constructors and destructors do not return a value type. The code is as follows:

Class people {private:char* name;char age;char* job;public:void setname (char*  name)  {this->name = new char[strlen (name)  + 1];strcpy (This->name,  name);} Int setage (char age)  {if  (age > 150 | |  age < 0)  {age = 0;return -1;} This->age = age;} Void setjob (char* job)  {this->job = new char[strlen (Job)  + 1]; strcpy (this->job, job);} Void printinfo ()  {cout <<  "name = "  << this->name  <<  ", age = "  << this->age <<  " job =   "&NBSP;&LT;&LT;&NBSP;THIS-&GT;JOB&NBSP;&LT;&LT;&NBSP;ENDL;} People ()  {this->name = null;this->job = null;} People (Char* name, char age, char* job)  {this->name = new&Nbsp;char[strlen (name)  + 1];strcpy (this->name, name);this->age = age;this-> Job = new char[strlen (Job)  + 1];strcpy (this->job, job);} ~people ()  {if  (this->name != null) delete this->name;if  (this->job  != null) delete this->job;}};

The ~people () is a destructor. When the program finishes executing, the destructor is automatically called. The space for the dynamic application is released.

This article is from "Van Gogh said my brain is sick" blog, please be sure to keep this source http://chen0547.blog.51cto.com/12489941/1978275

C + + constructors and destructors

Related Article

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.