C ++ class Constructor

Source: Internet
Author: User

This article mainly introduces the example of c ++ class constructor. For more information, see

The Code is as follows:

// I. What is the constructor?

/* When a class object is created, compile the system object to allocate 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 object. After initialization, The m_value of c1 is set to 0.

Therefore, the role of the constructor is to initialize the data members of an object. */

Class Counter

{

Public: // constructor of the Counter class, which uses the class name as the function name and has no return type.

Counter (){

M_value = 0;

}

Private:

Int m_value; // Private Data member of the class

}

// 2. Types of Constructor

# Include

Using namespace std;

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 to 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, which may cause risks. The specific reasons are described in the articles on "shortest copy" and "Deep copy ".

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: creates an object of this Class Based on a specified type object. Note that this is actually a General constructor, however, for single-parameter constructors, 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;

}

// Equal sign operator overload (also called 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 itself is, 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 to support interconnection. For example: a = B = c, the system first runs B = c and then runs a = (B = c's return value, here it should be the B object after copying the c value)

Return * this;

}

};

// 3. Use the class object defined above to describe the usage of each constructor:

Int main ()

{

// Call the no-argument constructor. The initial value of the data member is assigned a value of 0.0.

Complex c1, c2;

// Call a General constructor. The initial values of data members are assigned to the specified values respectively.

Complex c3 (1.0, 2.5 );

// Of course, you can also use the following form

// Complex c3 = Complex (1.0, 2.5 );

// Assign the value of the c3 data member to the created object 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 c2

C2 = 5.2;

// Call the copy constructor (the following two call methods are available)

Complex c5 (c3 );

Complex c4 = c3;

// Note the distinction between = and = Operator overloading. the object on the left of the equal sign is not created in advance, so 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.

// C5 adopts copy initialization, while c4 adopts value assignment initialization. Both methods call 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.