The constructors that can take arguments are called parameterized constructors. When a constructor is parameterized, we must pass arguments to the constructor.
When a constructor is parameterized, the object declaration without parameter may not work. We must pass the initial values as arguments to the constructor.
This can be done in two ways:
1. By implicit call - The implicit call is implemented as follows:
student mk (1200,19); /””implicit call*/
This method is also called the shorthand method, and is used very often as it is shorter, looks better and easy to implement. In the above example, student is a class name and mk is name of object and passed arguments are 1200, 19. One can notice that function name is not taken to invoke constructor.
2. By Explicit Call - The following statement illustrates the explicit call for the parameterized constructor
Student dushyanth = Student (1201, 20); /””explicit call*/
In the above example, Student is class name and dushyanth is object name.
After the = symbol, the name Student refers to parameterized constructor with argument
1201,20.
The parameterized constructor syntax: