Constructors for C + + classes

Source: Internet
Author: User

1. Constructor function

The task of the constructor is to initialize the data members of the class object and execute the constructor whenever the object of the class is created.

    • The constructor name is the same as the class name
    • No return type
    • can be overloaded
    • cannot be declared as const

(1) The default constructor of the composition

If we do not explicitly define a constructor for a class, the compiler implicitly defines a default constructor (without any arguments), also called the composition's default constructor , which initializes the data members of the class according to the following rules:

    1. If there is an initial value in the class, it is used to initialize the member (for example, units_sold=0,revenue=0.0 above). the initial value within the class must be represented by = or {}.
    2. Otherwise, the default initialization (such as Bookno is initialized to an empty string).

(2) Customizing the default constructor

in general, a class needs to define a default constructor, mainly for the following three reasons:

    1. Once other constructors have been defined, the class will not have a default constructor unless we define a default constructor ourselves.
    2. If the class contains data for a built-in type or composite type (such as arrays and pointers), the value of the default constructor using the composition will be undefined if the initial value in the class is not provided (the compiler may not support the initial value within the class).
    3. If the class contains objects of other classes, and this object does not have a default constructor, then the compiler cannot synthesize the default constructor for the class, and we must define a default constructor ourselves.

Sales_data () =default;

The function name is the same as the class name, there is no argument list, and a =default after the argument list requires the compiler to generate a default constructor.

(3) Constructor initial value list

Sales_data (const string & s,unsigned n,double P): Bookno (s), Units_sold (n), Revenue (P*n) {}

The middle part of the colon and braces is the initial list of values for the constructor. If a data member is not included in the list of initial values, the value is implicitly initialized in the same way as the composite default constructor, and is initialized in the same way for constructors that do not have an initial list of values.

2. Constructor initial Value List

(1) The type of the constructor initializer list must be used

Constructors for C + + classes

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.