First, the constructor function
1. A constructor is an object that is automatically invoked when an object is created, and must be called once and only once (not including forced calls) throughout the object's lifetime.
2, in the constructor can be responsible for the initialization of member variables, allocation of resources, set the initial state of the object.
3. Constructors can have multiple versions, which construct overloads between different versions, create objects differently, give different arguments, call the appropriate constructors, and may cause compilation errors if the calling constructor does not exist.
4. If a constructor is not defined in the class, the compiler automatically generates an argument-free constructor, and when a different version of the constructor is customized, the system default parameterless function is no longer generated. However, to prevent an error from creating an object without a parameter, it is recommended that you set 2 constructors.
5, no parameter constructs may not have the parameter, in C + + The function can have the default parameter, if has the parameter constructs all set the default parameter, will and the non-parameter constructs have the conflict, they 2 can have only one existence.
6, copy constructor is a special constructor, is to use an existing object to construct its homogeneous copy object, is the object cloning.
#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;classstudent{Charname[ -]; Charsex; ShortAge ; Public: Student (Const Char* _name,Char_sex, Short_age) { if(Strlen (_name) < -) {strcpy (name,_name); } Sex=_sex; Age=_age; } Student (Student&That ) {strcpy (name,that.name); Sex=That.sex; Age=That.age; cout<<"I've been called."<<Endl; } voidShowvoid) {cout<<name <<" "<<sex <<" "<< age<<Endl; }};intMain () {Student stu1 ("hehe",'m', -); Student STU2=stu1; Stu2.show ();}
7. Similarly, the compiler will generate a copy constructor by default, and compile the generated copy constructor by default for each member in the byte-by-bytes replication class. If there is a member of Class B in Class A, the copy constructor of Class B is automatically called in the copy constructor of Class A.
8, the programmer can customize the copy construction to replace the default copy construction, once the custom copy constructor, then the compiler is no longer generated, and the copy constructor and constructor is different, copy construction can only have one, cannot be overloaded.
9, in general, the compiler generated copy construction is fully sufficient, do not need to customize the copy constructor.
10, the assignment constructor is an object to assign value to another object when the function of the call, and the copy construction is different, the object A and B are already constructed, this time to do is to speak B =a, and copy construction is required object A to create object B.
#include <iostream>#include<string.h>using namespacestd;classstudent{Char*name; Charsex; ShortAge ; Public: Student (Const Char* _name="",Charsex='F', ShortAge=1): Sex (Sex), Age {Name=New Char[Strlen (_name) +1]; strcpy (Name,_name); } Student (Conststudent&That ) {Name=New Char[Strlen (That.name) +1]; strcpy (Name,that.name); Sex=That.sex; Age=That.age; } voidShowvoid) {cout<< name <<" "<< Age <<Endl; } voidAddage (void)Const{ Age++; } void operator= (student&That ) { if( This= = &that)return; Delete[] name; Name=New Char[Strlen (That.name) +1]; strcpy (Name,that.name); Sex=That.sex; Age=That.age; } ~student (void) { Delete[] name; }};intMain () {Student S1 ("Lili",'m', -); S1.show (); Student S2=S1; S2.show (); S2.addage (); S2.show (); S2=S1; S2.show ();}
11, the same compiler will default to generate an assignment constructor, generally, the default assignment is basically enough, unless a member is a pointer, pointing to the additional memory space, in this case we need to customize the copy constructor and assignment constructor.
Second, the destructor function
1, when the object is destroyed automatically called the function called destructors, the entire life cycle of the object can only be called once, it is the object is destroyed before the last execution of the action.
Class name
{
cannot be overloaded, only one///no return value, no parameters
~ Class name (void)
{
}
} 2, the compiler will default to produce a destructor, the default destructor is responsible for destroying the members can be seen, if a member is a class, will automatically call the member's destructor, the member's destruction process is the opposite of the construction process. 3, destructors, although not with overloading, but can be customized, there is a custom destructor default destructor will not be generated. 4. When there are resources in the class that are not visible to the constructor (New/malloc), when there is a need to do so, you need to customize the destructor when the open file is closed/saved to the retrieved data.
C + + 's construction and destructor