CPP Overview

Very Quick CPP Recap - Page 1


A look at primitive data types, operators, type safety, conditional statements, loops and scope

Otter dancing with a fish

You will need to install either Visual Studio Community 19 on the PC with C++ drivers or XCode on the mac.

Why C++

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

  • C++ is a lower level language and is more complex but has greater perfomance. Games are very performance oriented so this is the language of choice for the engine. Often higher level scripting languages are used for game play scripting. Unity game engine is written in c++ but you write all the game scripts in c#.
  • Manual memory management which avoids any issues with higher level languages not freeing up memory, or freeing up so much that it affects the performance of the game.
  • Easier to optimize heavily used routines
  • There is a C++ compiler available for all platforms, so is beneficial if you want to support PC's, consoles and mobile.
  • Many libaries such as Havok or Scaleform are written in C++
  • It is used extensively in lower level systems like rendering, audio and physics
  • C++ is a compiled programming language
  • We will be using C++ 14 as this is supported by Unreal.

    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

1. Hello World

Get our sdk up and compile and run the simplest string

1-1

Visual Studio Installer

If you are on a mac and NOT windows 10 then go to step 1-6. The C++ compiler does not install by default. So, run Visual Studio Installer and make sure that under Modify that the C++ compilers are ready to go.


1-2

Run VS 19

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:


1-3

Console App

Select a Console App as we will only be working withing the console.


1-4

Name Project

Name the project CPP-Overview and select a location to save it in and press the Create button.


1-5

Hello World Default

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 " is a string with a new line () 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.


1-6

XCODE

If you are on the mac run XCode. Select Create a new Xcode project.


1-7

Command Line

Select a macOS | Command Line Tool project.


1-8

Save

Select the folder you want to save to


1-9

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 Mac which we are working on now.

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

return 0 is not required but is a common way to end a main cpp file so that when the program terminates it returns '0'. If we never get this return in the console we know that the program crashed or stopped.

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.


1-10

Clean Up Code

We will continue with Visual Studio screenshots but it will all also apply to XCode. Now clean up the comments (everything with a // before it), add a starting comment and add a return 0 if it is missing. Now run the game by selecting the run button with Local Windows Debugger selected or just press the F5 button.


1-11

Run Console

So the end result is in the console should look something like:


1-12

printf

There are always more than one way to skin a cat, so we can also call a c 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::.


1-13

printf() Definition

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. <iostream> includes the function printf as well as all std:: methods.


1-14

Main

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.


1-15

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.


C++ is a Compiled Language

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). You can a diagram here