C++ Overview - Page 1


Why C++

Out of all the modern programming languages why do we still use C++ for games?

Modern C++ Language Syntax Unreal Engine is built to be massively portable to many C++ compilers, so we are careful to use features that are compatible with the compilers we might be supporting. Sometimes features are so useful that we will wrap them up in macros and use them pervasively. However, we usually wait until all of the compilers we might be supporting are up to the latest standard.

We are using many C++14 language features that seem to be well-supported across modern compilers, such as range-based-for, move semantics and lambdas with capture initializers. In some cases, we can wrap up usage of these features in preprocessor conditionals (such as rvalue references in containers). However, we might decide to avoid certain language features entirely, until we are confident we won’t be surprised by the appearance of a new platform appearing that can’t digest the syntax.

Unless specified below, as a modern C++ compiler feature we are supporting, you should not use compiler-specific language features unless they are wrapped in preprocessor macros or conditionals and used sparingly. - UE4 Documentation

Hello World

  1. Open up Visual Studio 19 Community and you should be able to login with your LSU credentials. You should see a screen like this. Click on the Create a New Project button:
Screenshot of Microsoft's Visual Studio Community website page

  1. Select a Console App as we will only be working withing the console.
Screenshot of Microsoft's Visual Studio Community website page demonstrating community version that is needed download

  1. Name the project and select a location to save it in and press the Create button.
Screenshot of Microsoft's Visual Studio Community website page demonstrating community version that is needed download

  1. You should get a stubbed in project that prints “Hello World” to console. First lets look at the elements. We include the <iostream> libraries. This includes the global object std::cout that controls the output stream buffer. This allows us to access the stream that eventually gets sent to the console.

    std is the namespace that represents the word 'standard' which are built in C++ libraries available on all platforms including Windows which we are working on now.

    "Hello World\n" is a string with a new line (\n) which gets stored as a single character.

    All lines that begin with // are comments and are not compiled into the final project. These are here for you to read and explain to yourself and other developers the intent of what you are trying to do.
A screenshot of the default www.onlinegdb.com webpage with C++14 selected

  1. Now clean up the comments (everything with a // before it). Now run the game by selecting the run button with Local Windows Debugger selected or just press the F5 button.
Running the default www.onlinegdb.com webpage with C++14 selected

  1. So the end result is in the console should look something like:
Highlighting a commented block of code in C++14

  1. There are always more than one way to skin a cat, so we can also call a function called printf and pass it the “Hello World” string as a parameter. Run it by pressing the green run button or F5 and notice that it should have the same end result. Notice that this is in global name space and we did not need to include std::.
Highlights the included file <stdio.h> in c++14

  1. Go to a web browser and click on the link. Notice that it includes a function called printf (we will explain functions in a future lesson). Without libraries and built in functions, the language is fairly limited. Anytime we need to do anything specialized for a specific OS and system, we probably need to load a set of libraries.
Screenshot of cplusplus.com's definition of the printf function

  1. Every C++ program starts by calling a function called main. We know if is a function as it is a name followed by () parenthesis. It then runs everything between the following curly braces {....}. It executes them in order line by line, 13 through 15 in my case.
Highlights the main(){...} function in a C++14 program

  1. So the first thing the program does in main() is to run the function printf and passes a string parameter of “Hello World”. We will get into this more shortly when we dive into strings.
Highlights the printf function in a C++14 program

  1. What this line does is print the string passed to it to the console. In this case it prints Hello World.
Shows console output of Hello World in C++14 console output

  1. It is customary to add return 0 to the end but should work without it on most platforms. This function returns to its caller (we don’t call it) a 0. So if the program runs and doesn’t crash it should finish with an exit code of 0. Look at the second line of the Debug Console window and you will see exited with code 0. This means the program completed without crashing.
Highlights return 0 in C++14 program

  1. When you press run the program is compiled. What does this mean? This is the process of going from a human readable form script and creates object code that forms an executable (an .exe on a PC). So the compiler turns it from words into zeros and ones (machine code).
Illustration of a program that goes from a source file to machine code

  1. In Unreal we will be using c style printing like we do here. But lets go back to printing with std::cout. Lets look at the iostream libraries that includes 4 other libraries and gets us access to an Object called std::cout (standard output). We will be getting into objects later on. But we can call the Object cout and pipe it into an output stream.
Screenshot of cppreference.com's difinition of standard library header \<iostream\>

  1. We do this by adding the line cout << "My name is Marc!"; replacing the call to the printf() function. Press the Run button. One of the thing the compiler does is sends an error intead of running the program. This means it did not compile and create a new executable. In this case, it shows an error that cout is not in scope and is undefined.
Highlight the line cout <<  "My Name is Marc " in a C++14 program

  1. Now lets add std:: before the cout so that it will be in scope. We will go into more detail later on describing this. Press run and you should see it print the new message we typed. cout is an abbreviation for character output stream. Notice that we end the line with a ; semicolon. Every line is a statement and has to be terminated by a semicolon. The compiler needs to know where one statement ends and the next begins.

    Now the << operator inserts the data that follows it into an output stream (which in our case will be the console displayed on the monitor).
Create new sprite with button

  1. If we remove the semi-colon and try and run the program the compiler will give us an error when we press run (when we run it, the program is compiled and it tries to run it). Try this and read the error. Sometimes the error messages are clear and sometimes they are hard to read and understand. This is a compiler error. In a script any spelling mistake or typing error will result in a program error of some sort.
Add ste:: before cout in C++14 program

  1. Put the semi-colon back and now mispell the file we are including. This will generate another error saying it can’t find the file. It also puts a red squiggly line under the errors. Notice that since it can’t load the libraries <iostream> it no longer knows what std::cout means and throws a second error.
Mispell the included file and see compiler error in a C++14 program

  1. Spell <iostream> correctly and remove one of the parenthesis from the stream and press run. Look at the error printed.
Remove a single parenthesis from a statement and see compiler error in a C++14 program

  1. Put the parenthesis back and mispell the return type before the funciont int main() to interest main(). We will get into functions later on. This int is a data type and we will get into this more on the next page.

    Fix the spelling and get the program to compile and run again.
Mispell the return type from a function and see compiler error in a C++14 program

Home    Continue ->