Hello everyone.
So today we will learn about constructors before starting today's video.
I want to cover two more things that I forgot to cover in the last review so the first thing is see
we have public private and then again public.
So this whole gold belongs to public discord belongs to a public so we can write we can write this code
in a better way in a better manner.
So how to optimize this code.
First of all let's remove and document from here and put it in the public section and let's remove this
line.
So now we write public only one time.
And similarly there is no need to write private way.
Let me show you.
See this age is private.
Why.
Because there is no access modifier defined above it so by default the access modifier is private.
So there is no need to write private.
And similarly in the previous code public was written two times.
But here public is right here we write public only one time.
Okay.
So this code is much better.
Earlier it was like public public and then private.
And then again public.
So this doesn't seem right to me.
This one is much better.
We are using we are using the properties what property first we are using their default property that
default is private.
So there is no need to write private and we are writing public on live ones.
Okay.
Second thing that I want to cover is how display function can access each and all no fee.
Rule number is public.
So rule number can be accessed anywhere in the program but ages private since age is private.
It can only be accessed within this class and display function is defined within this class.
So display function can access each similarly gated function can also access each because good its function
is also defined in the student class.
And similarly stage function can also access private variable each because this function is also defined
within the class.
So these are the two things that I want to look about in the last radio but I forward to color.
Okay.
Now let's start with I this topic.
So till now we have learned how to create classes how to create objects of a class and how to access
properties of the object and access modifiers.
Let's see what our construct does and how to use them.
So when we write student S-1 what will happen
rule number age and we get a garbage values in these fields.
Okay.
So if I will do s not display
these two will get printed.
Fine.
Now let's analyze this a statement.
Let's analyze this run properly what will happen.
So this internally in debates in this we as one art student
internally this statement will be executed in this fashion as one art student.
So what is the meaning of astronaut student.
So we can see here student is a function just like display student is a function just like the display
and S1 object is calling that function but this function name is same as last name.
The name of other classes is student and the name of the function is also student.
So the function name is same as last name.
So this function is called constructor this function is called constructor.
So what is the purpose of a constructor.
So it will do one simple thing.
It will initialize the data member or the properties with the garbage values.
Okay so properties and the name of properties are data members data member okay.
So what this constructively Lou it will initialize the data member or we can say the properties of the
object with the garbage values.
So that's why these two properties are initialized with the God which values because this is the work
of dysfunction so constructor function has some special properties.
So construct basis special function.
Now let us write some of its properties first its name will be seen misc last name so same name as the
name of the class second.
This function has no return type
third properties no input argument
no input argument.
So these are the properties of a constructor or we can say default constructor.
These are some of the properties of the default constructor.
Okay so let's analyze it a bit more so I am saying when we write student S1 this statement internally
will be interpreted as S1 dart Student object name dart function name.
So now the question is how we created this function.
No we have not created this a student function in our class but this function is being called and other
properties are initialized with their default values.
That is the god of basic values.
So what happened is.
So as soon as we create a class constructor is automatically created.
So we do not need to create it so we can use this inbuilt constructor to initialize the properties of
our object.
Okay so we get a default constructor as soon as we create a class we can also create constructor on
our own.
We will see in some time how to create our own constructor.
Okay so now let's see how the inbuilt constructor look like.
So I told you there are some properties first there will be no return type so empty and then the name
of the function will be say now name of the constructor will be same ethic last name.
So the class name is student.
So the name of the function is student no input argument.
And similarly it will larger than anything no return type no argument and same name as the class name.
In fact we can remove the remote.
Okay.
And also this.
Okay.
So this is the default constructor default constructor will look like this.
So what are the properties of default constructor.
Same name as the class name.
No input argument and no return type.
So as soon as we create an object this function or we can say this construct they will be called for
every object we create constructively record.
So what is the purpose of a constructor.
So the purpose of a constructor the work of a constructor is it initialized the object initialize the
object or we can see it initialize is the property of an object but the default values with regard to
basic values okay.
So for every object constructor will be called only once in the lifetime of the object so then it will
be called it will recall at the time of creation of the object.
Okay let's see some example then you will understand it better if I write student S1 then this function
will be called if I will right.
Student S2 then this function will be called even if I create dynamic object.
Student Start S3 is new student
then this function will be called.
So basically the constructor will be called only once in the lifetime of the object at the time of the
creation of the object.
Okay and how all this statement will be interpreted internally.
So this is same as S1 not student
and this is S2 dart student.
So internally these statements will be interpreted like this and how will the statement will be interpreted
internally.
Star S3 dart.
Function name.
Student or we can see S3 ATL student
Okay so now let's seeing called
Let's Make a constructor
let's make a default constructor
so the function name will be same as the class name last name is student.
So function name will also be student.
There will be no input argument.
And that's all.
So this is our default constructor.
Okay now internal constructor will look exactly like this.
Now one important thing is as soon as we create our own constructor the internal constructor is no longer
available to us.
So this is the constructor that we have created
so as soon as we create our own constructor and then construct that is no longer available to us.
So now if we really create object then I will constructively be called internal constructor will not
recall because it is not there.
Okay so I would constructively do the same book as the internal construct I was doing.
That is it will initialize every property of the object to a default value okay.
Now let's see
let's say some statements here a brand statement.
So we'll see out
Gestapo guard
okay.
Now let us create some object here.
So student S1 student S1 what will happen as soon as this object is created it will call the constructor
that we have created and Daniel construct does not do really well because we gave it our own constructed.
Now let's call the function display word display function will do it will print that you gender number
and since the agent that all number we have not set out oneself.
They both contain God which values similarly let's create another object student S2 and let's call the
display function.
Now let's create a dynamic object so slowly start history as new student
and let's call the display function.
OK so what will we do.
So if I had run this file what will be out I would put first.
I created this object.
So what will happen constructively recalled.
So output will be constructor called and then display function will print the agent that will number
both out of garbage.
So garbage value garbage value then then the concept will be called for object as to so output will
be constructor called
and then it will print the garbage will lose each and the roll number both out of garbage.
And similarly for this concept called and garbage value of each garbage value overall number.
So this is I would expect it output.
Now let's see where do we add output.
So first we have to run this file.
Now we will run this file.
Now you can see the output concept called for object S1.
Then the garbage values of a general lumber concept called for object as to garbage value of a general
lumber concept called for Dynamic object as tree and the garbage values of a general number.
Okay.
So whenever we create objects and build constructive will be called.
But if we create our own concept then our constructed will be called and internal constructor will vanish
and mental construct that will go away as soon as.
Because as soon as we create our own constructor Okay so we can create many constructors for example.
This is our default sector let's create a bad image based constructor.
So betterment based constructor.
So as the name suggests this construct that we have big arguments.
This constructor will take barometers as input.
So function name will be seen as the class name which is the student.
And let's say take input.
Rule number and what it does is it will be assigned all number with a better metered
and let's say 8 brand statement.
So see out constructor to
constructor to guard
so
so if you will create.
So if we will create object with no parameters then this function will be called this constructively
recall.
And if we will create our object with one barometer then this function will be called.
Okay let's take an example to understand it better.
So if I will write student s for what will happen the default constructor will be called because there
are no barometers but if I write if I pass a bad amateur student s Food a barometer then now what will
happen bad image tries to construct it will be called
See when I write student s for it will be interpreted as S for dot student
and this student is the default constructor that we have defined a default constructor.
But I want to call betterment based constructor so for betterment based constructor I have to pass one
barometer which is when we have 2 percent in digital barometer.
So this land will be interpreted as as for dot student and Ben.
Okay.
Now this time betterment tries to construct a will be called okay.
So this is the concept of functional learning.
We have covered function overloading earlier this is seem this is same as the concept of functional
learning.
We have two functions but one function doesn't take any input any barometer as input.
And the second function take one digit argument as input.
So these two are different
so as for our display
now lives in our program OK.
So we have to run this file first and now we will run this file
see constructor to call for object.
As for constructor to is being adding Guard and as photo display print age and that remember so rule
number is Ben because we set it and ages.
I've got a visual view okay.
Let's see you again see out
betterment days to construct our demo.
Now let's run this fighting
see Batman based constructor demo constructor to call because it passed one input one integer argument
as input and rule number is ages a garbage value at all but it's ten by ten because we set rule number
with.
Okay
similarly we can create dynamic objects as well so student star S5 is new student and we have to pass
an integer argument so 20 as faith not display
okay sorry I say federal display
so constructor to call the value of age as a garbage value and the value for number is 20
so
so for one object only one constructively regarded it will never happen that default and betterment
based construct both are being getting called no if there is one object only one construct a little
bit called only at the time of the creation of the object constructively recalled only at the time of
the creation of the object.
Okay so this is very important for every object when constructed will be called only at the time of
the creation of the object.
Okay so we will learn more about constructor in the next video.
Thank you.