This article mainly introduces the C + + class constructor examples, need friends can refer to the following
The code is as follows:
What does a constructor do?
/* Class object is created, compile the system object to allocate memory space, and call the constructor automatically-> the constructor completes the member's initialization work
Eg:counter C1;
The compilation system allocates the memory space for each data member (M_VALUE) C1 the object, and calls the constructor counter () to initialize the object automatically, and the M_value value after initialization is set to 0
So: the constructor function: initializes the data member of the object. */
Class Counter
{
Public://Class counter constructor, with class masterpieces as function name, no return type
Counter () {
m_value = 0;
}
Private
int m_value; Class-Private data members
}
Ii. types of constructors
#include
using namespace Std;
Class Complex
{
Private:
Double m_real;
Double M_imag;
Public
* Constructor without parameters
If you create a class you do not write any constructors, the default parameterless constructor is automatically generated, the function is empty, nothing is done
As long as you write one of the following constructors, the system will no longer automatically generate such a default constructor, if you want to have such a parameterless constructor, you need to write it yourself
Complex (void)
{m_real = 0.0;
M_imag = 0.0;
}
* General constructors (also called overloaded constructors)
General constructors can have various parameter forms, and a class can have more than one general constructor if the number or type of arguments is different (based on the principle of overloaded functions of C + +)
For example: You can also write a Complex (int num) constructor that calls different constructors depending on the parameters passed in when creating an object
Complex (double real, double imag)
{m_real = real;
M_imag = Imag;
}
* Copy Constructor (also known as copy constructor)
The copy constructor parameter is a reference to the class object itself, used to copy a new object of that class based on an existing object, typically copying the value of the data member of an existing object into a newly created object in a function
If a write copy constructor is not displayed, the system creates a copy constructor by default, but when there are pointer members in the class, the default creation of the copy constructor by the system is risky, as discussed in the article on "Shallow copy", "Deep copy"
Complex (const Complex & c)
{//Copy data member values from Object C
M_real = C.m_real;
M_imag = C.m_imag;
}
* Type conversion constructors, which create an object of this class based on an object of a specified type, it is important to note that this is actually a generic constructor, but for a constructor with such a single parameter, C + + will default to convert the type of the parameter to that class type.
Sometimes this kind of privacy conversion is not what we want, so we need to use explicit to limit this conversion.
For example, the following creates a complex object based on a double type object
Complex (Double R)
{m_real = R;
M_imag = 0.0;
}
equals operator overload (also called an assignment constructor)
Note that this similar copy constructor copies the value of the object of this class on the right side to the object to the left of the equals sign, which is not part of the constructor, and the object on both sides of the equal sign must already be created.
If the Write = operator overload is not displayed, the system also creates a default = operator overload, doing only some basic copy work
Complex &operator= (const Complex &RHS)
{///first detect whether the right side of the equal sign is the object on the left, and if this object itself, return directly
if (this = = &RHS)
{return *this;
}
Copy the member to the right of the equal sign to the object on the left
This->m_real = Rhs.m_real;
This->m_imag = Rhs.m_imag;
The object to the left of the equal sign is again outgoing, in order to support the EG:A=B=C system running B=c first and then running A= (B=c's return value, which should be the B-object after the C value is copied)
return *this;
}
};
Use the class object defined above to illustrate the usage of each constructor:
int main ()
{
Call the parameterless constructor, the data member initial value is assigned to 0.0
Complex c1,c2;
Call the general constructor, the data member initial value is assigned to the specified values
Complex C3 (1.0,2.5);
Of course, you can also use the following form
Complex C3 = Complex (1.0,2.5);
Assigns the value of a C3 data member to a previously created object C1
Since C1 has been created beforehand, no constructors are called
Only the = number operator overload function is called
C1 = C3;
Calling type conversion constructors
The system first calls the type conversion constructor, creates 5.2 as a temporary object of this class, and then calls the Equals operator overload, assigning the temporary object to the C2
C2 = 5.2;
Call copy constructor (there are two ways to invoke it)
Complex C5 (C3);
Complex C4 = c3;
Note the distinction between the and = operator overloads, where the object on the left side of the equal sign is not previously created, so it is necessary to invoke the copy constructor, the parameter is C2
This is particularly important, here is initialization, not assignment.
In fact, there are two types of initialization in C + +: Replication initialization and assignment initialization.
Where C5 uses replication initialization, and C4 takes the assignment initialization, both of which are called copy constructors.
}