No history yet

Introduction to C++ Programming

Your First C++ Program

Let's start with the classic "Hello, World!" program. It's a simple tradition, but it shows the basic structure of a C++ application. Think of it as the skeleton that holds everything else together.

// This line includes the input/output stream library
#include <iostream>

// This is the main function where the program starts
int main() {
    // Print "Hello, World!" to the console
    std::cout << "Hello, World!\n"; 

    // Return 0 to indicate the program ran successfully
    return 0; 
}

Every C++ program has a main() function. This is the entry point—the first thing that runs. The #include <iostream> line is a preprocessor directive. It tells the compiler to include the iostream library, which lets us work with input and output, like printing text to the screen using std::cout.

Notice the semicolons ; at the end of most lines. In C++, semicolons are like periods in a sentence; they mark the end of a statement.

Storing Information

To do anything useful, programs need to store information. We do this using variables. A variable is just a named piece of memory that holds a value. But before you can use a variable, you have to declare it, which means giving it a name and a data type.

The data type tells the computer what kind of information the variable will hold. This is important because storing a whole number is different from storing a letter or a number with a decimal point.

Data TypeDescriptionExample
intStores whole numbersint score = 100;
floatStores single-precision floating-point numbersfloat speed = 1.5f;
doubleStores double-precision floating-point numbersdouble pi = 3.14159;
charStores a single characterchar initial = 'A';
boolStores a true or false valuebool isGameOver = false;
std::stringStores a sequence of charactersstd::string name = "Player1";

Here's how you might declare and use some variables:

#include <iostream>
#include <string>

int main() {
    int health = 100;
    float shieldStrength = 55.5f;
    bool hasKey = true;
    std::string playerName = "Alex";

    std::cout << "Player: " << playerName << "\n";
    std::cout << "Health: " << health << "\n";

    return 0;
}

Making Decisions and Repeating Actions

Programs often need to make decisions. Is the player's health zero? If so, the game is over. This logic is handled with control structures, like if-else statements.

int health = 0;

if (health <= 0) {
    std::cout << "Game Over!\n";
} else {
    std::cout << "Keep playing!\n";
}

Sometimes you need to repeat an action. Maybe you want to spawn five enemies or count down from ten. For this, we use loops. The two most common types are for loops and while loops.

A for loop is great when you know exactly how many times you want to repeat something.

// This loop runs 5 times, with i going from 0 to 4
for (int i = 0; i < 5; ++i) {
    std::cout << "Spawning enemy " << i + 1 << "\n";
}

A while loop is useful when you want to repeat an action as long as a certain condition is true.

int bullets = 6;

while (bullets > 0) {
    std::cout << "Fire! " << bullets << " left.\n";
    bullets = bullets - 1; // Decrease the bullet count
}

Organizing Code with Functions

As programs grow, putting all your code inside the main() function becomes messy. Functions let you break your code into smaller, reusable pieces. Each function can perform a specific task.

A function has a return type (what kind of data it sends back), a name, and a list of parameters (the data it needs to do its job).

// This function takes two integers and returns their sum
int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(5, 3); // Call the function
    std::cout << "The result is: " << result << "\n"; // Prints 8
    return 0;
}

Variables declared inside a function are only accessible within that function. This is called scope. A variable's scope is the region of the code where it can be used. This helps prevent different parts of your program from accidentally changing the same variable.

Thinking in Objects

Object-Oriented Programming (OOP) is a way of thinking about programming that's based on real-world objects. Instead of having loose variables and functions, you group related data and functions together into a blueprint called a class.

class

noun

A blueprint for creating objects. A class combines data (variables) and methods (functions) that operate on that data into a single unit.

For example, you could create a Player class. This class would contain data like health and ammo, and functions like takeDamage() and shoot().

class Player {
public:
    // Data members (variables)
    int health;
    int ammo;

    // Member functions (methods)
    void takeDamage(int amount) {
        health -= amount;
    }
};

Once you have the blueprint (the class), you can create actual instances, called objects.

int main() {
    Player player1; // Create a Player object named player1
    player1.health = 100;
    player1.ammo = 50;

    player1.takeDamage(20);
    std::cout << "Player 1 Health: " << player1.health << "\n"; // Prints 80

    return 0;
}

Two other core ideas in OOP are inheritance and polymorphism.

Inheritance lets you create a new class based on an existing one. For example, you could have a general Enemy class. Then, you could create more specific classes like Goblin and Dragon that inherit from Enemy. They would automatically get all the properties of an Enemy (like health) but could also add their own unique features (like breathing fire).

Polymorphism allows objects of different classes to be treated as objects of a common parent class. This means you could have a list of different Enemy types and call a single attack() function on each one, and each enemy would perform its own unique attack. It's a powerful way to write flexible and manageable code.

Learn the fundamental concepts of C++ programming, including basic syntax, control flow, functions, arrays, pointers, dynamic memory allocation, and object-oriented programming principles such as structs and classes.

These are the building blocks of C++. Mastering them is the first step toward building complex games and applications.

Ready to check your understanding?

Quiz Questions 1/6

What is the name of the function that serves as the entry point for every C++ program?

Quiz Questions 2/6

Which preprocessor directive is used to include the library for handling standard input and output, like std::cout?

With these concepts in hand, you have a solid foundation for tackling more advanced topics and eventually, building amazing things in Unreal Engine.