Constructor with Default Arguments in C++

It is possible in C++ to define constructors with default arguments. For example, the constructor integer(), defined in the previous section (Constructors in C++), can be defined as follows:

integer(int x, int y = 3);

The default value of the argument y is 3. Thus, the statement:

integer a(1);

assigns the value 1 to x variable and 3 to y (by default).

However, the statement:

integer a(1, 5);

assigns 1 to x and 5 to y. The actual parameters, when specified, overrides the default value.

Note: The missing arguments must be the trailing ones.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *