C++

C++ is a mid-level general purpose programming language based on C. It supports object-oriented programming and imperative programming. Currently, C++ is the most popular programming language for gamedev since it's compatible with most C libraries and supports object-oriented programming. However, while C++ is a very popular language, there are many people who hate it due to so many bloated features added over the years that make it difficult to fully learn and make compiling times slower.

Like C, C++ also uses the same naming scheme for its versions which is the last two digits of the year when it was released. So, C++17 (released in 2017) is newer than C++98 (released in 1998).

Pros and Cons

Pros

  • Widely used and an industry standard
  • Wide amount of libraries, APIs, frameworks, and engines
  • Wide amount of features
  • Mostly backwards compatible with C
  • Almost as fast as C
  • Multi-paradigm support
  • Compiles directly to machine code
  • Runs and compiles on most operating systems and hardware
  • Is still being updated. The latest update was C++20.

Cons

  • No built-in memory management
  • Archaic OOP system compared to other programming languages
  • Functions are not first class types
  • Not beginner friendly and not idiot proof - if you have warnings disabled on your compiler, it's easy to shoot yourself in the foot
  • Too many bloated and unnecessary features added over the years, making it difficult to fully learn
  • Slow compiling and linking times
  • Graphics are not part of the C++ specification, and therefore have different implementations on different systems.

Supported tools

Since C++ is an industry standard, most major operating systems, hardware, and libraries support it and since C++ is mostly backwards compatible with C, it's possible to use C libraries and APIs like Win32 and OpenGL with it.

Libraries

Frameworks

Engines

Resources

Tutorials

IDEs

Compilers

Text Editors

Sample code

Hello World

Using cout:

#include <iostream> // library containing cout
 
using namespace std; // namespace containing cout
 
int main() {
    cout << "Hello, world" << endl;
    return 0;
}

Using printf():
#include <stdio.h>
 
int main() {
    printf("Hello, world\n");
    return 0;
}

Fizzbuzz

#include <iostream>
 
using namespace std;
 
int main() {
    for( int x = 1; x <= 100; x++ ) {
        if(x%3 == 0 && x%5 == 0) {
            cout << "fizzbuzz" << endl;
        } else if(x%3 == 0) {
            cout << "fizz" << endl;
        } else if(x%5 == 0) {
            cout << "buzz" << endl;
        } else {
            cout << x << endl;
        }
    }
    return 0;
}

Compilation

Compilation is the process of taking the code you wrote and translating it into something the computer understands.

Windows

If you use an IDE then the process will usually be as simple as clicking a button or hitting a keyboard shortcut. IDEs generally have a built-in compiler so you don't need to download one.

You don't need an IDE to program, however. Many programmers use a text editor like Notepad++ and compile using the console. If you are using a command-line compiler, you must open a console, navigate to your source file, and then type something like the following (depending on the compiler you installed):

g++ input.cpp  -o output.exe

The GNU Compiler Collection (GCC) is probably the best free compiler out there, it's constantly being updated and supports operating systems and hardware you didn't know exists. The catch is that they don't provide binaries, only source code. You will want a pre-compiled binary. MinGW is probably your best option. MinGW is basically a Windows port of GCC, with a few extra goodies like threading. Once you install it you should be able to invoke the compiler using the above command. Be aware that installing 64-bit MinGW will only allow you to compile 64-bit programs. If you are planning on developing software that can run on older computers, you should probably use the 32-bit version.

Linux

Most Linux distros (and most other Unix-like operating systems) come bundled with GCC and therefore you do not normally need to install it. If it isn't installed, it can usually be found using a package manager. Invoking the compiler is similar to Windows:

g++ input.cpp  -o output.out

File extensions are optional in Linux, however .out is traditionally used.

Linking

If you are using any libraries or frameworks, you will need to link the library files when compiling your program. If you are using an IDE, there is usually a menu where you can select which libraries to link to. However, if you are just using a text editor and compiler, you need to link the libraries manually. In GCC, the libraries are added at the end of the arguments:

g++ input.cpp -o output.exe -o -library1 -library2 -library3

Tips for Newbies

Obey Warnings

You should compile your code with all warnings using the flag -Wall. It's a hard habit to get into, but will make you a better programmer in the long run. You can disable warnings about unused variables with the flags -Wno-unused-variable -Wno-unused-parameter -Wno-unused-but-set-variable. Any other warnings are best heeded, and can break your program if you ignore them.

Common Mistakes

Here's a piece of code with a lot of common mistakes in it. The code is designed to take two points where P1 is (x1,y1) and P2 is (x2,y2) and return the slope of a line drawn between these two points:

unsigned int getSlope (unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
{
   return (y2-y1)/(x2-x1)
}

Don't forget the semicolon
It is very common for newbies to forget the ; at the end of a statement. With practice you will learn to immediately notice any code that looks incorrect.

Return the correct datatype
If you are returning a slope, it is a decimal value. If you return an integer, it will be rounded down (the computer will throw away the numbers after the decimal, called the mantissa). You should return a double or float.

Beware of integer overflow
What happens if you subtract 1 from 0 with an unsigned variable? The variable will wrap around to its maximum value. In the case of an unsigned int it will become 4,294,967,295. That will cause some problems with your program if you aren't expecting it. What happens if you try to find the slope of the points (4,10) and (5,5)? The calculation becomes (5-10)/(5-4). If you're using unsigned integers, 5-10 becomes 4,294,967,291. So you end up returning a slope of 4,294,967,291. Generally for these reasons it's best to stick with signed integers.

Never divide by zero
Not just a meme. If you divide by 0 in C++, your program will crash. Even if your program is the flight control system on a space ship. Your game isn't that important but it will still crash. What happens if you try to find the slope of points (5,8) and (5,10)? The slope calculation becomes (10-8)/(5-5). This becomes 2/0. That's a division by zero, and your application will crash to desktop. Whenever you divide a number by a variable you must account for this.

Beware of typecasting
When you divide an integer by another integer. The result will be converted to an integer. You must override this behaviour by specifying one of the values to be a double or float. Another thing to look out for is typecasting a character into an integer or vice versa as setting an integer to a character will set it to its numeric value. For example, setting an integer as 'A' will set it to 65.

Here's the correct code:

double getSlope (int x1, int y1, int x2, int y2)
{
   if (x2=x1) { return 0; }
   return (double)(y2-y1)/(x2-x1);
}

Oh wait, no that's not correct.

Use == for comparison, = for assignment
This is a very common mistake among newbies. And here's the thing. If you have warnings disabled, that code will compile fine. It's actually valid C++ code. The expression (y2=y1) will return y1. If y1 is equal to 0, it will be converted to false. Any other number is converted to true, thus creating a logic error. In most other languages like C# or Java, the code wouldn't compile. This is what they mean when they say that C++ lets you shoot yourself in the foot.

Here's the real code:

double getSlope (const int x1, const int y1, const int x2, const int y2)
{
   if (x2==x1) { return 0; }
   return (double)(y2-y1)/(x2-x1);
}

Would you like to know more? Read this, enjoy.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License