Constructor overloading

Suppose we have a 'Student' class and while making its object, we want to pass a name of it and if nothing is passed then the name should be "unknown". And yes! we can do this by having two constructors.

Now let's understand this example. Here, we made two objects of class 'Student'. While creating an object 'a', we passed a string "xyz" to the object as Student a( "xyz" );. This invoked the constructor having a string parameter Student( string n ).

Similarly, while creating a second object 'b' of the class Student, we didn't pass anything to the object 'b' as Student b;. So, the constructor having no parameter Student() got invoked and initialized the name with the value unknown.

Condition for constructor overloading

The one condition for constructor overloading is that both the constructors must have different parameters. Like in the above example, in the first constructor, we passed one String and in the second, nothing.

We can't make two constructors having exactly same arguments( e.g.- both having two ints ).