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.
So, we've already seen two important numeric data types: int and double, in the previous lecture. Now we'll consider more useful data types that represent basic textual data. We will use one of the primitive types, char, that is C-H-A-R. As well as a class type called string, S-T-R-I-N-G. We will talk more about the distinction between primitive and class types later in this course. For now, you just have to know that primitive types are relatively simple and are often used to help build more complex types. And class types themselves are typically much more complex and provide a lot of interesting operations that can be performed on them. First, let's create a new project called TextFun. We're going to select Empty Project. Make sure we are in C++ as we always do. Hit 'Next' and we will call it TextFun. We again make sure that the location is a folder that we want it to be in and that we can remember. In this case, this is fine. This is where I want it. So, I will click 'Create'. Now we're going to again, add a C++ source file by right clicking Source Files, going to Add, and then New Item. Then, we make sure that we have C++ file selected and I'm going to change it to main.cpp again and click 'Add' or hit 'Enter' on the keyboard. Once I've done this, I'm going to add the skeleton program like we did before. Don't forget of using directive and our iostreams so we can do input and output. And we're also going to have our entry point to the application name. Note also as an aside, that we can really put this open curly brace on the same line as main or we can put it on the next line. It really is just a matter of preference. We'll keep it on the next line this time. So, inside main, let's create a char variable named singleChar; char singleChar. And we're going to assign it the value 'a'. Note that the character variable can only hold a single character inside single quotation marks. This is a character literal. This might seem limiting, but characters are the foundation for more complex data types like the class string that we will see in just a moment. Let's write the code to print the character to the console and run our application. Let's cout singleChar and endl. And again notice we don't have to put std followed by the scope resolution operator, the two colons, because we have put using namespace standard at the top. So, let's run this. When we go to Debug, Start Without Debugging, it automatically compiles. And then it also launches our code here. So, now it's just printing out the character 'a'. And of course, the stuff that Visual Studio prints, a path to the executable and return value of zero indicating there were no problems, and also the obligatory press any key to close this window. Excellent job. And I know a lot of this might seem redundant; what we're doing. But we're exploring different data types and slowly becoming more and more confident with our use of the cout object and stream insertion operator, along also with the endl variable. The only way we will get better at coding is to do it repeatedly until it becomes part of us, part of our memory, part of our muscle memory, that is until it becomes like second nature. Just like we saw before learning a natural language like English or Spanish, Hindi, or German, the more we immerse ourselves in it, the better we will become. And I promise the code will get more and more interesting as we go along. Now that we are comfortable with the basic character data type, let's use the data type meant to hold a string of characters, which is called the string data type. Since the string data type is not part of the C++ language itself, we need to include a library just like we did with iostream. So, below iostream, add an include directive to include the string library. And remember, just like libraries, you might have in your own school, university, or community, they give our program access to resources that we wouldn't have access to without them. So, here we go. There we go. Now we can use our string class as a data type. Inside main, let's declare a variable called myName of type string and initialize it with the value of John. So, right here under our char, I'm going to write a string called myName, and we're going to store the value John in it. So, as a side note, to link this with our previous discussion about the standard namespace, if we didn't have the using directive in this program right here, then we would have to put std:: in front of the string as well. Now, let's write the code to print it to the console and run the application. Cout << myName << endl. I'm going to go to Debug, Start Without Debugging, and it will build our application automatically and then run the application. As expected, we have an a right here from our character and then the string John being printed to the console. You'll notice just like the Hello World that we had before, I have double quotes around the string literal John, but this time I'm storing it in a string variable named myName. So, you'll also note if I want to say Hello John, that I can do that by putting a string literal here, Hello followed by space. And then I'm going to use the stream insertion operator again to separate this literal from the variable and then from another variable here, and all of those will be sending the data into cout. So, we can use as many of these stream insertion operators to separate variables and literals from one another as we need. So, let's run this and see what happens. Debug, Start Without Debugging, and now it says Hello John. So, we did that before but entirely as a string literal. Now we have the string variable with the extra space in here because computers are very, very, very literal in the sense that they will only do what you tell them to do. And in this case, if I didn't put the space there, the Hello and the value of myName, the John, right now would be crammed right next to each other. We don't want that. Now there's a tiny little exercise for you. I'd like you to pause the video and get the code to print out Hello but followed by your name instead of mine just like we did before, but this time you're using a string variable. When you're done, unpause it and let's see what we can do together. All right. Since my name is already John, I'm going to change the value of the variable name myName to Rob, instead, and let's see what happens. So, put Robin here, and we will Debug, Start Without Debugging, and it now says, of course, the character because we're still printing that out, and then Hello Rob. Awesome. You're doing a great job. So, up until now we've covered numeric types and learned to work with basic textual data like characters and strings, and practiced printing lots and lots of data to the console. Hopefully, it's becoming a little more comfortable. But if you're still having a hard time or don't understand a lot of what's going on, don't worry. We will keep practicing until we get better at it. Up next, we will look at another very important data type that is used extensively to help our programs make decisions under 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.