C ++ class Constructor

Source: Internet
Author: User

I. What are Constructors?
Class counter
{
Public:
// Constructor of the counter class
// Feature: the class name is used as the function name and no return type is returned.
Counter ()
{
M_value = 0;
}
PRIVATE:
// Data member
Int m_value;
}
When the class object is created, the Compilation System Object allocates memory space, And automatically call the constructor-> the constructor completes member initialization.
Eg: Counter C1;
The compilation system allocates memory space for each data member (m_value) of the object C1, and calls the constructor counter () to automatically initialize the m_value of the object C1 and set it to 0.
Therefore:
The role of the constructor is to initialize the data members of an object.
Ii. Types of Constructor
Class Complex
{
PRIVATE:
Double m_real;
Double m_imag;
Public:
// No parameter Constructor
// If you do not write any constructor to create a class, the system automatically generates the default non-argument constructor. The function is empty and does nothing.
// As long as you write a constructor below, the system will not automatically generate such a default constructor. If you want such a constructor, you need to write it out on your own.
Complex (void)
{
M_real = 0.0;
M_imag = 0.0;
}
// General constructor (also weigh the loaded constructor)
// A General constructor can have various parameter forms. A class can have multiple General constructor, provided that the number or type of parameters are different (based on the principle of C ++ overload function)
// For example, you can also write a complex (INT num) constructor.
// Call different constructors based on different input parameters when creating an object
Complex (double real, double IMAG)
{
M_real = real;
M_imag = imag;
}
// Copy constructor (also called copy constructor)
// The copy constructor parameter is a reference of the class object. It is used to copy a new class object based on an existing object, in a function, the data member values of an existing object are copied to the newly created object.
// If no displayed write-copy constructor is displayed, the system creates a copy constructor by default, by default, the replication constructor is created by the system. For details about the causes, see Article Discussion
Complex (const complex & C)
{
// Copy the data member value in Object C
M_real = C. m_real;
M_imag = C. m_imag;
}
// Type conversion constructor, which creates an object of this Class Based on a specified type object ,
// Note that this is actually a General constructor, but for Single-parameter constructor, c ++ converts the type corresponding to the parameter to this type by default. Sometimes, this type of private conversion is not what we want, so we need to use explicit to limit this type of conversion.

// For example, a complex object is created based on a double object.

Complex (Double R)
{
M_real = R;
M_imag = 0.0;
}
// The equal sign operator is overloaded. ( Also called the value assignment constructor)
// Note: this is similar to the copy constructor. Copy the value of the = object on the right to the object on the left of the equal sign. It does not belong to the constructor, objects on both sides of the equal sign must have been created
// If no explicit write = Operator overload is displayed, the system will also create a default = Operator overload to only perform some basic copy operations.
Complex & operator = (const complex & RHs)
{
// First, check whether the object on the right of the equal sign is the object on the left. If the object is itself, the system returns
If (this = & RHs)
{
Return * this;
}
// Copy the member on the right of the equal sign to the object on the left.
This-> m_real = RHS. m_real;
This-> m_imag = RHS. m_imag;
// Output the object on the left of the equal sign again
// The purpose is to support the interconnection of Eg: a = B = C system first run B = C
// Then run a = (B = C's return value. Here it should be the B object after copying the C value)
Return * this;
}
};

The class objects defined above are used to describe the usage of each constructor:
Int main ()
{
// Call the no-argument constructor. The initial value of the data member is assigned as 0.0.
Complex C1, C2;

// Call a common constructor. The initial value of the data member is assigned a specified value.
Complex C3 (1.0, 2.5 );
// You can also use the following format
Complex C3 = complex (1.0, 2.5 );

// Assign the value of the C3 data member to C1
// Since C1 has been created in advance, no constructor will be called.
// Only the = Operator is called to reload the Function
C1 = C3;
// Call the type conversion Constructor
// The system first calls the type conversion constructor, creates 5.2 as a temporary object of this class, then calls the equal sign operator to overload it, and assigns the temporary object to C1
C2 = 5.2;
// Call the copy constructor (the following two call methods are available)
Complex C5 (C2 );
Complex C4 = c2; // note that the object on the left of the equal sign is not created beforehand. Therefore, you need to call the copy constructor. The parameter is C2.
// This is especially important. Initialization is not a value assignment. In fact, two initialization methods in C ++ are involved: Copy initialization and value assignment initialization. Among them, C5 adopts the replication initialization, while C4 adopts the value assignment initialization. Both methods call the copy constructor.
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.