0:01 hi guys and welcome to my channel in
0:03 this video you are going to learn about
0:05 Constructors in objectoriented
0:07 programming and I will show you on a
0:10 practical example how Constructors are
0:12 used and why we need them this is the
0:15 second video in a playlist where I teach
0:17 you about practical objectoriented
0:19 programming and in the first video we
0:21 have built this application that you can
0:23 see on the screen if you did not watch
0:25 that first video I'm going to link it
0:27 here and also I will link in the
0:29 description the entire playlist where
0:31 you can learn about practical
0:33 objectoriented programming so first
0:35 let's explain the application that we
0:37 have built in the previous video and
0:39 that I have here and then we are going
0:41 to continue building this application
0:43 together so here we have an application
0:46 for managing bank accounts and if I
0:49 start this application you will see that
0:52 here inside this grid we have three bank
0:54 accounts and all those three bank
0:57 accounts have owner account number and
0:59 balance in the previous video we learned
1:02 what are classes and what are objects
1:05 and we have also created a class called
1:07 bank account and then we have created
1:10 three objects of that class that is this
1:13 bank account this bank account and then
1:15 this bank account here so now I will
1:18 show you how that looks like in code so
1:21 here is our bank account class and as
1:24 you can see it has three properties
1:27 owner account number and balance and
1:30 then in the code behind of this form we
1:33 have created three objects of that class
1:35 so let's open code behind simply right
1:38 click on the form and click on view code
1:41 okay so here we are creating first
1:43 object and then we are setting its owner
1:46 account number and balance and then here
1:48 we are creating second bank account and
1:50 then third bank account and then here
1:52 I'm creating an empty list of bank
1:55 accounts I'm adding all those three bank
1:58 accounts in the list and then I simply
2:01 set that list as the data source for my
2:04 grid so when I start the application you
2:07 can see these three bank accounts inside
2:09 our grid so that is what we have done in
2:11 the previous video and then in this
2:13 video we are going to learn about
2:16 Constructors and we will also Implement
2:18 these three functionalities here so that
2:20 we can create new bank account that will
2:23 be added to this grid and also deposit
2:26 and withdraw money from existing bank
2:29 accounts so our goal for this chapter is
2:31 we don't want to hard code these three
2:33 bank accounts like we have currently we
2:35 want to be able to create them
2:38 programmatically and then deposit and
2:40 withdraw money from those bank accounts
2:42 hey there if your goal is to learn
2:44 programming and start a successful
2:46 career build interesting and useful
2:48 applications and if you enjoy learning
2:50 from my YouTube videos then you should
2:52 definitely enroll in my practical
2:54 programming course I will take you from
2:57 beginner to expert in just a few months
2:59 and I will give you all of the support
3:01 and help that you need to achieve your
3:03 goals in the course you will work on
3:05 realistic projects you will get access
3:07 to a large community of successful
3:09 developers and everything that you need
3:12 to learn will be on one place you just
3:14 need to follow the plan that I have
3:16 created for you and those of you who
3:18 finish the course successfully will get
3:19 a special certificate that is going to
3:22 open a lot of job opportunities for you
3:24 in the description you can find a coupon
3:26 code to save some money and if you're
3:28 not sure if this course is for you or
3:30 not we have 7 days money back guarantee
3:32 which means that if you don't like it
3:35 for any reason you will get all of your
3:37 money back no questions asked so now
3:39 instead of coming up with excuses use
3:41 this opportunity to finally learn
3:44 programming and transform your life
3:46 because you really have nothing to lose
3:49 and a lot to gain so as I said our goal
3:51 for this video is to be able to create
3:54 these bank accounts programmatically so
3:57 the user enters name of the owner and
3:59 then when he clicks on create account
4:02 then account should be added into this
4:04 grid and then also we want to implement
4:06 these two functionalities so that we can
4:09 deposit and withdraw money from existing
4:12 bank accounts and that is what we are
4:14 going to do together in this video and
4:16 if you want to download my project so
4:18 that you have the same application that
4:20 I have here and so that you can continue
4:22 building it together with me I will
4:24 leave a link in the description to my
4:26 GitHub repository and from there you can
4:28 download this application and you can
4:30 code along with me me and Implement all
4:33 of these functionalities please just
4:34 keep in mind that in order to be able to
4:36 work on this application you need to
4:39 install two things you need to have
4:42 Visual Studio 2022 or newer version and
4:45 the second thing is you need to install
4:48 net 8.0 framework because those are two
4:50 things that I have used in order to
4:52 create this application and if you don't
4:54 have those two again I will leave a link
4:56 in the description of this video that
4:58 you can use to download them and install
5:01 them and the in ation process is very
5:03 easy mostly next next finish and then
5:06 they are completely free for students so
5:08 let's first explain what are
5:10 Constructors you can understand a
5:13 Constructor as a special method that is
5:16 invoked when you create an object of a
5:19 class and the job of Constructor is to
5:22 create or to construct that object so
5:25 basically Constructors are used to set
5:28 the values for the properties of the
5:31 object that you are creating so when you
5:33 first create an object its Constructor
5:36 will be invoked and inside Constructor
5:39 you can set the initial values for the
5:42 properties of that object so this is the
5:44 definition and now let's see how this
5:47 works in practice because that way it
5:49 will be much easier for you to
5:51 understand and to learn what are
5:53 Constructors and what are the benefits
5:57 of using Constructors so I will stop my
5:59 application and I will open my bank
6:02 account account class and I want to
6:05 create the Constructor for this class so
6:07 now let's see how we can do that now
6:09 there are a few rules when you are
6:11 creating a Constructor and the first
6:13 rule is that you should create a
6:16 Constructor inside the class that it
6:19 belongs to so if we are creating
6:22 Constructor for bank account class then
6:24 we need to create it inside these curly
6:28 brackets of our bank account class that
6:30 is the first rule the second rule is
6:33 that Constructor always needs to be
6:37 public so I will say public and there
6:39 are some exceptions to this rule you can
6:41 learn more about those exceptions in my
6:43 practical programming course in the code
6:45 Beauty Academy because those are some
6:48 more advanced topics for now let's just
6:51 say that Constructors always need to be
6:54 public okay that is the second rule the
6:57 third rule is that Constructor does not
7:00 have a return type and then the fourth
7:03 rule is that the name of the Constructor
7:06 is the same as the name of the class
7:10 that it belongs to so by doing this I
7:13 have created a Constructor now this
7:15 Constructor is currently empty but as I
7:18 explained the Constructor will be
7:20 invoked every time that you create an
7:24 object of bank account class which means
7:26 that here we can write the code that
7:29 will be executed every time that we
7:32 create a new bank account and before we
7:35 start writing the code and changing this
7:38 empty default Constructor let's first
7:41 explain what we want to achieve so let's
7:43 go to the code behind of our form simply
7:45 right click on the form and click on
7:49 view code and here you will notice that
7:52 we are creating bank account objects
7:56 this is the first second and third bank
7:58 account that we have created and you can
8:01 already notice that it is taking a lot
8:04 of lines of code to create these three
8:07 objects so now the question is is there
8:10 an easier and better way to do this same
8:13 thing it would be very cool if we could
8:15 do something like this so instead of
8:19 creating new bank account that is empty
8:22 and then assigning owner account number
8:24 and balance we would like to do
8:27 something like this I'm going to comment
8:30 these three lines of code and and here
8:32 inside these parentheses when I'm
8:35 creating my bank account I will pass
8:38 this owner name like this and then I
8:42 will pass this go like this and then
8:45 instead of passing this as a balance
8:47 let's say that initially whenever you
8:50 create a new bank account its balance is
8:52 going to be zero and then if we want to
8:55 create another bank account again I will
8:58 not need these three lines of code
9:00 instead I will pass here everything that
9:03 is necessary to construct that bank
9:07 account so owner's name again and then I
9:12 will pass this new guid like this and
9:14 then as I said balance initially when
9:17 you are creating an account will be zero
9:19 like this and we will do the same when
9:22 we are creating this third bank account
9:25 so I will comment out this entire code
9:28 and then here when I'm constructing that
9:32 object I will pass owner's name and then
9:35 good which will be the same as in the
9:37 previous two and then balance will be
9:41 initially zero like this so my goal is
9:43 to delete this code that I have
9:46 commented and then when I'm creating my
9:50 objects to use just this code here that
9:53 remains so now the question is can we do
9:56 this and how can we make our code to
9:58 work because currently we have these red
10:00 squiggles which means that we have
10:04 errors in our code well let's explain
10:06 what is happening here so when you are
10:10 creating bank account object here will
10:12 be invoked the Constructor because as we
10:15 said Constructor is used in order to
10:19 create or to construct that object so
10:21 here what we are doing instead of having
10:24 empty parentheses is we are passing some
10:27 parameters to our Constructor and those
10:30 parameters are the values or the
10:32 information that the Constructor needs
10:36 in order to construct that object in our
10:39 case we are passing owner's name and
10:42 then good and then balance and we are
10:45 doing that for all of the bank account
10:48 objects that we are creating and the
10:50 reason why we have this red squiggly
10:53 this error is because we don't have any
10:56 Constructors that correspond to these
10:58 parameters here so we don't have any
11:01 Constructor that receive these three
11:03 parameters and then based on these three
11:07 parameters they construct the object so
11:09 in order to fix these red squigglies we
11:12 would have to create a Constructor that
11:16 receives owner's name new guid and then
11:18 balance and then it creates an object
11:22 based on these three informations but
11:24 before we do that let's see if we can
11:26 make this code even shorter please
11:28 notice that when we are creating these
11:31 three Bank account initially the only
11:34 thing that is different is owner's name
11:36 so this account is mine and then this
11:39 one is from Elon Musk and this one is
11:42 from Bill Gates these remaining two
11:45 things so good and balance the way that
11:48 we are creating those is the same so
11:50 here we are generating new good which
11:53 means new account number and we are
11:55 passing that as a parameter to our
11:58 Constructor and then here as well we are
12:00 creating new guid new account number and
12:02 again we are passing it as a parameter
12:04 and we are doing absolutely the same
12:06 thing for this third bank account which
12:08 means that we don't need to pass this as
12:11 a parameter to our Constructor we can
12:14 simply request a new guid in the
12:16 Constructor itself and then for this
12:19 third parameter which is balance we said
12:22 that initially each bank account that is
12:25 created it's balance will be zero so
12:27 again we don't need to pass this as a
12:30 parameter to our Constructor we can
12:32 simply do it in the Constructor for each
12:35 bank account that we create so that
12:37 means that we can remove these two
12:40 parameters from our Constructor like
12:42 this and the only thing that we need to
12:45 leave is owner's name so I will remove
12:49 it from here as well and then from here
12:52 also so when we are creating new bank
12:54 account the only thing that we need to
12:57 pass to the Constructor is the name of
13:00 the owner so so now instead of creating
13:02 a Constructor that has three parameters
13:04 we can create a Constructor that has
13:08 just one parameter and that is owner so
13:11 now let's do that let's go to our bank
13:13 account class and here let's say that
13:17 our Constructor will receive one
13:20 parameter it will be of type string and
13:23 it will be called owner with lower case
13:27 letters okay so here inside these curly
13:29 brackets of my Constructor what I will
13:32 do is I will say that this property
13:35 owner property of my bank account will
13:39 be equal to this parameter that I have
13:42 received so whoever is creating new bank
13:45 account object he needs to pass owner as
13:48 the parameter to the Constructor and
13:51 then here we will construct that object
13:54 based on the value that we have received
13:55 as the parameter that is going to be the
13:59 name of the owner and then for these two
14:01 remaining properties account number and
14:03 balance we already know how to
14:07 initialize those so for account
14:10 number I will say that it is equal to
14:13 new guid like this so for each bank
14:16 account please generate new good and
14:19 then for balance we know that initially
14:21 when you create a new bank account its
14:24 balance is going to be zero so with this
14:27 we have successfully created our
14:29 Constructor it receives one string
14:32 parameter that is owner of the bank
14:35 account and here inside these curly
14:37 brackets of the Constructor we are
14:39 setting initial values for the
14:43 properties of that object so the owner
14:45 of the bank account will be the value
14:47 that we receive here and then account
14:50 number will be new guid and then balance
14:53 of that bank account initially will be
14:55 zero and this Constructor will be
14:58 invoked for each new bank account object
15:01 that that we create and if we go now to
15:05 the code behind of our form now you can
15:07 see that the red squiggly that we had
15:10 here has disappeared and currently we
15:13 don't have any errors so now we are able
15:17 to use this code which is a lot shorter
15:19 in order to create objects of bank
15:22 account class so when we are creating a
15:26 new bank account here it will invoke its
15:28 Constructor and this will be passed as
15:31 the OWN for the bank account and then in
15:33 the background Constructor will also set
15:35 the account number so it will generate
15:38 new good for the account number and also
15:41 it will set the balance for that account
15:44 to zero and if I start the application
15:46 as you can see we will get the same
15:48 behavior that we previously had but now
15:52 the code is a lot shorter and even if I
15:54 want to add a new bank account I will
15:56 simply copy this line of code I will
15:59 create bank account for and here I will
16:02 say that the owner is going to be let's
16:06 say Mark Zuckerberg like this and then
16:09 we will simply add that forth bank
16:12 account to our list and that list will
16:15 be set as the data source for our grid
16:17 so now when I start the application
16:20 again as you can see here is our forth
16:22 bank account so in this video you
16:24 learned what are Constructors and how to
16:26 use Constructors in order to easily
16:30 create objects of a class class and here
16:32 we have four objects that we have
16:36 created with the help of our Constructor
16:38 now there are a lot more things that we
16:40 want to do in this application for
16:42 example something that I currently don't
16:45 like is that these four objects are
16:48 hardcoded in our code and what I want to
16:51 be able to do instead is I want to be
16:53 able to create new bank account by
16:55 clicking on this button and then also I
16:57 want to implement these two
16:59 functionalities so that I can deposit
17:03 and withdraw money from my bank accounts
17:04 but since I know that you don't like
17:06 videos that are too long let's do that
17:09 in the next video of this playlist which
17:12 I will link here so in the next video we
17:14 are going to implement these three
17:16 functionalities so that we can
17:18 programmatically add new bank account
17:20 and then so that we can deposit or
17:23 withdraw money from the selected bank
17:25 account so if you have any questions
17:26 please leave those in the comment
17:29 section and if you enjoyed this video
17:30 give it a big thumbs up so that I know
17:33 to create more videos like this one and
17:35 if your goal is to learn practical
17:37 programming so that you can start a
17:39 successful career and build useful and
17:42 interesting applications and if you
17:44 enjoy learning from my YouTube channel
17:46 then you should definitely enroll in my
17:48 practical programming course because
17:50 there I will take you from beginner to
17:52 expert in just a few months and you will
17:55 get all of the support and help from me
17:57 that you need in order to achieve your
17:59 goals in the course we are working on
18:02 realistic projects so you will get real
18:04 experience that you need I will also
18:06 introduce you to a community of
18:08 successful developers that you can learn
18:11 from and collaborate with and everything
18:14 that you need will be on one place so
18:16 your job is just to follow the plan that
18:19 I have created and that plan will take
18:21 you to your first job as a software
18:24 developer so if you decide to join in
18:26 the description I will leave a special
18:28 coupon code that you can use in order to
18:30 save some money and if you're not sure
18:32 if the course is for you or not you can
18:35 definitely try it out for 7 days because
18:37 we have a 7-Day money back guarantee
18:38 which means that if you don't like it
18:41 for any reason you can get all of your
18:43 money back no questions asked from our
18:46 side so you really have nothing to lose
18:48 and learning to code can absolutely
18:52 change your life so with that being said
18:54 thank you very much for watching and I