C++ Example Tutorial – Complete Guide

Welcome to our exploration of C++. This powerful, highly flexible programming language is fundamental to game development, system programming, and many other contemporary computer science fields. Whether you’re just beginning your coding journey or have some experience under your belt, this guide seeks to illuminate and inspire with engaging, practical examples in C++.

What is C++?

C++ is a high-level programming language developed as an enhancement of the C language. It encompasses procedural, object-oriented, and generic programming features, making it enormously versatile and applicable across numerous development domains.

What is it used for?

C++ is renowned for its performance and efficiency. Consequently, it’s widely used in the creation of software requiring ‘tactile’ real-time response such as games. Furthermore, you’ll often find C++ humming in the engine rooms of operating and embedded systems.

Why should you learn C++?

Mastering C++ is a ticket to understanding more about how software interacts with hardware. As such, it’s an invaluable addition to the toolset of coders seeking a deeper understanding of the digital world. Furthermore, as a universally recognized language, C++ skills are highly coveted, broadening your horizons for career opportunities.

OK, enough words! Time to get our hands dirty…

CTA Small Image




Getting Started with C++

So let’s start coding! Here is a simple example of a classic “Hello, World!” program in C++:


int main() {
    std::cout << "Hello, World!";
    return 0;

In this code:

  • #include is a preprocessor command that includes the iostream library to the program. This library allows us to perform input/output operations.
  • int main() is the main function where our program begins execution.
  • std::cout is the standard character output device. It corresponds to the output screen. Here, we are using it to output a message “Hello, World!”.
  • return 0; ends the main function.

Data Types in C++

Data types are used to specify the type and size of information that a variable can store. Here are some basic data types in C++:

int anInteger = 10; // Integer
double aDouble = 9.99; // Floating point number
char aChar = 'A'; // Character
bool aBool = true; // Boolean
string aString = "Hello"; // String

Operators in C++

Operators are symbols that perform operations on operands. Here are some common C++ operators:

int x = 10, y = 4;

// Arithmatic operators
std::cout << x + y; // Outputs 14
std::cout << x - y; // Outputs 6
std::cout << x * y; // Outputs 40
std::cout << x / y; // Outputs 2

// Relation operators
std::cout << (x == y); // Outputs 0 (false)
std::cout << (x != y); // Outputs 1 (true)
std::cout < y);  // Outputs 1 (true)
std::cout << (x < y);  // Outputs 0 (false)

If-Else Statement in C++

If-Else statement is used to perform different actions based on different conditions. Here is an example:

int x = 10;

if(x > 5) {
    std::cout << "x is greater than 5";
} else {
    std::cout << "x is not greater than 5";

This will output “x is greater than 5” because the given condition (x > 5) is true.

Looping in C++

Loops are programming structures that repeat a block of code until a certain condition is met. There are three types of loops in C++: for loop, while loop, and do-while loop.

For Loop

A for loop repeats a block of code a specific number of times. Here is an example:

for(int i = 0; i < 5; i++) {
    std::cout << "Hello, World!\n";

This will output “Hello, World!” five times.

While Loop

A while loop continues to execute a block of code as long as a particular condition is true. Here’s how to use a while loop to achieve the same result as the for loop example above:

int i = 0;
while(i < 5) {
    std::cout << "Hello, World!\n";

Do-While Loop

A do-while loop is similar to a while loop but with one critical difference: the loop conditions are tested after the loop is executed. This guarantees that the block of code is executed at least once. Let’s continue with our “Hello, World!” example:

int i = 0;
do {
    std::cout << "Hello, World!\n";
} while (i < 5);

Functions in C++

Functions are blocks of code designed to perform a particular task. A C++ function is defined with the keyword void, followed by a user-defined name. Let’s create a simple function to print “Hello, World!”:

void sayHello() {
    std::cout << "Hello, World!";

int main() {
    return 0;

This code defines a function named sayHello(), which outputs “Hello, World!” when called. The call to this function is made in the main() function.


These basic examples only scratch the surface of what C++ can do. As a highly versatile and powerful language, it is widely used and provides a solid foundation for any professional programmer. With C++ in your toolbelt, you’ll be well-equipped to delve deeper into the world of game development, system programming, and much more!

Arrays in C++

Arrays are used to store multiple values of the same type in a single variable. Here’s an example of how to declare an array and access its elements:

int numbers[5] = {10, 20, 30, 40, 50};

// Accessing the elements
std::cout << numbers[0]; // Outputs 10
std::cout << numbers[2]; // Outputs 30

The index of an array starts from 0, which means the first element is accessed by ‘0’, the second by ‘1’, etc.

Strings in C++

Strings are used to store text. Here’s how to create a string and perform basic operations:

// Creating a string
std::string greeting = "Hello, Zenva!";

// Getting the length of the string
int len = greeting.length();
std::cout << len; // Outputs 11

// Concatenating strings
std::string welcome = "Welcome to";
std::string course = "C++ Course";
std::string message = welcome + " " + course; // Outputs "Welcome to C++ Course"

// Accessing specific character in a string
char ch = greeting[0];
std::cout << ch; // Outputs H

Switch Case in C++

The switch case statement is used to perform different actions based on different conditions. It is a multiple-choice selection statement. Below is an example:

int day = 3;

switch(day) {
  case 1:
    std::cout << "Monday";
  case 2:
    std::cout << "Tuesday";
  case 3:
    std::cout << "Wednesday";
    std::cout << "Invalid day";

This will output “Wednesday”. The break statement is used to prevent the code from running into the next case accidentally.

Pointers in C++

Pointers are variables that store the address of another variable. Here is how to declare a pointer, assign its value and use it

int num = 10;
int *p = &num; // Declare a pointer

std::cout << *p; // Outputs 10

The asterisk ( * ) is used to declare pointers and to dereference pointers. The ampersand ( & ) is used to obtain the address of a variable.

This should give you a taste of some additional concepts in C++, all of which are important stepping stones to becoming proficient in this powerful language.

Where to Go Next?

You’re off to a great start with C++! Keep up your momentum by taking your learning to the next level with Zenva Academy. Whether you’re a beginner honing your skills or an advanced coder looking to diversify, we’ve got you covered with over 250 supported courses that can boost your career.

We’d particularly recommend our C++ Programming Academy, which offers an in-depth look into the world of C++. Here, you’ll learn everything from the basics of C++ to more complex concepts such as object-oriented programming and memory management. What’s more, you’ll also get hands-on experience in creating simple games using C++, allowing you to build a portfolio of real C++ projects.

For a more varied collection of C++ courses, hazard a click over to our C++ Courses section. With Zenva, the journey from beginner to professional coder becomes a whole lot more engaging. Happy coding!


C++ is a powerful language brimming with potential. Gaining proficiency in C++ can open doors to diverse roles across numerous digital fields, particularly game development and system programming. As the core of many operating systems and games, C++ skills are highly coveted in the tech industry.

By mastering C++, you don’t just learn a programming language- you explore the breathtaking depth of computer science. To continue your learning path, Zenva offers comprehensive courses in C++, whether you’re starting out or looking to deepen your understanding. We can’t wait for you to join us on this coding adventure!


Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Unreal, Python, Godot and more.