Beginning raspberry pi c++ programming tutorial

C++ is very similar to C in terms of syntax and design which made the transition for C programmers very easy. Create a directory in your home folder called cplus.

Hint: If you enter the following at the command line

$ cd ~

This will take you back to your home directory. You can create the directory using the mkdir command.

$ mkdir cplus

$ cd cplus

Next check that you have a compiler installed that is capable of compiling C++ code.

$ g++ -V

If g++ isn’t installed then just enter

$ sudo apt-get install g++

Start nano.

$ sudo nano hello.cpp

Enter the following which is an example of a typical hello world program.

#include <iostream>

int main()

{
    cout << “Hello world!\n”;

    return 0;

}

The #include statement is required so we can access the built in output functions of C++ which reside inside the <iostream> file. Next is the int keyword which works in the same way that the C program worked. The main() function will expect an integer value to be returned so we can determine in all went well inside the main() function. We declare the main() functions and start the block of code with an open curly bracket. The next line down uses cout. The cout keyword stands for console out and the << indicate that we are creating some sort of output to the standard console. We are displaying a string so we need to wrap this in double quotes “ “.

Inside the string appears a backslash \ character. These are known as escape characters and have a special meaning depending on which character appears after it. In this case the n is used to create a new line. The line is then completed by using a ; semi colon. The next line uses the return keyword which returns the value of 0 to main. This is required because we have told main that it is expecting an integer value returned to it.

To compile the program enter the following.

g++-o hello hello.cpp

The above uses g++ to compile the program and uses the -o flag to create a programmed called hello. It will create this program based on the hello.cpp source code file. To execute or run the program enter

$ ./hello

The output will display Hello World.

The above code can be simplified by using variables instead of using hard coded strings. Hard coded strings are string that appear between the double quotes “ “. When you have a lot of strings scattered throughout your program you will find that it will become increasing hard to tracks these down when you need to change a word.

Start Nano again by this time call the file raspberry_pi.cpp

//The hello raspberry pi programming using variables

#include <iostream>

 

int main()

{

string message1 = “Hello ”;

string message2 = “Raspberry pi\n”

string message3 = “programming”;


    cout << message1 << message2 << message3;

    return 0;

}

 

Save the file and exit. Enter the following to compile the new program.

g++-o raspberry_pi raspberry_pi.cpp

The output will display

Hello Raspberry Pi
programming

The modifications of this file are mainly the introduction of string variables. You have defined a list of variable names called message1, message2 and message 3. You have done this by using the string keyword followed by the name of the variable which is message1 etc. You then assign a value to this variable by using the = assignment operator or equals symbol and include a string in double quotes “ “.

The cout << keyword uses multiple << output symbols. This just means that each variable will output one after the other.

Take a look at this example which uses a mixture of integers and strings. Create a new file using Nano called result.cpp.

//The hello raspberry pi programming using variables

#include <iostream>

 

int main()

{

string message1 = “The sum of 5+6 is “;

int num1 = 5;

int num2 = 6;

 

//create a result variable which will add num1 + num2

int result = num1 + num2;

 

//display a message and the result
    cout << message1 << result;

    return 0;

}

Compile this and execute the program. The result will display

The sum of 5+6 is 11

The keyword int is used to signify that we want to create an integer variable. Following this is the variable name, num1 and then the assignment = symbol. The value that appears on the right of the assignment symbol will be assigned to num1.

int num1 = 5;

You can even create variables from variables. For example, the following line will create a result variable but first will add num1 + num2 together and then assigns it to the result variable.

//create a result variable which will add num1 + num2

int result = num1 + num2;

Programming the Raspberry Pi

Next Raspberry Pi Beginning Object Oriented Programming Week 7 – Day 5 >>