C ++ object-oriented Review (1) -- Constructor

Source: Internet
Author: User

Constructor

In the C ++ class, when the system allocates memory, it only allocates memory to the data.Code, And other characters are not allocated.

Data members in a class are not allowed to be initialized when the class is declared. If all the members in the class are public, you can initialize the data members when defining the object.

C ++ provides constructors for object initialization.

Constructor features:

1. constructor is a special member function that is automatically executed when a class is created instead of being called by the user.

2. the constructor name must be the same as the class name. It does not have any type and cannot return values.

Class names and domain delimiters must be added to the class constructors of the three out-of-class definitions ::

4. The function body of the constructor can not only assign initial values to data members, but also include other statements. The constructor is generally declared as public.

5 if the user does not define the constructor, the C ++ system will automatically generate a constructor, except that the function body is empty and no operation is performed.

6. constructor with Parameters

A constructor without parameters can only initialize a group defined in the function. You cannot customize the group by yourself. A constructor with parameters is provided in C ++, it is used to initialize various data at the same time as the definition class.

Pattern when defining a constructor: constructor name (type 1 parameter 1, type 2 parameter 2 .....)

For a class that defines a constructor with parameters: Class Name (real parameter 1, real parameter 2 ...)

7. Box: :( int H, int W, int Len): height (H), width (W), lenth (LEN ){}

This is another method for initializing data members.) -- parameter initialization method

Add a colon to the end of the function and list the initialization list. Because of its simplicity and popularity

8. overload of Constructors

When calling a constructor, you do not have to give a real parameter constructor as the default constructor. A class can have only one default constructor.

For example, if a class contains box (); and box (Int = 10, Int = 10, Int = 10), an error is returned.

9. A class can contain multiple constructors. But for each object, only one constructor is executed, not all constructors.

10. Generally, the overload of the constructor and the constructor with default parameters are not used at the same time.

 

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.