Constructors and destructors for classes

Source: Internet
Author: User

The constructor is initialized automatically when the class object is created, not by the interface user invoking the function itself.

The destructor is for the final cleanup work when the class object expires. In summary, it is the function that is called when the object is destroyed.

Note: Constructors and destructors do not have return values and claim types

constructor function:

Declaration method:

1 class_name::class_name (...) {...}  // same as normal function but the function name must be the same as the class name

Calling method: Class_name class = Class_name (...);
Class_name class (...);

Use NEW:

Class_name * class = new class_name (...);

Default constructor: To create a constructor for an object without providing the initial value of the display, typically the compiler uses the default constructor by default when no declaration is made to define the constructor.

There are two ways to define this: 1. Provides default parameters for all parameters of an existing constructor.

2. Use a function overload to define another constructor ———— a constructor with no arguments. ( common )

Destructors:

Class_name::~class_name (...) {...};

Constructors and destructors for 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.