oh if you okay if we didn't initialize name would the code still run the same we could certainly do that like for example we could have a constructor that only takes in the integer and only initializes the balance because as long as we have set functions i mean we ultimately would still have a way to set the name but usually you're going to want to have a constructor that can handle all of the all of the member variables here and sometimes that could result in having different constructors or in some cases we might just have
kind of one big constructor that takes in all the arguments and initializes all of them and that and that's fine in the sense that you know we can handle everything with this one constructor but what that means in terms of the application program is now there's really one specific type of way that we have to go about creating account objects so we'll see especially in chapter nine different you know different ways in general of handling constructors because we've hinted at the idea that you can have multiple constructors meaning that you can overload constructors you can
also have things like default default arguments which you saw that in 110 165 where you can have a single constructor but you can potentially pass in differing numbers of arguments so there's different ways in general to handle all the parameters that you have but usually between all your constructors you're going to have you're going to be dealing with each of those member variables in terms of initialization meaning that in this case since we have the string and the integer we're going to have at least one constructor that could be used to initialize each of those
and then the next function here um is that deposit function um so we're just able to pass in a amount that we want to deposit and then add that to the initial balance and again we just have a little bit of a check here so if the deposit amount is positive number um then we're going to just add that to balance and then that's going to become uh the new balance and then we have a get balance function here and a couple of things we'll notice is that number one we don't have a direct set
balance function and that's something that you you know technically could have added but we chose to not have that here and now one of the things that we mentioned last week was that in many cases for the member variables that you have you know a common thing to do is to have a set function and a get function for that member variable but this is an example of where we chose not to do that and the idea now we have a deposit function here you could also have a withdrawal function if you want to be
able to you know decrease the balance or take money out of the count the idea here though in terms of functionality in terms of how this is going to be used in the application program uh what we're in effect saying is that you know we don't want um the application program or the user to be able to just at any point in time go in and call a set balance function to just change the balance in a way that's completely unrelated to what the balance was previously um so the idea here in terms of how
we would use these account objects is in the beginning we do have to provide an initial down initial value for the balance because that's the only constructor that we provided so we're going to have to provide both of those two arguments when we create the object but from there once the object has been created the only way it's going to be changed is in terms of things like this deposit function or if we wanted to have a withdrawal function then we could also deduct from the balance but the reason there why you might not want
to include a set balance function is because that's going to just allow you to or the user to essentially just kind of randomly change the balance without really any regard to what the balance originally was whereas here if we instead limit it to things like a deposit function or a withdrawal function then any change of the balance um is relative to what it currently was right in the sense that if you have 500 dollars for example as your initial balance and then you deposit 300 then it can go up to 800 whereas if we had
a direct set balance type function then you could just kind of at any point later while the program is running you know set it to a million dollars right for example even though it was maybe much less before that and there was no um there were no actual deposits or withdrawals that took place so this is one of those examples where we may not really want to have a set balance function but rather do things in terms of maybe deposits or withdrawals so every change that that takes place related to the balance is in terms
of what it currently is and we're either adding a certain amount to it to increase it or we're deducting a certain amount from it to um to reduce it but we're not just allowed to kind of randomly change it all together while the program is running so for that reason there was no um set balance function so sometimes um you want to kind of think about that for each member variable and whether you know it's really appropriate to have that or not but we do have a get balance function so the user can do things
like return the balance and then maybe print it out and one of the things that we hit on also a little bit last time was this keyword const and we actually saw that come up with a lot several of these get functions where we would have the usual information for the function the return type name parameter list but then after the parameter list and before the body we have that keyword const and um does anybody remember what that um what that was being used for good good yeah i can't change used to restrict our ability
to change the value um in particular the values of the member variables so constant is really used as essentially a safeguard here so it just means that if we try to change any of these member variables inside of that inside of that constant function then we're going to get a compiler error and it's not going to run or similar idea if we tried to do something like if get balance for example tried to call one of the functions that doesn't have constant which we sometimes call those non-constant functions so if we try to do something
like have get balance call set name that's also an issue because that would still allow you to indirectly change a member variable through the function get balance so it even detects those more indirect ways um of trying to get around that and trying to change it and so we'll see that so certainly const here is a good idea um for these get type functions and um we'll see with some of the examples that we'll look at in chapter nine that it's also a good thing uh because in some cases you might have constant objects meaning
objects in your application program that you create with the keyword const and those objects can only call member functions that also have the keyword const so that's another reason why we also do that so that way if we have a constant object we could still call a function like get balance and see the balance for that constant object so it protects yes because in terms of the the function the compiler doesn't doesn't know your intention so to speak right so we're calling the function get balance um or or likewise down here we have function set
name but all the compiler really sees is whether it was declared constant or not so if it's uh if it's not declared constant then you have access to everything right so for example in the set name function you can change you could still change the balance right inside the setting function because the compiler that you know doesn't have a way to to know what what the intention what the plan is all it really knows is whether we've defined it as a constant function or a non-constant function so in that sense you could still do things
like change the balance right inside of the set name function but at least in terms of these functions like the get balance which not only should they not change the balance right they really shouldn't be changing anything um so we can just put that in there as an extra safeguard so that way if we did you know inadvertently write a statement that was going to try and change uh really the balance or any of the number variables then it's just not going to ultimately be able to compile and build so things like that right for
example both of those are actually are actually not allowed because so one is trying to directly change the balance the other is trying to call a uh a function that's not constant which could potentially still end up changing um the variable so it still restricts even things like like this type of function call as well and so it also detects the more kind of indirect ways where you might try to try to get around things like cotton so it's it's gonna not allow not allow that either um and then now for name we did have
a set function so that can be changed in the in the usual way with the you know just calling the set function and then we have a get name function so not too much i mean in this case we just really had one constructor a deposit function for increasing the balance and then a get balance function and then set name and get name and then again for now at least we also had um we had everything inside of the in in this same file so but in chapter 9 we'll see a different way of setting
this all up where we can have multiple files and we'll see how you can even do things like for example if you were going to share your class not give away the implementation so you'd be sharing your your class h file but instead of sharing the um class cpp file you could instead share the um object file that has the binary instructions so that way they don't see how you actually implemented your class functions in that cpp file and then the application program here account test cpp we have some calls to the constructor here there
was only one constructor so we had to provide right both of the two arguments because of the way that we set this class so if we try and do something like this um we can't do that now with the way that it is because we don't have a constructor that takes in no arguments now if we didn't write out any constructor at all then we would be doing it on this line that's highlighted here because we'd be calling the default constructor but since we wrote out our own constructor and the only one we wrote out
was the one that took in two arguments that means that we're going to have to always explicitly provide both of those arguments right so for now at least we do not have a constructor to handle that yes oh good question because that's uh the question that you asked is kind of one of the major themes of chapter nine about hiding uh hiding the implementation and now we first talked about the idea of encapsulation you know that's kind of one of those big terms that comes up uh with object-oriented programming the idea that we have both
variables and functions and we encapsulate them together into an object and another way of thinking about it was that the the functions kind of shield the variables right from the outside world right in the sense that code outside the class like maine can't directly access the variables but it can directly access the functions and we call that encapsulation right the idea that we have both of those things functions and variables encapsulated together in a class but then taking it to the next step is not only following the rules of encapsulations which we see that that
really comes down to in general making your functions public making your variables private right and then code for example in maine has to always go through the functions to do any sort of work whether it's setting a value or getting a value or in the case of this class depositing money right to make the account balance bigger um so that's part of it in terms of what we're trying to achieve here with object-oriented programming but if we want to take it you know one step further then not only can we follow the rules of encapsulation
but we can also actually hide the implementation in terms of how we implemented the logic for the functions now right now we're not doing that because since everything is in this file account.h and we'll see in chapter 9 that you you definitely have to share the h file with whoever is going to use your class right meaning your class header file so if we put all the class code inside of the header file then whoever we're allowing to use the class is going to see of course how we implement it right so they're going to
see you know all the details in terms of how we implemented this two argument constructor and the deposit function and so forth and you know i mean some of these functions might be predictable in the sense that it might not really be a surprise um in terms of how they're implemented um so things like a kind of a bare bone set function or a get function but in general right when you're talking about these functions that make up the class um they may be proprietary right and they may be you know much more involved than
what we're doing here in this account class example uh and you don't want to just necessarily give it away right you don't want to just give away how you did it to everybody who's going to use your class right because they might even be you know they might have been paying to use your class right but the point is they're paying to use the class not to not to know all the details in terms of how you implemented it right you still may want to keep that you know proprietary and within the company and not
just share that with everybody so if we do everything in the h file then they're going to see how everything was done but if we split the class code up into two files one of those would still be the h file the other would be a cpp file then we can start to actually hide the implementation um because they don't they don't interpret what they need they also don't need to know that right if i'm writing this very explosive sorry [Music] what was that yeah because if we're like if we're writing this um because you
know oftentimes the people that are writing the class code are right going to be different from the people writing the application um of course in a course like this um for your assignments you'll be doing both sides of it right you'll be right in the class side um and then in some cases also writing the application program but you know in kind of a real world or industry type setting um you know you'd have different people developing the class versus the application that uses the class and in fact you might even have a lot of
different applications that use the class because another important thing is for usability in the sense of trying to make classes as reusable as possible so they can be used with different types of applications but the underlying idea also here is that we don't need to know for example to call the get name function or to you or to call the constructor right we don't have to really know what the actual internal logic is there's really just a few things that we have to actually know in order to be able to call a constructor or a
function um and and that and that's the things that are already going to be included in the prototype so things like the return type which that doesn't apply to a constructor because it doesn't return a value but a return type um for a non-constructor function and then the name of the function which in the case of a constructor is always the name of the class and then the parameter list in terms of are there any parameters and then if so what are their types and those are really the only three things that you have to
actually know to call the function and so the idea which we'll see in chapter nine is that you'll just have prototypes inside of the h file and then you'll implement them in another file and that file does not have to be directly shared so that's how we can enforce this idea of hiding hiding the implementation details right it's just kind of like a lot of things you know you think about driving a car for example you know you don't have to know the internal details of how the car works we just kind of have to
know the basics of you know the steering wheel or the gas pedal um and we don't have to know how it actually works and it's a similar idea here in terms of calling functions within a class we just have to know really a few basic things um and we can use it completely without knowing how it was actually implemented so that's why in addition to encapsulation we use oftentimes also want to actually hide the implement implementation details of the functions as well oh there is one thing that we haven't um hit on quite yet in
terms of public and private