This video introduces the concept of constructors in object-oriented programming, demonstrating their practical application in simplifying object creation and initialization within a bank account management system.
Mind Map
Clicca per espandere
Clicca per esplorare la mappa mentale interattiva completa
hi guys and welcome to my channel in
this video you are going to learn about
Constructors in objectoriented
programming and I will show you on a
practical example how Constructors are
used and why we need them this is the
second video in a playlist where I teach
you about practical objectoriented
programming and in the first video we
have built this application that you can
see on the screen if you did not watch
that first video I'm going to link it
here and also I will link in the
description the entire playlist where
you can learn about practical
objectoriented programming so first
let's explain the application that we
have built in the previous video and
that I have here and then we are going
to continue building this application
together so here we have an application
for managing bank accounts and if I
start this application you will see that
here inside this grid we have three bank
accounts and all those three bank
accounts have owner account number and
balance in the previous video we learned
what are classes and what are objects
and we have also created a class called
bank account and then we have created
three objects of that class that is this
bank account this bank account and then
this bank account here so now I will
show you how that looks like in code so
here is our bank account class and as
you can see it has three properties
owner account number and balance and
then in the code behind of this form we
have created three objects of that class
so let's open code behind simply right
click on the form and click on view code
okay so here we are creating first
object and then we are setting its owner
account number and balance and then here
we are creating second bank account and
then third bank account and then here
I'm creating an empty list of bank
accounts I'm adding all those three bank
accounts in the list and then I simply
set that list as the data source for my
grid so when I start the application you
can see these three bank accounts inside
our grid so that is what we have done in
the previous video and then in this
video we are going to learn about
Constructors and we will also Implement
these three functionalities here so that
we can create new bank account that will
be added to this grid and also deposit
and withdraw money from existing bank
accounts so our goal for this chapter is
we don't want to hard code these three
bank accounts like we have currently we
want to be able to create them
programmatically and then deposit and
withdraw money from those bank accounts
hey there if your goal is to learn
programming and start a successful
career build interesting and useful
applications and if you enjoy learning
from my YouTube videos then you should
definitely enroll in my practical
programming course I will take you from
beginner to expert in just a few months
and I will give you all of the support
and help that you need to achieve your
goals in the course you will work on
realistic projects you will get access
to a large community of successful
developers and everything that you need
to learn will be on one place you just
need to follow the plan that I have
created for you and those of you who
finish the course successfully will get
a special certificate that is going to
open a lot of job opportunities for you
in the description you can find a coupon
code to save some money and if you're
not sure if this course is for you or
not we have 7 days money back guarantee
which means that if you don't like it
for any reason you will get all of your
money back no questions asked so now
instead of coming up with excuses use
this opportunity to finally learn
programming and transform your life
because you really have nothing to lose
and a lot to gain so as I said our goal
for this video is to be able to create
these bank accounts programmatically so
the user enters name of the owner and
then when he clicks on create account
then account should be added into this
grid and then also we want to implement
these two functionalities so that we can
deposit and withdraw money from existing
bank accounts and that is what we are
going to do together in this video and
if you want to download my project so
that you have the same application that
I have here and so that you can continue
building it together with me I will
leave a link in the description to my
GitHub repository and from there you can
download this application and you can
code along with me me and Implement all
of these functionalities please just
keep in mind that in order to be able to
work on this application you need to
install two things you need to have
Visual Studio 2022 or newer version and
the second thing is you need to install
net 8.0 framework because those are two
things that I have used in order to
create this application and if you don't
have those two again I will leave a link
in the description of this video that
you can use to download them and install
them and the in ation process is very
easy mostly next next finish and then
they are completely free for students so
let's first explain what are
Constructors you can understand a
Constructor as a special method that is
invoked when you create an object of a
class and the job of Constructor is to
create or to construct that object so
basically Constructors are used to set
the values for the properties of the
object that you are creating so when you
first create an object its Constructor
will be invoked and inside Constructor
you can set the initial values for the
properties of that object so this is the
definition and now let's see how this
works in practice because that way it
will be much easier for you to
understand and to learn what are
Constructors and what are the benefits
of using Constructors so I will stop my
application and I will open my bank
account account class and I want to
create the Constructor for this class so
now let's see how we can do that now
there are a few rules when you are
creating a Constructor and the first
rule is that you should create a
Constructor inside the class that it
belongs to so if we are creating
Constructor for bank account class then
we need to create it inside these curly
brackets of our bank account class that
is the first rule the second rule is
that Constructor always needs to be
public so I will say public and there
are some exceptions to this rule you can
learn more about those exceptions in my
practical programming course in the code
Beauty Academy because those are some
more advanced topics for now let's just
say that Constructors always need to be
public okay that is the second rule the
third rule is that Constructor does not
have a return type and then the fourth
rule is that the name of the Constructor
is the same as the name of the class
that it belongs to so by doing this I
have created a Constructor now this
Constructor is currently empty but as I
explained the Constructor will be
invoked every time that you create an
object of bank account class which means
that here we can write the code that
will be executed every time that we
create a new bank account and before we
start writing the code and changing this
empty default Constructor let's first
explain what we want to achieve so let's
go to the code behind of our form simply
right click on the form and click on
view code and here you will notice that
we are creating bank account objects
this is the first second and third bank
account that we have created and you can
already notice that it is taking a lot
of lines of code to create these three
objects so now the question is is there
an easier and better way to do this same
thing it would be very cool if we could
do something like this so instead of
creating new bank account that is empty
and then assigning owner account number
and balance we would like to do
something like this I'm going to comment
these three lines of code and and here
inside these parentheses when I'm
creating my bank account I will pass
this owner name like this and then I
will pass this go like this and then
instead of passing this as a balance
let's say that initially whenever you
create a new bank account its balance is
going to be zero and then if we want to
create another bank account again I will
not need these three lines of code
instead I will pass here everything that
is necessary to construct that bank
account so owner's name again and then I
will pass this new guid like this and
then as I said balance initially when
you are creating an account will be zero
like this and we will do the same when
we are creating this third bank account
so I will comment out this entire code
and then here when I'm constructing that
object I will pass owner's name and then
good which will be the same as in the
previous two and then balance will be
initially zero like this so my goal is
to delete this code that I have
commented and then when I'm creating my
objects to use just this code here that
remains so now the question is can we do
this and how can we make our code to
work because currently we have these red
squiggles which means that we have
errors in our code well let's explain
what is happening here so when you are
creating bank account object here will
be invoked the Constructor because as we
said Constructor is used in order to
create or to construct that object so
here what we are doing instead of having
empty parentheses is we are passing some
parameters to our Constructor and those
parameters are the values or the
information that the Constructor needs
in order to construct that object in our
case we are passing owner's name and
then good and then balance and we are
doing that for all of the bank account
objects that we are creating and the
reason why we have this red squiggly
this error is because we don't have any
Constructors that correspond to these
parameters here so we don't have any
Constructor that receive these three
parameters and then based on these three
parameters they construct the object so
in order to fix these red squigglies we
would have to create a Constructor that
receives owner's name new guid and then
balance and then it creates an object
based on these three informations but
before we do that let's see if we can
make this code even shorter please
notice that when we are creating these
three Bank account initially the only
thing that is different is owner's name
so this account is mine and then this
one is from Elon Musk and this one is
from Bill Gates these remaining two
things so good and balance the way that
we are creating those is the same so
here we are generating new good which
means new account number and we are
passing that as a parameter to our
Constructor and then here as well we are
creating new guid new account number and
again we are passing it as a parameter
and we are doing absolutely the same
thing for this third bank account which
means that we don't need to pass this as
a parameter to our Constructor we can
simply request a new guid in the
Constructor itself and then for this
third parameter which is balance we said
that initially each bank account that is
created it's balance will be zero so
again we don't need to pass this as a
parameter to our Constructor we can
simply do it in the Constructor for each
bank account that we create so that
means that we can remove these two
parameters from our Constructor like
this and the only thing that we need to
leave is owner's name so I will remove
it from here as well and then from here
also so when we are creating new bank
account the only thing that we need to
pass to the Constructor is the name of
the owner so so now instead of creating
a Constructor that has three parameters
we can create a Constructor that has
just one parameter and that is owner so
now let's do that let's go to our bank
account class and here let's say that
our Constructor will receive one
parameter it will be of type string and
it will be called owner with lower case
letters okay so here inside these curly
brackets of my Constructor what I will
do is I will say that this property
owner property of my bank account will
be equal to this parameter that I have
received so whoever is creating new bank
account object he needs to pass owner as
the parameter to the Constructor and
then here we will construct that object
based on the value that we have received
as the parameter that is going to be the
name of the owner and then for these two
remaining properties account number and
balance we already know how to
initialize those so for account
number I will say that it is equal to
new guid like this so for each bank
account please generate new good and
then for balance we know that initially
when you create a new bank account its
balance is going to be zero so with this
we have successfully created our
Constructor it receives one string
parameter that is owner of the bank
account and here inside these curly
brackets of the Constructor we are
setting initial values for the
properties of that object so the owner
of the bank account will be the value
that we receive here and then account
number will be new guid and then balance
of that bank account initially will be
zero and this Constructor will be
invoked for each new bank account object
that that we create and if we go now to
the code behind of our form now you can
see that the red squiggly that we had
here has disappeared and currently we
don't have any errors so now we are able
to use this code which is a lot shorter
in order to create objects of bank
account class so when we are creating a
new bank account here it will invoke its
Constructor and this will be passed as
the OWN for the bank account and then in
the background Constructor will also set
the account number so it will generate
new good for the account number and also
it will set the balance for that account
to zero and if I start the application
as you can see we will get the same
behavior that we previously had but now
the code is a lot shorter and even if I
want to add a new bank account I will
simply copy this line of code I will
create bank account for and here I will
say that the owner is going to be let's
say Mark Zuckerberg like this and then
we will simply add that forth bank
account to our list and that list will
be set as the data source for our grid
so now when I start the application
again as you can see here is our forth
bank account so in this video you
learned what are Constructors and how to
use Constructors in order to easily
create objects of a class class and here
we have four objects that we have
created with the help of our Constructor
now there are a lot more things that we
want to do in this application for
example something that I currently don't
like is that these four objects are
hardcoded in our code and what I want to
be able to do instead is I want to be
able to create new bank account by
clicking on this button and then also I
want to implement these two
functionalities so that I can deposit
and withdraw money from my bank accounts
but since I know that you don't like
videos that are too long let's do that
in the next video of this playlist which
I will link here so in the next video we
are going to implement these three
functionalities so that we can
programmatically add new bank account
and then so that we can deposit or
withdraw money from the selected bank
account so if you have any questions
please leave those in the comment
section and if you enjoyed this video
give it a big thumbs up so that I know
to create more videos like this one and
if your goal is to learn practical
programming so that you can start a
successful career and build useful and
interesting applications and if you
enjoy learning from my YouTube channel
then you should definitely enroll in my
practical programming course because
there I will take you from beginner to
expert in just a few months and you will
get all of the support and help from me
that you need in order to achieve your
goals in the course we are working on
realistic projects so you will get real
experience that you need I will also
introduce you to a community of
successful developers that you can learn
from and collaborate with and everything
that you need will be on one place so
your job is just to follow the plan that
I have created and that plan will take
you to your first job as a software
developer so if you decide to join in
the description I will leave a special
coupon code that you can use in order to
save some money and if you're not sure
if the course is for you or not you can
definitely try it out for 7 days because
we have a 7-Day money back guarantee
which means that if you don't like it
for any reason you can get all of your
money back no questions asked from our
side so you really have nothing to lose
and learning to code can absolutely
change your life so with that being said
thank you very much for watching and I
Clicca su qualsiasi testo o timestamp per andare direttamente a quel momento del video
Condividi:
La maggior parte delle trascrizioni è pronta in meno di 5 secondi
Copia in un clicOltre 125 lingueCerca nel contenutoVai ai timestamp
Incolla l'URL di YouTube
Inserisci il link di qualsiasi video YouTube per ottenere la trascrizione completa
Modulo di estrazione trascrizione
La maggior parte delle trascrizioni è pronta in meno di 5 secondi
Installa la nostra estensione per Chrome
Ottieni le trascrizioni all'istante senza uscire da YouTube. Installa la nostra estensione per Chrome e accedi con un clic alla trascrizione di qualsiasi video direttamente dalla pagina di riproduzione.