About C + + constructors

Source: Internet
Author: User

1 constructors are special member functions that are generally declared to be public
2 Create a new object of class type, the system automatically calls the constructor, and is called only once, does not allow manual invocation
3 constructor is to ensure that every data member of an object is correctly initialized
The function name is exactly the same as the class name
You cannot define the type of a constructor (and you cannot have a return type), or you cannot use void
Normally a constructor should be declared as a public function, otherwise it cannot be explicitly called like any other member function
Constructors are declared private for special purposes.
Constructors can have any type and any number of arguments, a class can have multiple constructors (overloads)
The name of the constructor parameter and the name of the member in the class cannot be defined as the same

A summary of the destructor:

1 function names are similar to class names (one more character "~" is preceded)
2 No return type
3 No parameters
4 destructors cannot be overloaded
5 if no destructor is defined, the compiler automatically generates a default destructor, in the following format:
Class Name:: ~ Default destructor name ()
{
}
6 The default destructor is an empty function.

1 Example 1:2 classTest3 {4  Public:5     //if the class does not provide any one constructor, the system will provide us with a non-parametric6     //the default constructor7 Test ();8 Private:9     intnum_;Ten }; One  A //constructors without parameters are called default constructors - test::test () - { theNum_ =0; -cout<<"Initializing Default"<<Endl; -}

1 Example 2:2 classTest3 {4  Public:5     //if the class does not provide any one constructor, the system will provide us with a non-parametric6     //the default constructor7     //Test ();8Test (intnum);9     voidDisplay ();Ten  One Private: A     intnum_; - }; -  theTest::test (intnum) - { -Num_ =num; -cout<<"Initializing"<<num_<<Endl; + } -  + voidTest::D isplay () A { atcout<<"num="<<num_<<Endl; - } -  - intMainvoid) - { -Test T;//This means that you want to invoke 1 constructors without parameters, and we have built a constructor with parameters, and the system will not give us 1 constructors without arguments . in T.display (); -     return 0; to } + //this will cause an error because we built a constructor with parameters, and the system doesn't give us a constructor . - //If this is the case, there will be no error . the intMainvoid) * { $Test T (Ten);//Create a new objectPanax Notoginseng T.display (); -     return 0; the}
1 Example 3:2 classTest3 {4  Public:5     6 Test ();7Test (intNUM);//these two constructors make up the overloaded8     voidDisplay ();9~test ();//Destructors cannot be overloaded, which means that destructors cannot have parametersTen Private: One     intnum_; A }; -  - test::test () the { -Num_ =0; -cout<<"Initializing Default"<<Endl; - } +  -Test::test (intnum) + { ANum_ =num; atcout<<"Initializing"<<num_<<endl;//The phrase is to output initializing and num_, not just output initializing - } -  -test::~Test () - { -cout<<"Destroy"<<num_<<Endl; in } -  to voidTest::D isplay () + { -cout<<"num="<<num_<<Endl; the } *  $ intMainvoid)Panax Notoginseng { - Test t; the T.display (); +  ATest T2 (Ten); the T2. Display (); +     return 0; -}

About C + + constructors

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.