The constructor and destructor calls Tips

1, meaning the constructor: class member function name consistent with the name of the class that contains it, this member function called a constructor

2, Constructor Features: constructor is a special member function, without the user calls, automatically executed when creating an object, but we can also manually call.

form
class Test2 
{ 
    public : 
    Test2 ()   // no-argument constructor 
    { 
        M_A = 0 ; 
        M_B = 0 ; 
    } 
    Test2 ( int A) // there argument constructor 
    { 
        M_A = A; 
        M_B = 0 ; 
    } 

    Test2 ( int A, int B) // there is a constructor parameter   
    { 
        M_A = A; 
        M_B = B; 
    }
    // assignment constructor (copy constructor) 
    Test2 ( const Test2 & obj) 
    { 
        cout << " I am the constructor " << endl; 
    } 
    public :
     void printT () 
    { 
        cout << " ordinary member functions " << endl; 
    } 
    Private :
     int m_a;
     int M_B; 
}; 
// no-argument constructor calls the 
void main () 
{ 
    Test2 t0;   // create the object, nothing to write default constructor is called with no arguments 
    return ; 
}
Call the no-argument constructor
class Test2 
{ 
    public : 
    Test2 ()   // no-argument constructor 
    { 
        M_A = 0 ; 
        M_B = 0 ; 
    } 
    Test2 ( int A) // there argument constructor 
    { 
        M_A = A; 
        M_B = 0 ; 
    } 

    Test2 ( int A, int B) // there is a constructor parameter   
    { 
        M_A = A; 
        M_B = B; 
    }
    // assignment constructor (copy constructor) 
    Test2 ( const Test2 & obj) 
    { 
        cout << " I am the constructor " << endl; 
    } 
    public :
     void printT () 
    { 
        cout << " ordinary member functions " << endl; 
    } 
    Private :
     int M_A;
     int M_B; 
}; 
// with a call reference constructor 
void main () 
{ 
    // . 1 bracketing 
    Test2 T1 ( . 1 , 2);   // call the constructor two parameters
     // 2 = number France 
    Test2 T2 = ( . 3 , . 4 , . 5 , . 6 , . 7 ); // = the equal sign character of the C ++ enhancements 
    Test2 = T3 . 5 ;
     // . 3 directly manually call the constructor calls the constructor 
    Test2 Test2 t4 = ( . 1 , 2 );    // generate anonymous object, initializing the object t4
     //
     T1 = t4;   // to to t4 Copy T1   // assignment 
     // initialization of an object the assignment and the object are two different concepts 
    return ; 
}
There are calls arg constructor
Test4 class 
{ 
public: 
	Test4 () // constructor without parameters 
	{ 
	    M_A = 0; 
	    M_B = 0; 
	} 
	Test4 (int A) 
	{ 
            M_A = A; 
        M_B = 0; 
	} 
	Test4 (A int, int B) // there // constructor parameters of the three methods 
	{ 
	    M_A = A; 
	    M_B = B; 
	} 
	// constructor assignment (copy constructor) // 
	Test4 (const Test4 & obj) 
	{ 
	    M_B obj.m_b + = 100; 
	    M_A = obj.m_a 100 +; 
	} 
public: 
	void printT () 
	{ 
	  COUT << "normal member functions" << endl; 
       COUT << "M_A" << << M_A "M_B" << endl << M_B; 
	} 
Private:
	int m_a;
	M_B int; 
}; 

// Assignment. 1 with a constructor initializes the object to another object   
void main () 
{ 
	Test4 T1 (. 1, 2); 
	Test4 T0 (. 8,. 9); 

	these two will call assignment configuration // function 
	Test4 t2 = t1; // initialize with t1 to t2, the assignment will call the constructor. t1 obviously obj, t2, and member variables can be used to control the member variables t1, t2 similarly as the dependent variable in the mathematical, as independent variables t1, t2 = F (t1) 
        Test2 T5 (t1); // call anonymous object 

     // the following two will not
     Test4 t4 = Test4 (1,4); // initialize t4 object. The constructor parameter which has the same name and t4 member variable assignment, the latter (t4) is equal to the value of the same variable also the former.
     t0 = t1; // t0 to t1 using the assignment operator (common variable assignment member). And initialization are two different concepts.
	t2.printT (); // result m_a = 101 m_b = 102 This is a new private member variable t2 value 
     t4.printT (); // result is m_a = 1 m_b = 4 This is a new member variables private value t4
t0.printT (); // result is m_a = 1 m_b = 2 which is a new member variables t0 private value
t5.printT (); // result m_a = 101 m_b = 102 this is a new member variables private value t5
return; }

  

cout << "There argument constructor" << endl;}
// Constructor assignment (copy constructor) // Test4 (const Test4 & obj ) {cout << " I am also a constructor" << endl; m_b = obj. + 100 M_B; M_A obj.m_a + = 100;}
public: void printT () {COUT << "normal member functions" << endl; cout << "m_a " << m_a << "m_a" << m_b < <endl;} Private: M_A int; int M_B;};
// assignment. 1 with a constructor initializes the object to another object void main41 () {Test4 t1 ( 1, 2); Test4 t0 (1, 2);
= // assignment operator will call constructors // operator = () // throwing bricks t0 = t1; t0 to t1 // used and initialization operations are assigned to two different concepts
// call the first type method Test4 t2 = t1; // initialize with t1 to t2 t2.printT (); cout << " hello ..." << endl; system ( "pause"); return;}

Guess you like

Origin www.cnblogs.com/anSn/p/11589032.html