The constructor and destructor, these two functions will be the compiler
automatically calls constructors
complete object
initialization action, the destructor
is completed at the end of the object
cleanup
.
Note: Initialization and cleanup object is the compiler
to force
us to do,
even if you do not provide initialization and cleanup operations
, the compiler will give you increase
the default action
, but the
default initialization operation does not do anything
.
Constructors: instantiate an object when the system automatically calls
Destructor: automatically released when an object call
The constructor syntax:
The constructor and class names are the same, no return type, not even be void, but there may be parameters, can be overloaded
Destructor syntax:
Destructor function name is preceded by the class name "-", with no return type, not even void can not have parameters, can not be overloaded
. 1 class the Data 2 { . 3 public : . 4 int NUM; . 5 public : . 6 // constructor (the constructor with no arguments) . 7 the Data () . 8 { . 9 NUM = 0 ; 10 COUT << " no-argument constructor " << endl; . 11 } 12 is // constructor (with reference to structure) 13 is the Data ( int n-) 14 { 15 NUM = n-; 16 COUT <<" There constructor parameter " << endl; . 17 } 18 is . 19 // destructor 20 is ~ the Data () 21 is { 22 is COUT << " destructor " << endl; 23 is } 24 25 }; 26 is void Test01 ( ) 27 { 28 // class instance object constructor is called custom system 29 the Data ob; 30 31 is // function ends when the object ob is locally released automatically calls the destructor 32 } 33 is int main ( int argc,char *argv[]) 34 { 35 cout<<"-----001-----"<<endl; 36 test01(); 37 cout<<"------002-------"<<endl; 38 return 0; 39 }
1, the constructors classification:
Parametric Type: divided and have no argument constructor argument constructor
Classification of: General constructor and copy constructor (copy constructor)
2, call the constructor
class Data { public : int NUM; public : // (no-argument constructor) Constructor the Data () { a = 0 ; COUT << " no-argument constructor = NUM " << NUM << endl; } // constructor (with configuration parameters) the Data ( int n-) { Surely = n; COUT << " There argument constructor function num = " << NUM << endl; } // destructor (no return value type parameter can not be overloaded no) ~ the Data () { COUT << " destructor = NUM " << NUM << endl; } }; void test02() { // call the default configuration parameters, or without (implicit call) the Data OB1; // call the constructor with no arguments (display call) the Data OB2 = the Data (); // call has parameters configured (implicit call) the Data OB3 ( 10 ); // call has parameters configured (to show the call) the Data OB4 = the Data ( 20 is ); // implicit conversion invoke configured with a reference (directed to only one data member) (try not to use) the Data OB5 = 30 ; // converted to Data ob5 (30) // anonymous object (the end of the current sentence anonymous objects immediately release) the Data ( 40 ); cout<<"------"<<endl; // Do not call the constructor with no arguments with the way it }
Note: the same scope opposite order of the constructor and destructor
3, copy constructor (the system provides a copy constructor assignment)
. 1 void TEST03 () 2 { . 3 the Data OB1 ( 10 ); . 4 COUT << " ob1.num = " << ob1.num << endl; . 5 . 6 // call the copy constructor (if the user does not realize the system configuration of the copy call the default copy constructor) 7 // default copy constructor: simple whole assignment (shallow copy) 8 // If the user system is configured to achieve a copy of the user calls the copy constructor implemented . 9 10 the Data OB2 (OB1); // implicit invocation copy constructor . 11 COUT << " ob2.num = " << ob2.num << endl; 12 is 13 is the Data OB3 the Data = (OB1);// display call the copy constructor 14 cout<<"ob3.num = "<<ob3.num<<endl; 15 16 Data ob4 = ob1;//=隐式转换调用 17 cout<<"ob4.num = "<<ob4.num<<endl; 18 }