Default constructor with all parameters given default values?

308 views Asked by At

I was reading the book by Joyce Farell on C++, and it's saying that a default constructor is the one with no arguments. I understood it without any hiccups. But things started getting worse after it says that if you set default values to all arguments in the constructor it still is called default constructor.

But I think it is still parameterized constructor as we can override the values by passing arguments when calling the constructor.

Here's the code:

Default or parameterized constructor

Now, what shall I call this constructor? When creating the employee e1 object, it is acting as the default constructor. But at the same time, when creating the employee e2 object, the same constructor is acting as a parameterized constructor.

But the constructor is the same - hence it should be either default or parameterized, not both at the same time.

I don't think you can call it constructor overloading, as you are calling the same constructor, not different ones.

I am actually trying to clarify my concept. There is no any error in my code.

1

There are 1 answers

0
François Andrieux On

From https://en.cppreference.com/w/cpp/language/default_constructor

A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter).

A constructor which doesn't require arguments is a default constructor. This includes a constructor with parameters if all of the parameters have default values. Such a default constructor can also optionally be called with arguments. Being able to be called such a constructor with arguments doesn't make it any less of a default constructor because the arguments aren't required.

C++ doesn't recognize the notion of "parameterized constructor". If you mean a constructor with parameters, then the notion is not exclusive with default constructor. By that definition a constructor can be both parameterized and a default constructor.