Fundamentals of C++
The course is part of this learning path
This course provides you with a solid understanding of the fundamentals of C++. We will take a look at the components of the programming language and then put these into practice through a couple of projects that we will run through at the end of the course.
- Learn how to store different types of data in main memory
- Understand how to manipulate and perform operations on that data, including performing arithmetic on numbers
- Understand how programs make decisions
- Learn how you can write your programs to communicate with users
- Beginner coders, new to C++
- Developers looking to upskill by adding C++ to their CV
- Experienced C++ programmers who want to stay sharp!
- College students and anyone studying C++
This is a beginner-level course and so no prior knowledge of C++ is necessary.
Earlier, we learned about variables and the values that they hold. We learned that when you literally type out an integer like 150, for example, that this is called a literal value or just a literal. Well, it also has another name, a literal constant. It is constant because we can never change the value of a literal. For example, I can't change 150 to a different value. I can't say 150 = 200 because it's not a variable. The other type of constant is what this lecture is about. A constant that you give a name just like a variable is called a symbolic constant. You might hear developers still refer to them as variables, which they are in the sense that they are a symbolic name that refers to a block of memory that holds a value. But they are different because once the value is assigned to them, you cannot change it i.e. their value remains constant. In order to create a symbolic constant, you declare it the same way that you do a variable but with one little addition to the declaration, the const keyword. Also, you should set the value immediately at the same time as when you do the declaration. So, let's create a project called ConstantFun. That's a project, Empty Project and ConstantFun. This is a really good name and it has a double meaning, because not only is it fun with constants, but I'm also sure that this course is certainly a constant source of fun and excitement. Okay, bad joke. Wait, who am I kidding? That was an awesome joke. Anyway, let's keep going. So, we're going to create the main source file by right-clicking Source Files, going to Add, New Item. And then as always, changing our little name down there to main.cpp. And now, I will write our skeleton code. So, all right. Inside of main, we're going to create a constant double named MY_PI, and set it to 3.14159. All right. So, notice that if I try to set this value again later on, if I try to change its value using an assignment operation, I will get a compiler error. So, let's take a look at that. If I set MY_PI = 3.14159, you'll notice it underlines it and tells me there's an error. It says expression must be a modifiable lvalue. Now that might be a little confusing, but all that means is lvalue means something that goes on the left side of an assignment operation that can be modified. So, that's what the modifiable means. So, this would be the rvalue because it's on the right side, this is what they're calling the lvalue because it's on the left. Now, when we run applications, we've typically been going to Debug, Start Without Debugging. But just to show you you could actually be doing this. Before you try running, you could go to build solution. And if there's an error, you'll notice that it will list your errors down here in the error list. So, expression must be a modifiable lvalue. MY_PI, you cannot assign to a variable that is const. So, just be aware of that. Most of the time we've just been able to do Debug, Start Without Debugging, but you'll notice if I try that it will say there were build errors. Do you want to run the last successful build? And I'll say no. Then I can look again and see the different errors down in the error list. So, I can't change the value of a constant because even though I've given it a name unlike a literal constant, it behaves just like a literal constant. The symbolic constant cannot have its value changed, just like you can't change 150's value to 200. So, let's have a quick side discussion here related to naming your variables because you'll notice this might be a little bit different than what you're expecting. So, we get rid of the offending code for a minute here, and I'll just recompile it just to prove that it does compile. You'll notice that just during the building, it does say that the build has succeeded. Right down here, I'll make that I like that. So, earlier I mentioned that variables by convention should typically use camel case or camel casing in C++, where we start the variable name with a lowercase letter and then continue capitalizing only the beginnings of new words or partial words within the variable identifier. For example, if I had int myTaxRate, we'll say equals 4 or 4% or whatever. You'll notice that this right here starts with the lowercase m, uppercase T, uppercase R, because it looks like the humps of a camel. Now, that's camel casing. This right here is clearly not camel casing. So, with constants, we typically follow a different naming convention. For constants, we use something called capitalized snake case or simply just capitalized underscore case. So, that's why MY_PI is all capitalized and has an underlying or an underscore rather between the MY and the PI. If we made them all capitalized and didn't use the underscore, it would cram together just like using all lowercase, so we didn't want that. So, the underscore helps separate words, since we don't have the benefit of the upper lowercase alternation that we do with camel casing. So, now you know both camel casing and snake casing. These are again only conventions meaning they aren't syntax rules. You could switch these they would still keep the values, and you can still make constants that don't use this convention. You can make variables that don't use this convention as long as they're valid identifiers. However, they are conventions, and they are followed pretty heavily in a lot of industry. So, as a short challenge, I would like you to add to your current source file another constant. This time I want you to make it a constant string named MY_NAME, with which you should use capitalized snake casing. Try changing its value to demonstrate that it won't change, and then try printing it out. Do not forget to include the string library before using strings. So, pause the video and come back when you're done or if you need some help. I hope you were able to accomplish this little challenge. Let's do it together to make sure we're all on the same page. I can get rid of that little integer that I was working with, and we're going to say const string. We should include the string library just to be appropriate, and make sure it works in all compilation environments. We'll say string MY_NAME = "John". And then if I try changing it to Bob, you will notice that it doesn't let me; I get an error, right? So, if I try building it, I'm going to have a problem. No operator equals matches these operands. And the reason this time is because this one is a constant and this one is the string it's saying I can't, I don't know how to perform that operation. And you also get the no operator found which takes a left-hand operand of type "const std:: string", so there we go. Now, we can try to write code to print it out, so we'll just do this right there. And we will start without debugging, make sure it builds. And we do in fact get John printed out. Awesome job. We've learned a lot in this section, and a couple slightly more difficult projects are just on the horizon. So, we have a little more to do because although we have been frequently printing values to the console using cout and the stream insertion operator, we have not taken input from the user. We have simply modified data from within our code, which is called hard coding, which, of course, is not something we would expect our users to be able to do in most cases. We don't want them and we don't expect them to know how to modify our code. So, in the next section, we will learn how to get data from outside our application from the standard input stream using CN and the stream extraction operator. So, I'll see you in the next lecture.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.