Saying Hello to C++

The course is part of this learning path

Saying Hello to C++
2h 17m

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.

Learning Objectives

  • 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

Intended Audience

  • 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.



In this lecture, we will be writing a very simple program to generate output that the user of your program will be able to see. This will give us a foundation upon which we will build much more complex user interaction in future lectures. So, here on Visual Studio 2019, I'm going to click 'Create a new project.' Make sure that you have C++ set as the language and then you select 'Empty project.' And then click 'Next'. Here I'm going to ensure that the name of the project is 'HelloWorld'. Notice there's no space on the name. Although, I could put a space in the name of the project, it's a common convention not to do so. A convention is just something that is typically done by professionals. Then I will click 'Create'. So, with this new project over here under source files, I'm going to 'right' click here and go to 'Add' and then 'New item.' And then I'm going to add my CPP file right here. You can choose what name to give it. I typically change it to main.cpp although you could leave it as source.cpp and that would be fine. Then I'll click 'Add'. The source code is just the C++ that we write. The code we're about to write might look very foreign if you're new to programming. Don't worry at all though, because you'll get the hang of it once you've been doing it for a while. We will write the main function which acts as the entry point to our application. The entry point is where the program starts. So, here's our basic code, which I'll explain in just a moment. Let me explain a little more about this code. You'll notice the pound include iostream, or hashtag if you're from the twitter generation that I've added. This is called an include preprocessor directive; which is essentially just instructions for a part of the C++ system called the preprocessor. It tells it to include the contents of a library in this case called iostream, which we'll talk about in just a moment. The preprocessor does any work that is needed before another part of the C++ system called the compiler converts our source code written in C++ to machine code that the computer can understand. So, that it can run our program. Computers don't understand languages like C++ or java or other programming languages directly. So, this code often has to be converted into machine code so that the computer can understand it. And that is what the compiler is responsible for. Another term that is very important to our discussion is library; which is a collection of various objects, functions, templates and other things that help us get our jobs done as programmers. Don't worry if you don't know what objects, functions, or templates are; we will learn about them much more thoroughly later in the course. For now, just know that these are things that help us write code that do rather complicated tasks with very little effort on our part. Because much of the complex coding has been done for us and provided for us in the form of these libraries. So, specifically the library that we've included up here is iostream. The io in iostream stands for input/output stream. It allows us to interact with the user printing information to this thing called the console, which we'll talk about in just a bit. It also allows us to take in input from the user from devices such as the keyboard. So, back to our code as a whole. If we were to run this code, it wouldn't do much of anything, or at least you wouldn't see it do much of anything. That's because it just loads the main function, which is the entry point of the application and runs it. And then the main function returns the value 0, which is just a signal to the system that nothing went wrong. However, if we want to see something happen, we need to have some sort of information displayed to us when we run the program To do that, we will learn about two new pieces of syntax, which again means grammar rules of the language. So, I will write the cout object but I first have to prepend it with this thing called std and then :: which is the scope resolution operator. This says that the cout object belongs in this thing called the standard name space. A name space really is just like the name suggests a space where different names live and it's to keep conflict from occurring. So, someone might have one name space and they can have their own cout object and the standard name space has its own. So, that just means that if you prepend it, you're saying I'm using the one that I'm talking about from the standard name space. Now hopefully we won't have naming conflicts very often, but that is what we have to do in C++ in order for it to work properly. So, the cout stands for character output or sometimes just called console output. The console is the default place that the cout objects sends data to. The stream insertion operator, which looks like two less than signs; works with objects like cout to send character data to the console. Notice that these less than signs are actually really just arrows and they're pointing towards the cout object name. This is the direction of the flow of the data. The data flows from the right of the stream insertion operator and into the console ultimately. Note, also that you can use the stream insertion operator over and over again, even in the same line to separate various variables and liberals which we'll see right now. It's right here I am writing this 'HelloWorld'. And then I have again the standard name space indicated with the scope resolution operator and a variable called endl. This right here "Hello World" and the double quotes is called a literal. Specifically this is called a literal constant. And to be even more specific, this is called a string literal constant. We will cover strings much more thoroughly later in the course but for right now you can think of it as just a string of characters or string of these letters and spaces and things. So, that's what we have right here. The stream insertion operator separates the cout object from this string literal right here and then also from the endl object right at the bottom. So, let's run this without debugging and see what happens. So, to run this I will go to 'Debug', 'Start Without Debugging' and now we will finally meet this console I keep talking about. So, this is the console, it is text-based. It doesn't generally take mouse input. It's mostly for keyboard input and of course console output. So, we see our program has generated Hello World and then a new line and then this path right here. And it does say exited with code 0 at the end of this. So, that means that it succeeded. And then there's press any key to close this window. So, the last two lines are actually generated by Visual Studio. The only output that's really generated by our program is Hello world and then the new line right here. So, that's what the endl does is generates a new line and puts that into the output stream. Pretty exciting. This is obviously just the first step on a long journey. Note that we have to keep writing this std followed by two colons before the c:out and the endl. That can get a little bit tedious and redundant after a while. So, there is actually a shortcut that we can use heavily throughout this course that will keep us from having to write this. So, at the very top, I can tell it what is the default name space or what is one of the name spaces that I'm using and in this case it's using name space standard. If I do that, then I don't have to write standard followed by the scope resolution operator. So, let's run it again just to make sure we didn't break anything. I'm going to go again to 'Debug', 'Start without debugging'. It will automatically call the compiler and convert our code into machine code and then it will run the application. So, here's our console again. And again we get Hello World followed by a new line generated by the endl and then the data that's printed out from Visual Studio. Brilliant, we have our output again, it does the exact same thing but the code is much more concise. One last question I'd like to answer is, where does cout come from anyway? And the answer is: the iostream library that you have included at the top. The include directive includes the iostream library so that we have access to among other things, the variable. Now before we move on to another lecture, I want to give you a little challenge. I want you to replace the string literal that currently contains Hello World with Hello followed by a space and then followed by your name. So, in my case it would print out Hello John. So, I want you to pause the video, give it a shot, and come back when you've done it or if you get stuck. Okay, I hope you were able to accomplish that task. Let's do it together, so we can make sure that you know how. So, I'm going to change this from Hello World to Hello John. And let's run it again. So, we're going to go to 'Debug, 'Start Without Debugging'. It will build the application, and if there are no errors, it will just run. So, it says Hello John now, followed by again a new line. I hope you were able to get that to work. If so, nice job. If not, it's not that big of a deal, we will learn as we go along and you will get much better. So wow, we've learned a lot this lecture. We wrote our first source code in C++ and then compiled and ran the program to see the output that it produced. We learned the basics of the using directive, which makes our lives easier when we want to use constructs such as cout and endl. Being able to communicate with the users of your application is very important, and we just took our first steps in doing so in C++. That's it for this lecture. I'll see you in the next lecture, where we will learn about data types and variables.

About the Author
Learning Paths

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.

Covered Topics