C + + study notes (13): constructors

Source: Internet
Author: User

constructor Function

1. What is a constructor function

Each class defines how its objects are initialized, and the class controls the initialization of its objects by one or several special member functions, the constructor.

the task of the constructor is to initialize the data members of the object , and the constructor is executed whenever the object of the class is created.

2. Similarities and differences between constructors and other functions

In the same place:

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

② A class can contain more than one constructor, which is the same as an overloaded function

Differences:

The ① constructor has the same name as the class name, while the other functions do not.

The ② constructor does not have a return type.

The ③ constructor cannot be declared as Const.

3. Default constructor

Sometimes we do not provide an initial value for the objects in the class, but their objects are definitely initialized, and in fact they perform the default initialization process. class controls the default initialization process through a special constructor called the default constructor . default enough to create a function without any arguments .

If our class does not have a definition constructor displayed, then the compiler implicitly defines a default constructor- the default constructor for the composition.

In general, for any class, you need to define its own default constructor, because there are times when some classes cannot rely on the composition default constructor for a reason of three points:

The ① compiler generates a default constructor for us only if it discovers that the class does not contain any constructors. Once some other constructors are defined, there will be no default constructors unless we define a default constructor.

② for some classes, the synthesized default constructor might perform an incorrect operation. For example, in a class where arrays or pointer types are initialized by default, their values are undefined.

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

4. Defining constructors

Start with the following example:

struct sales_data{

Sales_data () = default; Take the default constructor

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

Bookno (s), Units_sold (n), Revenue (P*n) {}

Constructor Initial Value List

};


In the above program section,

Sales_data () = default;

This constructor does not accept any formal parameters, so he is a default constructor. The significance of defining this constructor here is that we need both a different form of constructor and a default constructor. The function here is exactly the same as using the composition's default constructor.

In the c++11 new standard, if we need the default behavior, you can ask the compiler to generate a constructor by writing a =default after the argument list.

Then look at this procedure:

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

Bookno (s), Units_sold (n), Revenue (P*n) {}

There are new expressions and new elements in this definition. We divide the above statement into three parts:

1, "()" in the formal parameter list;

2, the colon ":" and Braces "{}" between the code.

3. Contents of "{}".

For 1 and 3 we all understand that for the new expression in 2, we call it--a list of constructors ' initial values . The function of this initializer list is to assign an initial value to one or several data members of the newly created object. The constructor initial value is a list of member names followed by the initial values of the members enclosed in "()". Different member initializations are separated by commas.

Also, we notice that the function bodies of the two constructors above are empty. This is because the only purpose of these constructors is to assign an initial value to a data member.

Let's look at a piece of code below:

Sales_data:sales_data (Std::istream &is)

{

Read (is, *this); The function of the read function is to read the data from IS and store it in the

The This object

}

The above constructor is defined outside the class. The method is the same as defined externally and within the class. Just this constructor does not have a constructor initializer list, or its initial value list is empty.

C + + study notes (13): constructors

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.