 Hey everybody, this is Brian and welcome to the 86th Qt tutorial with C++ that could be programming I'm going to choose console application and today we're going to be doing more advanced binary output input output. So we'll just call this Benio2 And let's just throw this in the usual location Hmm, my microphone's got a bunch of knots in it. I think the cat's been playing with it Anyway, sorry to get sidetracked okay, so The basic premise of what we learned in the last tutorial was that you could take objects and output them to a file stream Well, this time we're going to do relatively the same thing But we're going to output a class to the file stream. So let's just Add new and we're going to make a C++ class Let's just call this person Notice how it makes person age and person.cpp and just next next and We have our basic person class here And you notice how there's really not much to the person class at all So we're just going to add a few things here and we're just going to say include Cucor, I know people hate it when I do this but it saves time Now we've got our constructor and we're just going to say int age and Normally you would use a getter and a setter This is actually very bad form to do it this way where you have your variables public publicly accessible But like I said, we're trying to save time and you already know how to do all this stuff So we're just making a very simple class. All this class does is hold the age and the name now What we need to do is in our main actually add a few includes so we'll say include the class we just made and let's just Cucor There we go, then we're going to want two functions Save and load and you guessed it one saves and one loads So the first thing we need to do is actually make a person And we'll just call this person one and we'll say person one That age something grabbed my mouse and we're going to say equals 36 And person one dot name We're gonna say is Brian because that's me and we're gonna make another person just for the sake of argument here. We're gonna make two of these Say person to we're gonna say 18 and we'll just give my daughter's name Heather All right now that we've got our two person classes or our people as we should say We're gonna actually create a queue file and you know stream this out So say queue file I'm gonna say file and Put this you know wherever you've been putting your files And we'll just call this people dot txt Now remember we're not actually you know, don't get thrown by the dot txt We're just doing that so you can simply open up a notepad and kind of peek at the contents if you really want to It is a binary file though. So chances are it's just gonna be nothing but gibberish And we're gonna say file open And we want to do queue IO device We want write only because we're going to write to the file So if we could not write to the file then we're just going to simply say Queue to bug and we're just going to Output here. Whoops. Hope if I got it in the quotes could not open file that way because I'm just you know basic Debug logging here kind of show us you know something catastrophic happened with the file system and then we exit out Otherwise if we can't do it, we're just gonna say queue Data stream and we'll just call this out Give it a reference to our file and then of course out Set version and then we want to do the queue Data stream and we want the version of this which is going to be Cute and the newest one which I believe I'm on for seven Although here than the cute five is scheduled to come out soon. I could be wrong. Anybody else hear anything about that? Let me know All right, scroll down here, and you know, we're just we're just doing some basic output here. So we're just gonna say out Person one out Person two So we're just gonna output our two classes out to the file say file that flush and nothing new here I mean, this is all everything you already know Alright now, what do you think's gonna happen when we go to run this? Well, let's find out. Let's actually go here and let's say save Some of you're probably snickering because you've made this mistake before. Oh, wait a minute No match for operator In out. Hmm. What does that mean? And you see oh my gosh catastrophic error. I mean all these things just exploded behind it Well, if you remember from your C++ book you did buy one didn't you? There was a chapter on operator overloading So if you go out to C++ comm let me see if I can expand this tutorial slash classes to They have this very very nice chapter on overloading operators and they tell you you know first off What is an operator? Well any of these symbols are an operator Plus minus multiply left shift right shift things of that nature. They're all operators Well in C++ you can actually override that behavior and it's actually expected that you do that This is a perfect example of why you would need to do this because we're trying to left shift this person one class into the output stream and Person one class doesn't know how to do that So we have to tell it, you know what we expect of it in order to do this So what I'm going to do here is We're just going to add new and we're going to make a header file And we're going to call this That's a good question. What should we call this? Let's call this overloads Now we've got our header file notice how there's no source file with it. It's just our header And we are going to Give it a few includes here Oops, we have a person dot h and we're going to say Include Qt core Now we have our includes now what we need to do is actually override the operators So that we can at any time Tell it what we wanted to do and how we expect it to act so Q data stream and we need to say the reference to the Operator and we want the left shift operator And we'll call this Q data stream Out And we want a constant Person class And we're just going to call this Person and let's see here Somebody asked me once in a while if I could just do that so you guys could see the code All right now we can just pretty much copy and paste this because they're actually very similar and Pretty much the same thing call this in These are our function prototypes here what you're needed and Then we're going to actually implement these and If you're like me when you first read your C++ book you did buy one didn't you? When you read it you got to the chapter on operator overloading you thought this is fascinating But when am I ever going to use this? Well, this is one of those times so we're going to write a person class to the stream and we're going to say out person not age and Person that name and then we're going to return the output stream So really all we're doing here is we're telling it exactly how to put this class into the output stream And remember anything that we do we must do the opposite so we'll say Read in a person class and we'll say person equal and then in we want to Now that we've got a new person class here because remember we may be reading it in and we don't actually have a class yet So what we're going to do is we're going to read in the age and then you guessed it the name and then we want to return the input stream So real basic operator overloading we got here We're going to override left shift and right shift. I believe those are called left shift and right shift Somebody please correct me if I'm wrong. It's been forever since I even done this And then we implement the overrides and really all we're doing is we're telling it exactly how We want that at output it in what order and remember you have to read it in the same order Now actually let me Throw that back up there make sure we're in the right file here. We are all right now We're in Maine what we can do is do an include and we include our overloads And if you remember back from our C++ tutorials anything that you include is going to be Pre-pended meaning it's just like putting the code right here. So let's save and run this and Yep, it's a liquid habit. We did something wrong here passing constant person as this argument of person Hmm. Oh, I see what the problem is here. We have it as a constant. We can't do that Geez, sorry about that. Like I said, it's been a while. So let's real quick review here We don't want a constant because constant means we can't change it and we're actually changing it when we read it in So for the output we want a constant because we don't want to change it for the input We want to change it. So there's no constant Let's run this Hmm definition of implicitly declared person class. Hmm. Yes Let's review real quick I want to make sure we've got this Taking out the person constructor and there's no person constructor here Now everything should line up and run just fine. There we go So we ran this we haven't outputted anything, but we did write out to the text file And we'll just grab a instance of Explorer and there it is And as you can see there's our two classes Brian and Heather So now we want to be able to read that in Now you may be asking why do you even need person dot cpp? Well, you really don't for this example because we're not really doing anything So you could get rid of that if you really wanted to. All right, there's our overloads. There's our main So now we want to load this up All right, so we'll get rid of that. We're just gonna Create two variables and not initialize them Use the same file We want read only we'll call this in in We're just gonna do in person one in person two Don't need to flush, but we do need to close then we're just gonna say Qtabug person one age and Actually, let's sorry. Let's flip this around. Let's say Person one that name we're gonna just gonna say, you know name is and then the age so Got that Let's hide this again for you. There we go And we want to read in person two Or should say we want to output to person two. We've already read it in Now I'm gonna come out out save you don't really need to I mean it would just write the file again And we're just gonna run this and see what happens when we load it Sure enough Brian is 36 Heather is 18. So that in a nutshell is I would call that more advanced Binary input output and let's review really really quick just to make sure all this kind of sinks in here We've got a few files We've got our person class, which is just you know a representation of the data. We want to store We have our overloads file, which has the function prototypes for the operator overloads And then it has the implementation of each one and remember We're just telling it how we want to write the data out and how we want to read it in C++ is an amazing and incredibly stupid tool. You have to tell it everything because it does exactly what you tell it to do We have our implementation file for person But it's empty because there's just nothing in there and then we have our actual program which you know We're importing overloads in person and then we're just you know creating an instance Filling with information Opening a file setting the version Outputting it flushing and closing and then we're just doing the reverse We're just creating a few instances opening reading the file setting the version Reading in the objects closing again, and then we're outputting to the screen Very simple very useful So that's all for this tutorial on This is Brian. Thank you for watching. You can find all this and more out on my website void realms comm Yes, I've really been pushing the website because you can go out Free of charge and get the source code for this tutorial save yourself a little bit of typing or if you you know Type it out. It's just not working. You can just download the source and it should just work So give me some user feedback I'm really kind of curious to hear what you guys want to see in the way of tutorials in the near future We've covered a lot of topics and I'm kind of interested to see where you guys want to go Now, please don't flood me with Brian Let's make a 3d video game because those take you know a dozen programmers three years to make And we're talking something simple that you can digest in you know 15 minutes or less. All right. Thanks guys