C++ Programming Tutorial 22 - Constants const, macro, and enum

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what in the heck has happened in my home dogs this video we're gonna be talking about constants before we dive in I want to give a shout out and thank you to our sponsor Embarcadero C++ builder C++ builder is a C++ IDE with awesome GPU accelerated native UI libraries so what does that mean in English basically we can create applications for different platforms so Android iOS Mac Windows and we can use their UI tools to build user interfaces that are native to these different platforms so you can make your user interface once and then what you can do is you can change the UI to make it perfectly designed for your target platforms so you can take advantage of native capabilities for these platforms so if you need a C++ IDE with visual designers check out our sponsor I'll leave a link for you guys in the description and by the way the Community Edition is free seeing get started right away now let's get back to constants when we create a variable like so int x equals 5 this here this value here is known as a literal you may also hear constant so this is a literal constant constants are very important and they're fundamental in any programming language we need them in order to sign values to variables and to use inside of expressions but that's not exactly the constant we're talking about here in this video so what exactly are we gonna be talking about in this video we're gonna be talking about what's known as a symbolic constant and what that is is when we prefix a variable with the keyword Const so now it's not really a variable now is it well it's still usually called a variable so people usually say a constant variable which just kind of sounds dumb when you say it out loud but anyways what we're gonna be doing now is we're gonna be talking about symbolic constants so I think the naming here isn't entirely a hundred percent clear there's a lot of confusion out there so try not to be too uptight with the naming basically when people say constant they could refer to this type of constant they could refer to this type of constant so now that you understand the naming let's talk a little bit about what this means here alright so first thing this creates a read-only variable so what do I mean by read-only because we're actually writing to it here well it's only read-only after declare the variable so here we're assigning a value to it but if we wanted to later change that value to say 10 that's not gonna work we're gonna get a compiling error you can see I scroll up just a little bit you can see it says error can I'll assign to variable X with Const qualified type so what does it mean by Const qualified well any time you hear the word qualified it means to basically per ethics something with a more descriptive term so we're pressing in twit Const this is similar to how we can do something like standard colon colon C out what we're doing is we're qualifying where this object is coming from it's coming from the standard namespace so similar thing when it comes to const we're qualifying the type int with the keyword Const the point is we can't change the variable value now when it comes to the name of a constant there are a lot of conventions out there some people will capitalize the first letter so like if we add xylophone other people will preface with a k so you might see k underscore personally I'm not a huge fan of prefix eing things with a type identifier that's the basis of a type of Hungarian notation which basically is when you embed the type of a variable in the name I'd say it's generally frowned upon and it's not widely accepted to use that type of Hungarian notation so what exactly do I mean by that here's an extreme example we could do something like int int age and set that equal to 5 well we're basically redundantly putting into your so when people use age they know it's an integer this doesn't add a lot of information for one C++ has static type checking so usually when we use a variable if it's of the wrong type we're gonna get an error and two is just extra information we have to type out which makes our variable names longer so I'm not going to go with the k prefix for constants what I'm gonna do is I'm just gonna leave them as is all right so enough jibber-jabber what else do I have to say about constants well their purpose is that we can create a constant and we can use it throughout our code and we don't have to worry about the value being changed I have read online that there are ways to change constant values so I don't know if it 100% foolproof but for most practical purposes we can trust that this value is always going to be 5 the value of this variable will be 5 so we might want to use a value throughout the entire program for example we could have some setting that has a possible value of 3 and then anywhere throughout our code we can get that value one thing you should know is that if you don't assign to it with the declaration you're not going to be able to do it after either so even if we haven't assigned anything here yet if we try to assign it here and compile we still get an error so that is one way to create a constant another way that you might see is up here and it looks something like this pound define and you could create a variable let's say X and give it the value 10 typically you'll see this in all uppercase letters this was the way it was done in C programming and it's also valid in C++ of the two options I would say the previous Const keyword is preferred mainly because you can put a type with it so you can go with this technique if you want I'm gonna prefer the Const technique because this is going to be scoped to a particular block so in this case it's only going to be available to the main function and anytime you can kind of restrict the scope of something it's generally a good thing there is a third type of constants you might see and those are enum constants what you can do is say enum and then put curly braces and inside of here create your constant so what we could do is say Y is equal to 100 for example and this is good because we're not going to be able to change it so if we do Y is equal to 50 we're gonna get an error and you can see it's not letting us reassign that value so when we try to assign another value to Y we get an error so to conclude there are three type of constants we talked about we talked about prefects seeing a type with Const we talked about what's actually known as macros using the defined directive up here and then we talked about enum constants down here which one to choose hmm that's a research project for you most scenarios they're all gonna do basically the same thing just choose one you like and go with it that's all I got for you guys in this video please be sure to check out the next video we're going to talk about something new it's gonna be a lot of fun so please be sure to subscribe and I'll see you in the next video [Music]
Info
Channel: Caleb Curry
Views: 23,728
Rating: undefined out of 5
Keywords: c++, programming, tutorial, tutorial 22, constant, const, macro, enum, caleb, curry, code, coding, beginner, calebthevideomaker2, c (programming language), c++ programming, c++ tutorial
Id: QICx5aZV7sY
Channel Id: undefined
Length: 7min 7sec (427 seconds)
Published: Mon Jan 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.