C + + basic syntax constructors and initialization tables

Source: Internet
Author: User

The author just system to re-learn the syntax of C + +, see the constructor and initialization table this piece, found that this piece of syntax is a bit complex and very miscellaneous, afraid to forget later, so write this article, later recalled the use.

C + + constructors

  

3. Constructors (constructor)

Class/struct class name {

constructor Function

class name (formal parameter list) { function body }

};

1) constructor name is the same as class name, no return type

2) The constructor is called automatically when the object is created

3) The constructor is primarily responsible for initializing the object, i.e. initializing the member variable

4) constructors are always called automatically at each object's lifetime, but are only called once

Class A {

A (void) {...}// constructor

};

Int Main (void) {

A; automatically call constructors

}

Constructor overloading (overloading its essence is the name of the compiler's changing function)

Constructors can be overloaded by different parameter tables, selecting matches by constructing the type of the argument when the object is created, representing how different objects are created.

2. Default Constructor (default constructor/ no parameter constructor)

1) If a class does not have any constructors defined, the compiler provides a default parameterless constructor

If a constructor is defined, the compiler will no longer provide a default parameterless constructor, regardless of the parameters.

2) for basic type member variables in a class, do not initialize

3) for member variables of class type, call the corresponding parameterless constructor to initialize the

3. Type conversion constructor (single parameter constructor)

Class Target type {

Target type (source type) {......}

};

You can receive a constructor for a single source type Object argument, which supports implicit conversions from source type to target type

Explicit Keywords: constructors for modifying type conversions, forcing the requirement that such conversions must be displayed

C + + basic syntax constructors and initialization tables

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.