C ++ default constructor of five kinds

class Complex 
{          
 
Private :
     Double m_real;
     Double m_imag; 
 
public : 
 
    // 1, no-argument constructor
     // If you create a class you do not have to write any constructors, the system automatically generates a default no-argument constructor function empty, do nothing
     // As long as you write a certain kind of following the constructor, the system will no longer automatically generate such a default constructor, if you want to have such a no-argument constructor, you need to write your own show out 
    Complex ( void ) 
    { 
         m_real = 0.0 ; 
         m_imag = 0.0 ; 
    } 
         
    // 2, general constructor (also called overloaded constructor)
     // general constructor parameters may have various forms, a class can have a plurality of general structure function, provided a different number or type of parameters (based on the principle of overloaded functions c ++)
     //For example: You can also write a Complex (int num) out of the constructor
     // according to different parameters passed to call a different constructor to create objects 
    Complex ( Double Real, Double imag) 
    { 
         m_real = Real; 
         m_imag = imag;          
     } 
     
    // 3, copy constructor
     // copy constructor for a class object reference parameter itself, for reproducing a new class of objects according to an object existing, general data members of the object already exists in the function will the value of the copy to the newly created object
     // If there is no display to write the copy constructor, the system will create a default copy constructor, but when there is a class member pointer, the system creates the default copy constructor risks, please consult the specific reasons related to "shallow copy", "deep copy" the article discusses 
    Complex ( const Complex & c) 
    { 
        // the data members of the object in the value of c copied 
        m_real =  c.m_real;
        m_img  = C.m_img; 
    }             
 
    // . 4, type conversion constructor, creates a class of this object according to the object of a specified type
     // example: Complex will create an object based on the object type double 
    Complex :: Complex ( Double R & lt) 
    { 
        m_real = R & lt; 
        m_imag = 0.0 ; 
    } 
 
    // . 5, the equal sign operator overloading
     // Note that this is similar to the copy constructor, the right to copy the value = object according to the object class left operand, it does not belonging to the constructor, the left and right sides of the equal sign is the object must have been created
     // If no display writing = operator overloading, the system also creates a default = operator overloading, only some basic working copy 
    Complex & operator = ( const & Complex rhs) 
    { 
        //First, the right hand side is detected whether the object is present to the left if this object itself, directly returns
        IF ( the this == & RHS) 
        { 
            return * the this ; 
        } 
             
        // Copy right-hand side member to the subject left 
        the this -> m_real = rhs.m_real;
         the this -> m_imag = rhs.m_imag; 
             
        // the equal sign the left object again came
         // purpose is to enable connection and other eg: a = b = c = c system first run b
         // then run a = (b = c of the return value, this should be copied after the value of c b Object)     
        return * the this ; 
    } 
};

 

Guess you like

Origin www.cnblogs.com/judes/p/11547045.html
Recommended