The constructor is a very important concept in c+, and here is a brief summary of its knowledge.
Definition of a constructor function
1. the constructor name in the class must be the same as the class name
2. constructors do not return class-valued specifiers with functions
[Special attention]:
A. The return value type of the constructor is not void, but is not
B. The constructor does not return a value type specifier, but the constructor has a return value, and the return value of the constructor is the object that it creates
1 class student{2public:3 Student () {} // the function name of the constructor must be the same as the class name 4 // constructor does not return a value type descriptor 5 ..... // Other Operations 6 }
3. When the program is running, when a new object is established, the constructor of the class to which the object belongs is automatically called and is called only this time during the lifetime of the object
4. constructors can be overloaded. in a class, you can define multiple constructors, which are distinguished by different parameter lists , which are selected as rules for general function overloading when called automatically.
1 classstudent{2 Public:3Student () {}//Constructor 14Student (stringname);//Constructor 25Student (stringNameintAge);//Constructor 36Student (stringNameintAgestringgender);//Constructor 47 Private:8 stringName;9 intAge ;Ten stringGender; One } A - intMain () { -Student stu1;//Call Constructor 1 theStudent STU2 ("tomwenxing");//Call Constructor 2 -Student STU3 ("tomwenxing", at);//Call Constructor 3 -Student Stu4 ("tomwenxing", at,"male");//Call Constructor 4 - return 0; +}
5. constructors can be defined in a class, or they can be defined outside of a class
1#include <iostream>2#include <string>3 using namespacestd;4 classstudent{5 Public:6 Student () {}7Student (stringName) {//defining constructors within a class8 This->name=name;9 }TenStudent (stringNameintAge);//declare constructors within a class, define constructors outside the class One Private: A stringname; - intAge ; - }; the -Student::student (stringNameintAge) {//Out-of- class definition constructors - This->name=name; - This->age=Age ; + } - + intMain () { A Student stu1; atStudent STU2 ("tomwenxing"); -Student STU3 ("tomwenxing", at); - return 0; -}
6. If no constructor is defined in the class definition, the C + + compiler will automatically give a default constructor (called the default constructor):
Default constructor: Class name () {}
However, as long as we manually define a constructor, the system will no longer automatically generate default constructors for us, and if you still want the default constructors in your class, you need to manually define them by yourself .
[Special attention]:
The C + + compiler considers the constructor to be the default as long as the constructor is non-parametric or as long as each parameter has a default value, so the default constructor can have at most one of the class definitions
1#include <iostream>2 using namespacestd;3 classstudent{4 Public:5Student () {}//Constructor 16Student (intValue=0){//Constructor 27 This->value=value;8 } 9Student (intValue=0,intValue2=0){//Constructor 3Ten This->value=value; One This->value2=value2; A } - Private: - intvalue; the intvalue2; - }; - intMain () { -Student stu1;//Error: I don't know which constructor to call + return 0; -}
7. constructor Initialization list: is responsible for assigning an initial value to one or more data members of the newly created object. where the constructor initial value is a list of member names followed by the initial values of the member (or in curly braces) that are enclosed in parentheses, and the initialization of the different members is separated by commas.
1#include <iostream>2 using namespacestd;3 classstudent{4 Public:5Student () {}//To manually define a default constructor6Student (intvalue1): Value (value1) {}//constructor Initialization list7Student (intValue1,intvalue2); 8 Private:9 intValue;Ten intValue2; One }; AStudent::student (intValue1,intvalue2): Value (value1), Value2 (value2) {} - intMain () { -Student STU1 (Ten); theStudent STU2 (Ten, -); - return 0; -}
C + +: constructors-generic constructors