So this time we have come to variables and constants. Probably some of the most important things in any software as you need somewhere to store the input and data your program uses.
This post is a long one, but hang in there and let’s see where this section leads us.
The course can as always be found on Udemy. I just realised I have forgotten to link it before. 🙂

Lecture 43: Section Overview
Yeah, well. Again a quick overview of the section.
I like these overviews because then I have some idea of what we’ll learn and expect from the section, and I don’t go in “totally blind”.
Lecture 44: What is a variable?
A nice little overview of what a variable is, what it does and how it is used.
This might seem like a general overview of variables in general programming, but it’s important to remember that this course goes through how variables are used in C++. C++ is a static programming language which simplified means that you have to declare both the variable AND the type before using it. The instructor does mention this, but it’s easy to miss.
Other programming languages have completely different rules about variables. Just because of the description, I personally have a fondness for duck typed languages. Those languages find out what kind of a variable it is, using the idea of If it walks like a duck. I’m not saying it’s better or worse, I just like the name. 🙂
Lecture 45: Declaring and Initializing Variables
This lecture goes through the rules for declaring, naming and initializing a variable and I think this is the second time the instructor goes into best practices in programming. This is something that I personally am very interested in and I’m keeping my eye out for more of this.
BTW, the first time he talked best practices was when he talked about the using directive. Go back in the course and review it if you don’t remember what he said about it, but in short: Don’t use it! 🙂 And that’s why I use it in my code during this course. 😀
The naming rules are pretty straight forward but it’s also very important to know these rules or you might end up in trouble. For instance it’s easy to think that the names Foo and foo are the same variable, but in C++ they are not.
A standard for naming things you use in C++ is important if you want to make your code clear and readable. The instructor goes through some examples, but there are many style guides out there and if you start working as a programmer somewhere, that company should have a style guide that you need to follow. Google for instance have published their C++ Style Guide on GitHub.
It might be interesting to read through some style guides when you get proficient in C++ and find one you like, but I might hold off on that until I know more C++. I skimmed through Google’s guide, and much of it was confusing as they talked about stuff I don’t know anything about yet.
The lecture ends with some practical examples on how these things work, so start up your CodeLite IDE and play around with what you’ve learned.
I think that the most important thing to remember from this lecture at this moment in the course is that you have to initialize your variable to avoid unexpected results. The compiler helps you with this as it throws up warnings if you’ve forgotten to do this, so check the errors and warnings when you compile your program.

Lecture 46: Global Variables
Pretty short and sweet lecture that discuss the difference between local and global variables. Pretty important to know the difference, as it has some consequences and pitfalls.
The punchline is to use local variables and global only when absolutely needed. Which is pretty seldom, as I’m sure we’ll learn how to pass values between functions pretty soon.
Lecture 47: C++ Built-in Primitive Types
The lecture goes into the fundamental data types that are built into C++. The fun thing in C++ is that there are some differences between the sizes of these types depending on what system you are on.
The instructor goes into all the four main data types and shows tables describing all the intricacies, which is really not that intricate but there are some things you need to be aware of, for instance when doing math.
The last half is a practical demo of the different types using CodeLite and some C++ code. There are some pitfalls you can get into, but if you’re being smart when initializing your variables the compiler will help you by throwing errors or warnings at you.
Lecture 48: What is the Size of a Variable (sizeof)
Remember I told you that the sizes of a data type varies in C++. Happily C++ has a built in operator for showing you the size, naturally called sizeof.
But when other courses only tell you about sizeof, Frank goes into where this operator gets its values from and how you can use those include-files in your own code. If you want to, you can check out my code with some examples. Go get the code on GitHub.
Lecture 49: What is a Constant?
A constant is almost the same as a variable, but their value is constant. Well, duh… 🙂
The instructor goes into why you would use constants instead of variables with some examples. Since C++ also have different types of constants, he shows us some examples of literal, declared and defined constants.
Be aware that in modern C++ you should NOT use defined constants. The replacing of defined constants are done by the preprocessor and since that is pretty dumb and don’t have the error checking capability of the compiler it can lead to errors that might not be easy to spot.
Lecture 50: Declaring and Using Constants
This is a loooong video, but happily it’s a practical example of using constants so nothing to worry about.
The code starts really easy with just one variable and several literal constants. Then he refactors the code to make it easier to read and update. You see the instructor create and run all the code as he writes it, errors and all. I find that to be a nice touch, as the also teaches you things and makes the instructor “more human”.
This section also introduces pseudocode as a way to start your programs. Solve the problem using natural language statements first and then you translate that to C++ code later. It makes it easier to think about the problem and how to solve it than doing it directly in code.

Lecture 51: Section Challenge and Quiz
So, the challenge. This is based on the previous lecture, just a little expanded.
You are running a carpet cleaning service and this program should calculate and give a price estimate to customers. The customers provide the number of small and large rooms and then the program spits out the cost, the tax and total estimate and how long it’s valid.
Pretty easy and straightforward, but a nice little review of what you learned through the section. My solution was a little different than the instructors, but both of them works quite nicely. I use some extra variables in there, so I don’t have to do the same calculation several places. Saves some typing and time.
The quiz is a nice little recap of what the instructor has told us earlier. You have all the info, but I think he only mentioned some of it once, so you need to pay attention to the lectures.
As much as I like the “Good job!” when you answer correctly, I would like some more feedback even when you answer correctly. It might be that you just had a lucky guess and don’t really know why the answer you gave is correct.
The solution and the rest of my code from this section can be found on my GitHub page.
Leave a Reply