all right good morning hope you all are doing well um so we left off last time looking at one of the um project examples from chapter three um and so we started to talk about uh we talked now about member functions and member variables and how classes really are kind of built around both of those two things together and we also started to look at the idea of the constructor which we saw that that was really a special type of function uh that could be used for initialization so as we go through um some of
these uh upcoming examples including examples from chapter 9 we'll start to see why we typically will include both a constructor and set functions all in the same class right because they serve different purposes really the purpose of a set function is okay i've created an object and then at some point later i want to change it right and in fact if we have a set function then we can change it as much as we would want right after it's been created so certainly in that sense set functions and get functions are very important building blocks
right for our classes but then constructors are important for that upfront initialization that we often want to be able to perform as well and one of the things that we'll see is that every class has at least one constructor no matter what because if you don't write out a constructor of your own then the compiler is just going to put in a constructor there automatically for you and and that constructor is what we call the default constructor or sometimes the no argument constructor and that's just a constructor that doesn't take in arguments and essentially doesn't
do anything but it nonetheless is there and then the example that we were looking at towards the end last time was focused on that account class and we started to see how the how the constructor was working in that class because there was actually a couple different things that were going on with that particular constructor this and so this was the um the h file so this one's actually broken up into two files we'll start to see in chapter nine um also how you can break your class up into three files which is what you'll
be doing in your assignment because with three files not only is the class code separate from the application program which is a good thing but we also have this ability to hide uh the implementation details so for now um all of the class code for this account class is in this file account.h but we'll later see how you could break this up so for example you'd have account h um and we sometimes call that the class interface and then you have another file called account.cpp which is where you would have the implementation of all your
functions and constructors and so forth so we'll see soon how to do that as well but the first very first thing that we're seeing here in this account class is the constructor so in this case we've only written out one constructor it is possible to have multiple constructors in the same class right so because we have to remember that a constructor is ultimately just a function right a special type of function and it does take in arguments or parameters and so in that sense we can also overload it as long as we have some difference
um in the parentheses for our different uh versions of the constructors and so the first one here has two parameters and and that matches up also with what we have here under private so because we had two member variables one was the um the string for the name and then the other was the balance and we sometimes call that an all argument constructor meaning that it just takes in arguments for all of the member variables that we've defined down here and sometimes again depending on how exactly we want to handle initializations we might also choose
um to have different constructors as well uh because there's you know a couple different ways that you could potentially handle that you could in addition to uh this constructor maybe have a um a note a constructor that takes a no arguments and then maybe one that just takes in the string and one that just takes in the integer but we have just one constructor for now that's going to handle all initializations and but we did this in a very specific kind of way because if we look at the the curly brackets here that represents the
body of the constructor but we also have some work that we're doing outside of that as well um on this very first line up here and the idea is that we have we're initializing both of the member variables so we'll see that um because we set the class up this in this specific kind of fashion um when we create an account object in the application program since the only constructor that we defined here was this uh two argument constructor that takes a string first and an integer second since that's the only one that we provided
what that means is that when we create an account object um we're also going to have to provide two arguments at the time that we create the object in other words we won't be able to do things like account um acc semicolon right there'd have to be two parameters that are always going to be passed in because that's the only constructor that we actually explicitly wrote out here and so one of the things that we'll see going back to that concept of a of a default constructor the idea there is is that the default constructor
is only um it's only automatically created if you don't write out any of your own constructors but as soon as you write out one or more of your own constructors then it's not just going to automatically put in that default constructor that has no arguments for you so that's how it's going to be handled but the first thing is up here on this line with the colon we have the initialization of the name field so we actually did a couple things here because we'll notice also that the name field is not referred to at all
inside the body right meaning in between the curly brackets the only time that we refer to it is on this very top line here where we have the colon and then we have name and then in the curly brackets we had account name and so this is a form of initialization with a constructor we have in general a couple different ways that we can that we can potentially initialize the member variables that are part of the class and one of them is what's called this member initializer list so that's what this part up here is
called with the colon we call that we call that number initializer list because we're just talking about the fact that we can potentially initialize all the member variables there but whether we end up doing that or not is usually going to depend on how exactly we're using those member variables so we can make it into a list and we briefly hit on that at the end of last time meaning that you can have a comma here for example and then do balance right curly bracket initial balance you know if you wanted to do that but
we're choosing to do it in a little bit of a different way because we wanted some input validation and sometimes you know it might work out to where with some of the member variables we don't really need to necessarily check or validate anything whereas with others we might need to so while we could have done um you know kind of the bare bones minimum implementation so to speak would be something along these lines where we would have uh where we would have both of the member variables up here on this line with the colon the
idea being that we would just simply be taking whatever was passed to account name and then storing that in name and likewise whatever is an initial balance storing that in balance um that would be kind of the minimal way of doing this but we're wanting to do some input validation here which is taking place inside of the body of the constructor so we're handling the name and the balance in two different ways because the name is something that we're not validating right we're not checking anything there but we are doing um a short check with
the balance before we actually put um the value of initial balance into the member member variable balance so that's why we're handling the two in two different ways but we just have a very you know quick check here so we're just saying if the value that was passed to initial balance right meaning the parameter initial balance if that's greater than zero then we're just going to take its value and put it into the balance member variable which is part of the object itself and that's one way of potentially handling some basic input validation and and
the reason why this is going to be okay here is because of how we initialize balance and we actually did that down here and we called that um so again there's kind of a lot of terms that'll come up in chapter three and chapter nine uh but what we did here for the balance was a little bit different than what we did for the name because we have these curly brackets and then the value zero and we call that an n class initializer so that's just saying that for any account object that we create the
default initial value for the balance right because uh we know from our discussion last time when we talked about um memory and objects that in terms of what you actually physically store for each object it's it's just the member variables right so for each account object we have space and memory to store the name and then we also have space and memory to store um the balance as an integer value but what this curly bracket with a 0 here is just saying is that that's going to be always the default initial value but we can
change that in places like the constructor or possibly a set function as well that's where we're first initializing balance is actually just down here um under private where we're actually declaring the member variables um and then what we're doing here lines up with with that initialization because what we're essentially saying is that if what we pass to initial balance if that was greater than zero then we're going to take that value and put in the balance otherwise we're not going to change balance meaning that balance would just remain at zero so if we pass in
zero then we don't need to change anything since balance is already zero but more importantly if we pass in a negative number then that's going to be our way of rejecting it so we can never pass in a negative number from the application program and and then have that actually become the value of balance in the actual object right so that's going to be prevented with this logic that we have with that if statement okay oh question chat um yeah if you physically code if you physically code a constructor with no arguments um is it
still called a default constructor yeah it's the same idea i mean sometimes we call it default constructor another term that sometimes come up comes up as no argument constructor it's the same general idea of course one difference is that the default constructor itself does not um does not initialize like your primitive types here like so if we don't write out any constructor and we just rely on the compiler generated constructor that one does not for example initialize balance whereas if you write out your own constructor that takes no arguments you could certainly do things like
manually initialize the balance to zero which we did it here in the under private directly but you could also do that in a constructor as well but yes typically yeah the one that has no arguments we call that usually default or no argument constructor sure so in terms of the initialization here that's again one way of doing it where we just kind of do a quick if check and then um in this case since it's initialized to zero uh we didn't really really need else there um but we'll see in chapter nine especially some alternative
ways of handling this general issue where the application program might be sending in an invalid value into the class one of the class functions or constructor because there's different ways in general that we could handle that sometimes what we might end up doing is we might have a constructor call a set function and then the set function will have uh its own input validation logic so that way you still end up going through that input validation um and and sometimes we might do it like what we're doing here and then another way of potentially handling
it is with what's called throwing an exception which we'll see some examples of that um in chapter nine uh but one of the ideas here is that we you know we usually don't do a lot of you know complex um input validation logic on the on the class code side so usually it's going to be pretty short in terms of what you do because really the application program needs to handle things like invalid inputs from the user right that's not something that typically a class function is going to be designed to handle