C + + Learning Note Constructor (a) default constructor

Source: Internet
Author: User

Definition: The constructor used to create an object when an explicit initial value is not provided.

Example:

Class TestClass

{

Private

int m_a;

int m_b;

Public

TestClass (); Default constructor 1

TestClass (int a,int b); constructor function

TestClass (int a=10,int b=5); Default constructor 2

};//Note that the actual use of a class in the default constructor 1 and the default constructor 2 can only appear 1, here just to illustrate

Summarize:

1) The default constructor is primarily used to complete the initialization of this form: TestClass A; In this case, if the programmer does not define a default constructor, the compiler will make an error.

2) If the programmer does not provide any constructors, then the compiler will automatically generate a default constructor.

3) There are two ways to define a default constructor, one of which is the default constructor 1 in the example above, a parameterless constructor, and the default constructor 2 in the example above, which defines a constructor where all formal parameters have default values.

4) A class can have only one default constructor.

5) If the programmer provides any form of constructor, then the compiler will no longer provide a default constructor.

C + + Learning Note Constructor (a) default constructor

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.