In this introductory lesson, we'll create our first program in C++!
We will start off very simple. We'll just be outputting some text to the screen.
Things will ramp up quickly, but first, our priority is to make sure we have everything we need to start writing and compiling C++ code.
If you do not have a development environment set up yet, lets start by going through some options we have.
If you do not yet have a code editor, now is the time to get one! There are plenty of free options for every major operating system.
For C++, we'd recommend these:
For Windows, Visual Studio is the common choice. The Community Edition is free and has everything we'll need
For Mac, Xcode is likely the best option. It can be downloaded for free from the App Store.
For Linux, Qt Creator is a commonly recommended choice.
JetBrains also offer editors for C++ that work across all popular operating systems. Their products includes CLion for general purpose C++, as well as Rider that is tailored towards Unreal Engine specifically.
Unlike the above three, JetBrains editors are not free. However, they have free education licenses if you're a student, and a free trials if you're not.
You will often hear the term "IDE" being used to describe the tools we use to build software. All the previous recommendations are IDEs.
In the past, developers needed to use a combination of tools to build software. In modern times, most developers now work in an IDE. These applications combine the most important tools into a single, unified user interface.
The screenshots we show in this course will be from Visual Studio 2022, but it's not that important what software you use. The interface and processes will be very similar, and the code will be entirely identical.
When installing your editor, ensure that you include C++ components during the setup if given the option. This can be done by reviewing the online documentation for your editor.
This course doesn't require anything specific or unusual - the basic ability to compile modern C++ will fulfill our needs.
In Visual Studio, this can be done by selecting one of the C++ Workloads during installation - for example, Desktop Development with C++ or Game Development with C++
If you already have Visual Studio installed without these components, you can configure your installation simply by running the installer again.
Once we've got your editor of choice set up, create a new, blank C++ project with it.
In Visual Studio, this involves creating an Empty Project from the Create a new project window. This window should be accessible from your start screen, or from File > New > Project from the top menu bar.
Once we created the project, we can then add the first C++ file to it.
Other editors may have already included a starting file when it generated the project, in which case this step can be skipped.
In Visual Studio, we can add a file from the top menu bar under Project > Add New Item.
We can call our file anything we want - I've went with main.cpp
In Visual Studio, this will create a new, empty file in our project.
If you're using a different editor, and that editor included a starting file for you, they may also have included some code in that file. If so, delete their starting code.
We want to have our starting code look exactly like what is below:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
}
To ensure everything works, paste this code into your main.cpp
, save your file, build your project, and run the program.
In Visual Studio, building and running the code can be done in a single step, using the Start Without Debugging button on the top menu.
Our program should open a terminal where we see the output of the code - a simple "Hello World!" message.
By the end of this course, you'll understand what every line of code is doing, but for now, we should skip over some things.
Until we introduce those more advanced topics, our code files should always follow the basic structure of the previous example.
Initially, we will limit our changes to just the area between the {
and }
Currently, that line of code is cout << "Hello World!";
.
cout
is an abbreviation of "console out". When we want to write (or "stream") things to the console, we will use cout
.
We stream things to the console using <<
followed by the thing we want to send.
If the thing we want to send is some text, we need to put the text in double quotes, such as "Hello World!"
or "Goodbye!"
.
One of the most common reasons our code doesn't work at this foundational level is that we're using the wrong type of quotes. There are several different options we have. In programming, we generally stick to the "
character.
Finally, we need to end our statement using a semi colon ;
In programming, a statement is an instruction - some action we are commanding the computer to carry out.
In C++, and many other programming languages, a semicolon ;
is used to denote the end of each statement.
We can stream as many things to the console as we want:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
cout << "My Name is Ryan";
cout << "Goodbye";
}
It would be nice if we could separate our messages into new lines. To do that, we have two main options:
"\n"
to the consoleendl
to the consoleAny subsequent output will then be on a new line.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
cout << endl;
cout << "My Name is Ryan";
cout << "\n";
cout << "Goodbye";
cout << endl;
cout << "\n";
cout << "That was much better!";
cout << endl;
}
We can stream multiple things to the terminal in a single statement. This is done by using the <<
operator multiple times within the same statement
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
cout << "My Name is Ryan" << endl;
cout << "Goodbye" << "\n" << endl;
cout << "That was much...." << "easier!" << endl;
}
We can also place the \n
character in the middle of a string of text to take a new line. It does not have to be streamed individually:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello\nWorld!" << endl;
}
Our output is:
Hello
World!
We can output many different types of data. We will see more of this soon, but here's an example of outputting a number. Note, we don't need to include quotes when we use numbers.
#include <iostream>
using namespace std;
int main()
{
cout << 15;
cout << "\nThat's a nice number\n";
cout << "Another number is " << 52 << endl;
}
We should now have confirmed that our development environment is set up. We are able to write code, compile it, run it, and see our output.
It's time to get started with the course!
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way