Learn Finance C++, Lesson 29, Bond Class Part III

More on the construction of our Bond Class.

Okay so we’ve um we’ve got this principle back and we’ve printed it out what if we now wanted to change it because when we when we run this program we’re getting the default um we’re getting the default value why is that because that’s what we’re setting it to oh by the way while we’re in here we really ought to do this properly so what i’m going to do is i’m

Going to i’m going to do this properly not this kind of hard coded we really ought to call the internal subroutines to do this rather than setting those variables directly same thing with set rate we should always go through the interface wherever possible just just to be nice and clean all the time if you can accept the hard coding for the moment anyway i know

This is hard curly but we’d use constants definitions and things to to to not have to use these things right so that i think that’s looking nice and clean now let’s get back to the back to the main program so what i could do is i could i could call the basic i could call the bond and get the default constructor which is this thing here which is going to set my

All my values to 100 and let’s say it was a thousand dollars that i actually wanted to to to do with my pricing of my bond well at the moment what i have to do is this i’d have to create the car key the basic bond object from the blueprint to class then i would have to go into that use the key to set the principal to somebody say a thousand dollars and now if i

Print that out we should see that that starts as 100 and goes to 1000. hopefully that’s all making sense but that’s a bit tedious isn’t it it’s a bit tedious creating the basic object and then having to call in an object method to change the default of 100. wouldn’t it be nicer instead if we did this instead so so we’ll do it another one down here so if i do bond

More complex bond but this time i set it with four parameters i set the principal to a thousand i set the rate to five percent i set the years to maturity to ten and i set this to being a semi-annual bond and then when this bond is created this this key what we will do actually let’s make this a a very weird bond very very strange bond with 999 in the principle

This would be nice and i’ll just cut and paste if i could just do it all in one hit like this so i need to just change all of these rounds obviously i know this isn’t going to work because i haven’t sorted this out yet so wouldn’t it be nice didn’t need to do all that did i if i could set it like that instead directly just straight away set it to 999.99 now i

Know this isn’t going to run but what i’d like to see is i’d like to see 999 coming out what i can do is i can set up another constructor so this constructor is you go to the mercedes dealership and you say i want a mercedes and the mercedes sales person says um what kind of mercedes and you say i don’t care just give me a mercedes so they just write down silver

Two liters um s-class but here we want um you know kind of we wanted to be a with a roof that comes off we want it to be a five-liter car uh we want it to be in bright yellow and we want it to be e-class or whatever yeah yeah you can do that you can have separate constructors so this constructor is the basic constructor and this constructor is going to take

These four parameters let’s then have a look at how we do that the first thing we do is we go to the dot h file and we’re going to have to set up another constructor notice it’s got the same name and it doesn’t return anything so there’s no inter or anything being returned same name nothing being returned c plus goes haha that must be um constructor now we’re

Getting an error here because you can’t have two constructors which are identical that’s a bit silly but we can overload here we can say well if this one’s gonna take a double double a double and a char no problem now um again this is the interface so this is the going to the mercedes deal i’m saying just give me a mercedes and they give you a silver two liter

Full door here you’re saying i want a mercedes but i want it in yellow i want the roof to come off i want a five liter engine and a big headlight big headlamps or whatever yes you can do it obviously we’re gonna have to go into this um file and write another constructor second constructor so this is going to have to take a double which will set to be prin a

Double which is the rate so i’ll call it rat so we know it’s different double which is the year to maturity and the char which is the type i’ll just do that so that we that so we can see it’s different from the variable okay now we can set those things via the special set methods which we wrote in the previous lesson copy and paste to rooney there we go i have

Created a tailored bond this is the default bond when we just have the basic bond constructor with no arguments this is the tailored bond constructor where we have all of these elements here so let’s now give that a quick compile build succeeded i’ve created a default bond this is before then i’ve called set principle then this is a thousand i’ve created a

Tailored bond a tailored bond here which has a principle of nine nine nine nine now sometimes as well sometimes you’ll see the following where you’ll see the notation that looks like that and sometimes with this one people will do that um let’s just quick quickly run that often typically works i just like this one it’s just this way of doing it’s just a little

Bit less uh just a little bit less typing again c plus plus just turns one into the other so don’t worry too much about that just go with the flow every shop has a different way of doing it okay i think with an early there i think we’ll just do one last little thing before we move on what i’m going to do is i’m going the whole point of this is to get a price to

Get a bond price so i need one more method get bond price and oh sorry i need to put it’s not the same as the name of the class i need to put what it’s going to return it’s going to return a double that looks alright i think so far it’s going to make use of these four variables to return a bond price so let’s um go into here and let’s put it before all of the

Getting sets which are all fairly standard so uh double gets bond price no parameters not used uh i’m just going to return 42 for the moment in the next lesson we’ll actually put a function in there which will calculate a price let’s go back to the main program so let’s get out some prices for these two bonds so see out basic bond price just copy that it

Bond price get one price and we’ll get the complex bond as well which has the tailored inputs and these will both come back 242 but we should be all right there uh let’s should come back both come back there’s 42 in the next lesson we’ll fail what’s wrong with that looks all right to me get one price in here that looks all right to me what if i didn’t wrong in

Here get bond price that looks all right as well what’s wrong with uh what’s wrong with these things try that again what have i done wrong here it’s not telling me where the error is what’s the problem oh very bizarre get button price let’s have a look at this thing here double that one price looks all right to me down here ah silly boy there we are forgot to

Put the uh forgot to get the thingy in there should work now yeah there we go got there in the end both coming back as 42. and i think we’re done next time we will put a proper algorithm in there to come back with a decent price see you next time

Transcribed from video
Learn Finance C++, Lesson 29, Bond Class Part III By AndyJamesDuncan