In this video we're going to look at default constructors.
And a default constructor is a constructor without any parameters.
That is just there for default cases where you set values as default values.
So let's go ahead and create a default constructor.
Therefore we're going to use the QR code snippet CTR press tab.
And then we get our beautiful default constructor.
So this is a default constructor okay.
Now the default constructor here we could for example just say okay.
So whenever we create a customer where we don't pass any arguments to the constructor to the new keyword
here, or after the new keyword here.
When we don't pass any details in here, then we want to use default values.
So then we could just say okay, then the name will be default name.
Then the address could be something like no address.
And then the contact number could be something like no contact number.
We could also say no name for the default name or whatever.
But yeah.
So that is something you can define in a default constructor.
Or we could call this one new customer something like that.
No address or unknown would also be an option.
So you see multiple different ways how you could define it.
So that's something you have to decide for yourself.
However you want to name your default values here.
And then when we go ahead and create a new customer now.
So customer my customer is going to be a new customer with no arguments given.
So here I'm creating a default customer.
So default customer with no arguments given okay.
So now let's go ahead and use CW to say details about customer.
And then we're going to say plus my customer dot name for example we could go ahead and add more like
the address and so forth.
But let's just use the name here and let's get rid of this line here or comment it out where we're talking
about my good friend URL.
My name is URL.
So details about customer new customer.
So maybe we should add a colon here.
But yeah basically that's it.
That's how you can create a default constructor.
And that's how you create an object using the default constructor where you pass no arguments whatsoever.
So when would that be useful?
The thing is this could be useful, for example for an admin back end where a customer is created and
then the details about the customer are added manually after the fact.
So we could now say my customer and now we know the name of the person.
So we can just say C double or console dot read line.
And we could say something like please enter the customer's name.
So here I'm going to comment this line out as well.
Please enter a customer's name.
And I could say Jimbo.
And then our customer's name of my customer of this one object would be Jimbo.
Okay, because I defined it.
So now let's actually reuse this line.
So I'm going to say Jimbo and say it says details about customer Jimbo.
So while you could say okay, well in this code you could have gone ahead and just used the customer
name as a separate variable before that and then passed it to that new customer when creating an instance
of it.
Yeah, but this is just because I have full control at this point, right now because I am the admin
writing the code.
But in a real world scenario, it could very well be that you create a new customer entry in your database,
but you don't have any details about that customer, and you want to set default values first, and
then you can overwrite them later on.
And that's what we did here.
So we created one with default values.
And then we overrode one of the values.
And now we could obviously go ahead and overwrite more of the values in the back end user interface
later on as an admin.
Okay.
So this is just an example.
There are plenty more examples where this would make sense, but I hope this gives you a good idea as
to how you can work around it.
Okay, so now that you've seen default constructors and using multiple custom constructors, you basically
know how to create instances of classes with different initial states providing more flexibility for
the user when working with your software.
And.
This could be, for example, when you are when a user creates an account, and then there are optional
fields that the user doesn't have to enter.
So we could say one field is a must field.
For example, the user has to create or enter a name, but the user doesn't have to enter any additional
information about themselves, like phone number or anything like that.
And then you would always get the name, but all the other stuff you wouldn't get, and there you could
set it as default values or whatever you want to do.
Good.
So that's it for this video.
See you in the next one.