Technical Discussion :: C++
Home > Technical Interview Q&A > C++ > Discussion
80 / 187
What is default argument constructor?
Answer:
It is possible to define constructors with default arguments. for example, the constructor complex( ) can be declared as follows:
Complex(float real, float imag=0)
The default value of the argument imag is zero.then the statement
Complex c(5.0);
Assigns the value 5.0 to the real variable and 0.0 to imag (by default). However, the statement
Complex c(2.0,3.0);
Assigns 2.0 to real and 3.0 to imag. The actual parameter, when specified, overrides the default value. As pointed out earlier, the missing arguments must be the trailing ones.
Asked In ::
Padmaja S
23 Feb, 2019 5:07 AM
It is possible to define constructors with default arguments. for example, the constructor complex( ) can be declared as follows:
Complex(float real, float imag=0)
The default value of the argument imag is zero.then the statement
Complex c(5.0);
Assigns the value 5.0 to the real variable and 0.0 to imag (by default). However, the statement
Complex c(2.0,3.0);
Assigns 2.0 to real and 3.0 to imag. The actual parameter, when specified, overrides the default value. As pointed out earlier, the missing arguments must be the trailing ones.
Complex(float real, float imag=0)
The default value of the argument imag is zero.then the statement
Complex c(5.0);
Assigns the value 5.0 to the real variable and 0.0 to imag (by default). However, the statement
Complex c(2.0,3.0);
Assigns 2.0 to real and 3.0 to imag. The actual parameter, when specified, overrides the default value. As pointed out earlier, the missing arguments must be the trailing ones.