Default parameters for C + + constructors
As with normal functions, the value of a parameter in a constructor can be passed either through an argument or some default value, that is, if the user does not specify an argument value, the compilation system makes the formal parameter default.
Cases
#include <iostream>
using namespace std;
Class Box
{public
:
box (int h=10,int w=10,int len=10);//Specify the default parameter
int volume ()
when declaring the constructor Private:
int height;
int width;
int length;
};
Box::box (int h,int w,int len)//You can define a function without specifying a default parameter
{
height=h;
Width=w;
Length=len;
}
int Box::volume ()
{return
(height*width*length);
}
int main ()
{
Box box1;//No arguments
cout<< "The volume of Box1 is" <<box1.volume () <<endl;< C27/>box Box2 (15); Given only one argument
cout<< "The volume of Box2 is" <<box2.volume () <<endl;
Box Box3 (15,30); Given only 2 arguments
cout<< "The volume of Box3 is" <<box3.volume () <<endl;
Box box4 (15,30,20); Given 3 arguments
cout<< "The volume of Box4 is" <<box4.volume () <<endl;
return 0;
}
The result of the program running is:
The volume of Box1 is 1000 the volume of Box2 are 1500 the volume of BOX3 is 4500 the volume of
box4 is
9000
The definition of a constructor (第12-16 line) in a program can also be written in the form of a parameter initialization table:
Box::box (int h,int w,int len): Height (h), Width (w), Length (len) {}
As you can see, it is convenient and efficient to use default parameters in a constructor, which provides a variety of choices when creating an object, which acts as a function of several overloaded constructors.
The advantage of this is that even when the constructor is called without providing an argument value, not only does it not go wrong, but it also ensures that the object is initialized with the default parameter value. This is especially convenient when you want to have the same initialization condition for each object.
A few notes on the constructor default values:
You should specify a default value when you declare a constructor, and you cannot specify a default value only when you define a constructor.
Line 5th of the program when declaring a constructor, the formal parameter name can be omitted.
If all the parameters of the constructor specify a default value, you can give one or more arguments or no arguments when you define the object.
After you define a constructor that is all default parameters in a class, you cannot define an overloaded constructor.
Overloading of C + + constructors
Multiple constructors can be defined in a class to provide different initialization methods for the user to choose from. These constructors have the same name, and the number of parameters or the type of parameter is not the same. This is called an overload of the constructor.
Use the following example to learn how to apply the overload of a constructor.
"Example" defines two constructors, one without parameters and one with parameters.
#include <iostream>
using namespace std;
Class box
{
public:box ();//Declare a parameterless constructor
//Declare a parameterized constructor, initialize the
Box (int h,int w,int len) to the data member with the initialization table of the parameter: Height (h), Width (w), Length (len) {}
int volume ();
Private:
int height;
int width;
int length;
};
Box::box ()//define a parameterless constructor
{
height=10; width=10; length=10;
}
int Box::volume () {return
(height*width*length);
}
int main ()
{
Box box1;//Create Object Box1, do not specify argument
cout<< "The volume of Box1 is" <<box1.volume () <& Lt;endl;
Box Box2 (15,30,25); Establishes an object Box2, specifying 3 arguments
cout<< "The volume of Box2 is" <<box2.volume () <<endl;
return 0;
}
There are two overloaded constructors defined in this program, and other overloaded constructors can be defined, and their prototype declarations can be:
Box::box (int h); constructor with 1 parameters
box::box (int h,int w);//constructor with two parameters
Given 1 parameters and 2 parameters, respectively, when creating an object.
A few notes on the overload of a constructor:
when calling a constructor, you do not have to give the constructor of the argument, known as the default constructor (defaulted constructor). Obviously, the parameterless constructor belongs to the default constructor. A class can have only one default constructor.
If you select an parameterless constructor when you create an object, you should be careful to write the statement that defines the object correctly.
Although you can include multiple constructors in a class, for each object, only one of the constructors is executed when the object is created, not every constructor is executed.