Home Content C++ for beginners

C++ for beginners

by Jack Simpson

In this tutorial, I’m going to provide an introduction to the basics of the programming language C++. I’ll describe how to compile your first program with the gcc compiler on Linux and Mac, although the code should also work on Windows using the Visual Studio compiler. If you’re new to programming, I’d probably recommend getting started with an easier scripting language like Python, before you get into C++. That said, hopefully the information in this post should still be useful for the complete beginner.

What is C++?
C++ was invented by Bjarne Stroustrup in 1978 as an extension of the popular C programming language. It added classes and the ability to write object orientated code.

Is C++ worth learning?
Although C and C++ came out decades ago, they remain extremely popular when speed and performance are a crucial requirement. Although C is a great language, C++ added a lot of functionality which comes in handy for big projects. While knowing C will give you a head-start when understanding C++, it is not essential. Personally, I started off learning C before changing to C++ because the imaging library I use for a lot of my work (OpenCV) is written in C++.

What a C++ program looks like
If we now jump into some code, I’ll explain a basic C++ program to you. Open a text editor and write the following code:

#include <iostream>

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

Save your file as “hello.cpp”. The “cpp” indicates the filetype for a C++ program. To compile, open a terminal and navigate to the folder where your “hello.cpp” file was saved, then type:

g++ hello.cpp -o hello

This will create a your program, which will be named “hello”. To execute your program type in:

 ./hello

The “./” before your program name tells the computer to look within the current directory for a program named “hello”. If everything worked, you should now see “Hello, world!” output in your terminal. Well done, you’ve compiled and run your first C++ program… but how did it work? I’ll write the code out again, but this time I’ll explain it using comments. Comments are text you can write in your code which the computer will ignore whilst compiling your program.

// This is a comment, when I write "//" it tells the
// compiler to ignore all text for the rest of the line!

/* This is a multiline comment
The compiler will ignore all text
until I write an asterisk and forward slash */

// include tells the compiler to add some prewritten code to your program
// iostream enables us to input and output text with the program via the terminal
// it is part of the standard library of C++
#include <iostream>

// All C++ programs are executed from within the main function
// the type of function is an int (integer) because it returns a number
int main()
{
    // we're now going to output text to the terminal
    // keep in mind how each line of code is ended with a ";" except for when a function is started
    // we'll use the "cout" command to do this: think of "cout" as its pronounced - "c-out"
    // cout is included in the standard library we brought into the program with the #include directive
    // to tell the compiler we're using commands from the standard library, we prefix them with "std::"
    // finally, the "endl" command adds a newline to the end of the text we output
    std::cout << "Hello, world!" << std::endl;
    // If the program runs without error, main() should return 0
    return 0;
}

You can also write the program so that you do not need to prefix standard library commands with “std::” by defining the namespace of the standard library:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, world!" << endl;
    return 0;
}

C++ Data Types
Now let’s create some variables and add some of the basic data types of C++ to the program:

#include <iostream>
// we need to include the string header because we'll be using this data type later
#include <string>
using namespace std;

int main()
{
    // some ways you can create variables holding integers
    int number1;
    number1 = 51;
    int number2 = 3;
    // initialising two variables at once only works if they're the same type
    int number3, number4;
    number3 = 23;
    number4 = 67;
    int addition = number3 + number4;
    cout << addition << endl;

    // for numbers with a decimal place, use the float data type
    // be wary that C++ has a finite level of precision after the decimal
    float decimal1 = 0.2;
    // if you want more precision, use the "double" data type
    double precise = 0.00000000000000201;

    // characters and strings
    // characters are enclosed in single quotation marks, strings in double
    char letter = 'a';
    // you can create arrays to store lists of letters or numbers
    // only one data type can be stored in each array
    // you can tell the compiler how many elements go into the array with a number in the brackets
    char character_array[4] = {'A','B','C','D'};
    // arrays are counted from zero, and are accessed using the brackets
    // so if you want to access the 'A'
    char first = character_array[0];
    // here is an easier way to create an array, the compiler will figure out the length itself
    // note the double quotation marks
    char sentence[] = "This is a sentence stored in an array";
    cout << sentence << endl;
    // C++ also added the string data type in the standard library
    // this makes working with text even easier!
    string sentence2 = "This is a sentence using the C++ standard library string type";

    // boolean is a simple data type: true or false, 1 or 0
    bool isTrue = true;
    bool wrong = false;

    return 0;
}

I hope you’ve found my introduction to the basics of C++ useful, I’m hoping to write future posts that go into C++ in more depth soon.

Sign up to my newsletter

Sign up to receive the latest articles straight to your inbox

You may also like

Leave a Comment