When you create an object, you often need to do some initialization work, such as assigning values to data members, and so on. To solve this problem, C + + provides a constructor function.
A constructor (Constructor) is a special member function whose name is the same as the class name, has no return value, does not require a user call (the user cannot call it), and is executed automatically when the object is created. The function of a constructor is to initialize the object when it is created, most commonly by assigning a value to the member variable.
An example of a constructor function:
#include <iostream>using namespacestd;classstudent{Private: Char*name; intAge ; floatscore; Public: //declaring ConstructorsStudent (Char*,int,float); //declaring ordinary member functions voidsay ();};//Defining ConstructorsStudent::student (Char*name1,intAge1,floatscore1) {Name=name1; Age=Age1; Score=Score1;}//defining ordinary member functionsvoidStudent::say () {cout<<name<<"The Age is"<<age<<", the results are"<<score<<Endl;}intMain () {//to create an object from a constructor functionStudent Stu ("Xiao Ming", the,90.5f);//the format of a parameter is similar to a function callStu.say (); return 0;}
In the class we define a constructor, Student (), whose function is to assign a value to a member variable of 3 private properties. In the main function, we create an object Stu based on the constructor, because the constructor has arguments, so the object is created with an argument that is similar to a function call.
Readers should note that once a constructor is defined in the class, the object is created and must be executed, and if the constructor has parameters, the arguments are passed when the object is created.
In addition, constructors are primarily used for initialization, there is no return value (there is no meaning to the return value), which means:
- The return value type cannot appear in front of the function name, either declaratively or as defined, even if void is not allowed;
- The function body cannot have a return statement.
Default constructor
If the user does not define the constructor itself, the compiler automatically generates a default constructor, except that the function body of the constructor is empty, has no parameters, and does nothing. For example, the Student class above, the constructor generated by default is as follows:
A class that must have a constructor, either defined by the user or generated automatically by the compiler. Once the user has defined the constructor itself, whether it is a public property or private or protected property, the compiler is no longer automatically generated. The Student class above has only one constructor, which we define ourselves.
Overloading of constructors
As with normal member functions, constructors are allowed to be overloaded. A class can provide multiple constructors that allow the user to make selections when creating an object, and the compiler determines which constructor to call based on the parameters passed when the object was created. Other words:
- Only one constructor will be executed;
- The arguments provided when creating the object must match one of the constructors, or compile the error.
#include <iostream>using namespacestd;classstudent{Private: Char*name; intAge ; floatscore; Public: //declaring ConstructorsStudent (); Student (Char*,int,float); //declaring ordinary member functions voidSetNameChar*); voidSetage (int); voidSetScore (float); voidsay ();};//Defining Constructorsstudent::student () {}student::student (Char*name1,intAge1,floatscore1) {Name=name1; Age=Age1; Score=Score1;}//defining ordinary member functionsvoidStudent::setname (Char*name1) {Name=name1;}voidStudent::setage (intage1) { Age=Age1;}voidStudent::setscore (floatscore1) {Score=Score1;}voidStudent::say () {cout<<name<<"The Age is"<<age<<", the results are"<<score<<Endl;}intMain () {//initializing member variables when creating an objectStudent STU1 ("Xiao Ming", the,90.5f); Stu1.say (); //call the member function to initialize the value of the member variableStudent STU2; Stu2.setname ("Li Lei"); Stu2.setage ( -); Stu2.setscore ( the); Stu2.say (); return 0;}
Two constructors are defined in the class, one with parameters without parameters, and they are overloaded relationships. When an object is created from a constructor that does not have parameters, no arguments are required and the member variables are not initialized, so the member function is called to set their values.
C + + Learning 7 constructors