How to write a class in C++ | easy and epic examples 2021

write a class in C++

Today want to write a class in C++ what we’ve learned about classes are 5.5 ago actually writing a class from scratch and we’re going to take it too far today when I’m going to write some massively complicated class we’re just going to write a basic local laws should demonstrate some of the basic faces with once I follow over the next steps are going to keep working on this class and slowly introduce new concepts so you’ll get to see the process and the difference of kind of going for a basic version of class to a more advanced version that does the same thing but it could be considered static card but first this article is brought to you by Amazon music unlimited which is a fantastic ad free music streaming and download service with over 40000000 songs that you listen to anywhere anytime programming without music is just not the same I mean every day at work I’ve got my headphones and I’m listening to music I’m concentrating on writing that good card I’m going to a 30 day free trial of Amazon music unlimited in the description below ours to go ahead and give that a shot if you do sign up it does help support the channel and the series thank you very much to those that do all right. write a class in C++ write a class in C++ write a class in C++

Let’s talk about this low class that we’re about to write what is this lockbox third this low class is going to be a way for us to manage our log messages on the web messages or information that we want our program to print to the console is usually used for debugging purposes and it really helped us out in a game or application if we want to kind of see what’s going on and just print the status of things into consultation the council an application is really like an information dump which we can use to just to just print out what’s going on it’s also something that is practically guaranteed to always walk right if we had a graphical display of a consul in on game or something that might be a little bit different and it might not work all the time because there’s something wrong with our graphics rendering system or something like that we might not get those messages printing properly however a console is something that’s basically built into the operating system so we can we can pretty much guarantee that it will always walk. write a class in C++ write a class in C++ write a class in C++ write a class in C++

I also really like the idea of using a low cost as an example because a logging system can be as simple or as complicated as you want to be an it’s amazing how complicated some log systems are like. I’m talking thousands of lines of card just print up to the console but it’s just so it’s so important for the bonding if element that it is definitely worth spending time on that and not only that the loading systems can do things like print just to the console print it comes on from college is actually output log messages to a file or network this so much you can do that you can literally make a log class B. 10 lines of code 10000 lines card it’s just there’s so much which is why it’s a great kind of example does is built upon all right sallow close going to be fairly simple to begin with it’s going to basically provide the ability to write texts to the console and it’s going to maintain some kind of local level which is basically what level of log messages we actually want to send out to the consul so we’ll have 3 will have 3 levels to begin with will have Arab warning and message or trace the basically. write a class in C++ write a class in C++ write a class in C++

What we can do is we can set outlook systems level to be say warning for example which means that early warnings and errors will be printed but not trace messages so this is really useful if you just don’t want to see a bunch of information you just want to see what goes wrong or what your warnings are in kind of clean up becomes a little a little bit by filtering what actually gets sent and print it all right let’s open and take a look at what that might look like okay so the first step of course is going to be to create that low classrooms going right close log all right there we go down to like going to now when you think about how I look possible actually whack and a really good way of creating a new class of designing an API is bites usage so I’m actually going to go down into his main function and start typing in how I might use that law class so first of all of course I’m going to want to instantiate it I may want to specify some kind of local level as an actual parameter but I’ll skip that for now I’m definitely going to want to set a low level so I might do something like sets level and then maybe something like log level warning for warnings so this means that on the warnings all more important messages or warnings or errors will actually be printed but not trace messages and then I might want to log a warning so might be something like log don’t warn and then not in our hello something like that so now what my law class you kind of look like and I can.  write a class in C++ write a class in C++ write a class in C++

Actually just go back and start filling in the blanks now start over here also will the public functions that I made which is going to be a set level function for now we’re just going to use integers as al log levels to keep things simple that’s going to definitely do something and then I’ve also got one Sir one is going to take in a constant shock. Want to which is going to be our message which is going to use construct pointed the strings now I’m I am actually going to make an episode about C. C++ string some kind of I wanted to look at things too much right now so we’re just using construct point is there’s really no harm in doing that so our purposes and then we got to send out we aren’t getting anywhere is anymore because these functions do exist however of course log level warning does not so let’s stop by declaring our local level we’re going to want some kind of private member variable which will maintain what level the log is actually set to it’s going to be an integer and I’m going to call it log level no not that I’m using this am on to school convention which basically tells me that this is a class member variable that is private that way when I’m running toward inside functions and I end up referring to member variables such as this. write a class in C++ write a class in C++ write a class in C++ write a class in C++

I know which ones are actually part of the class carbon which won the member variables and which ones are just local variables using conventions like this isn’t required of course but it doesn’t really help feed actually organize your card and keep it clean especially when you’re dealing with large code bases and complicated classes and stuff like that it just helps so much so I really recommend that you do following convention like this so that’s all set level function written you can say that all it does is a sign our member variable with the parameters so if I close that level with 2 for example it will assign value to a memorable over here and take that back to log level warning and that’s actually start creating these into just those are just saying that too will be in faro tries a sign that one will be warning of 0 with error of course that’s makes a cut a little bit hard to rate if I just write code like this hello one well what is level one it’s like I don’t know I have to basically memorize that okay one is warning and we don’t want to deal with someone else raises card. They can find a little bit difficult to be like what is what one with one main is so we want to create some kind of terrible which has the value one which represents what we actually want to represent which in this case is log level warning.  write a class in C++ write a class in C++

Somebody come up here and I’m actually going to make them public variables now you can say that I’m kind of a kind of public written twice here and that’s because I like to separate different parts of my class so in other words public methods might go in one section and then public variables might go another section and maybe public static variables Mike on another section again this is just something that’s part of my style you don’t have to follow it but it is a suggestion there’s going to be Constance so I’m going to write cost ends and then log level warning which was set to one I’m also going to write a log level for era what does that is the and then I’m going to write one more full log level trace which are my call log level in fire and also particle detectors so we have 3 types of log messages we’ve got errands without warnings and we just got information messages by default I’m going to assign my low level to be set to low level in for many that everything should get printed now finally I can write my one function sorry I definitely want my one function to print something out of the call is also going to be using C. out for now and then just see out my message. write a class in C++ write a class in C++ write a class in C++

However I don’t think that I might want to do is actually print what kind of messages it is someone otherwise if this is a warning I might want to print warning out the front and then my messages that was inside print info at the front and then my message etcetera so I definitely want to print something like maybe warning followed by my message right now because now it’s pretty good so let’s copy and paste this function and create one version for era and then another version 4 in 4 some Michael this Infor I’m going to change this to say in 4 I’m going to change this to say era and of course this will be my error function all right pretty cool so we’ve got a way to set the log level we’ve got era worn and in car however because right now all of these messages are going to get printed all the time because we have no way to save it Hey if the low level such a warning dark prince in pharmacists we can add that by it we can add that functionality by a simple if statement so in other words if the log level the current log level is actually greater than or equal to the level for this particular message then go ahead that’s fine.  write a class in C++ write a class in C++

So we know in this case the log level error is set equal to 0 which means that if low level is 0 or above you will log this message will copy and paste the same card into one and in far. And of course change this is a log level warning and this is a low level in 4 survive this scenario here of course if we specify the level to be log level warning which means was heading level to be one if we try to log in in far it then this comparison is going to be one is greater than or equal to 2 however because one is not greater than or equal to 2 so this message will not get printed since this if statement will evaluate faults we’ve got one final error here because log level warning of course is undefined because Lowell wanting is inside a law class at a fix this which is going to set this to be logged onto log level warning and every car we set the level to be one let’s go ahead and compilers card by having control of 7 making sure that everything works.

I’m going to hit F. 5 to run my card all right check this out we’ve got a warning printing it says it’s a warning and of course we have color sorry let’s try imprints and inform message I’m going to print in 4 hello as well and I’m also going to print era while I’m out of this well. So what 3 different log messages being printed here or on the same log messages but at different levels and you can see here that only the warning and the error gets printed in for does not because of course our level is set to warning if we never set a low level then of course it should be using in far as default which means if I run this all 3 of these to get printed and you can see that they do and finally let’s go ahead and set that low level to be era log.log level error. We’ll have 5 and you can see here that early hour Eric gets printed all right pretty cool so we’ve accomplished all the goals we’ve created a very basic low class.

Now I just want to say that this is absolutely not how I would write a little class absolutely not this is fairly terrible card for a few reasons but he is very simple and it works logically how a person might think about writing this class no really how a program with a lot of experience with right this of course and I’m sure there are a lot of you watching this article being like what card even is this this is not a representation of great card but it is simple card and it is something that if you’re just starting out hopefully you will understand a little bit it also gives me a great excuse to show you guys how I might improve this class using a few different concepts and why this is bad so over the next few episodes we’re going to spend some time learning more about supposed plus classes and learning more about how we can improve this and make this what I would call professional production level card but anyway thanks for reading this article.

Also see

What are Classes in C++ | Top and latest explanation 2021

Leave a Comment

Your email address will not be published. Required fields are marked *