C ++ Study Notes (13): constructor, learning note Constructor

Source: Internet
Author: User

C ++ Study Notes (13): constructor, learning note Constructor

1. What is a constructor?

Each class defines the initialization method of its object. The class controls the initialization process of its object through one or several special member functions, namely, constructor.

The task of the constructor is to initialize the data member of the object.Whenever a class object is created, the constructor is executed.


2. Differences between constructors and other functions


① The constructor has a (or 0) parameter list and a (or 0) function body.

② A class can contain multiple constructors, which is the same as the overloaded function.


① The constructor name is the same as the class name, but not the other functions.

② The constructor does not return a type.

③ Const cannot be declared as const.


3. default constructor

Sometimes we do not provide an initial value for the objects in the class, but the objects must be initialized. In fact, they execute the default initialization process. Class controls the default initialization process through a special constructor. This function is called the default constructor.By default, function creation is sufficient without any real parameters..

If our class does not display a defined constructor, the compiler will implicitly define a default constructor for us-the default constructor for synthesis.

Generally, you need to define your own default constructor for any class, because sometimes some classes cannot depend on the synthesis of default constructor for three reasons:

① The Compiler generates a default constructor for us only when it finds that the class does not contain any constructor. Once other constructors are defined, no default constructor will exist unless we define another default constructor.

② For some classes, the default constructor for merging may perform incorrect operations. For example, if the array or pointer type in the class is initialized by default, their values are undefined.

③ Sometimes the compiler cannot synthesize the default constructor for some classes. For example, if a class contains a type member of another class and this type member does not have a default constructor, the compiler Cannot initialize this member.


4. Define Constructors

The following example is introduced:

Struct Sales_data {

Sales_data () = default; // use the default constructor

Sales_data (const string & s, unsigned int n, double p ):

BookNo (s), units_sold (n), revenue (p * n ){}

// List of Constructors


In the above section,

Sales_data () = default;

This constructor does not accept any form parameters, so it is a default constructor. The significance of defining this constructor here is that we need both constructors of other forms and default constructor. This function is equivalent to the default constructor used for synthesis.

In the new C ++ 11 standard, if we need default behavior, we can ask the compiler to generate constructors by writing = default after the parameter list.

Let's take a look at this program:

Sales_data (const string & s, unsigned int n, double p ):

BookNo (s), units_sold (n), revenue (p * n ){}

New Expressions and new components appear in this definition. The preceding statement is divided into three parts:

1. List of parameters in;

2. Code between the colon ":" And curly braces.

3. Content in.

We can understand both 1 and 3. For the expression in 2, we call it the constructor Initial Value List. The function of this initial value list is to assign an initial value to one or more data members of the newly created object. The initial value of the constructor is a list of member names. Each name is followed by the initial values of Members enclosed. Different members are separated by commas (,) during initialization.

In addition, we noticed that the function bodies of the above two constructors are empty. This is because the only purpose of these constructors is to assign initial values to data members.

Next, let's look at the Code:

Sales_data: Sales_data (std: istream & is)


Read (is, * this); // The read function reads data from is and stores the data in

// This object


The above constructor is defined outside the class. The external definition is the same as the internal definition of the class. However, this constructor does not have a list of constructor values, or its list of initial values is empty.

Related Article

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.