Introduction to the C++ programming language

Now that you've been introduced to a simple programming language in Python, we need to spend a bit of time talking about a more complex but powerful language called C++. C++ is the original language of Linux, and has been around for many decades, but is still widely used by open source developers. It is similar to Python, but is also a bit different, and since you may need to understand and make changes to C++ code, you should be familiar with it and how it is used.

Prepare for lift off

As with Python, you will need to have access to the language capabilities. These come in the form of a compiler and build system, which turns your text files that contain programs to machine code that the processor can actually execute. To do this, type sudo apt-get install build-essential. This will install the programs you need to turn your code into executables for the system.

Engage thrusters

Now that the tools are installed, let's walk through some simple examples. Here is the first C++ code example:

Engage thrusters

And here is an explanation of the code:

  1. #include <iostream>: This is a library that is included so that your program can input data from the keyboard and output information to the screen.
  2. int main(): As with Python, we can put functions and classes in the file, but you will always want to start execution at a known point; C++ defines this as the main function.
  3. int a;: This defines a variable named a, of type int. C++ is a strongly typed language, which means we need to declare the type of the variable we are defining. The normal types are int: a number that has no decimal points, float: a number that requires decimal points, char: a character of text, and bool: a true or false value.
  4. int b;: This defines a variable named b, of type int.
  5. int c;: This defines a variable named c, of type int.
  6. std::cout << "Input value: ";: This will display the string Input value: to the screen.
  7. std::cin >> a;: The input that the user types will go into the variable a.
  8. std::cout << "Input second value: ";: This will display the string Input second value: to the screen.
  9. std::cin >> b;: The input that the user types will go into the variable a.
  10. c = a + b: The statement is a simple adding of two values, placing the result into the variable c.
  11. std::cout << c << std::endl;: The cout command prints out the value of c to the display. The endl at the end prints out a carriage return so that the next character appears on the next line.
  12. return 0;: The main function ends and returns 0.

To run this program, you'll need to run a compile process to turn it into an executable program that you can run. To do this, after you have created the program, type g++ example2.cpp –o example2. This will then process your program, turning it into a file that the computer can execute. The name of the executable program will be example2 (as specified by the name after the output –o option).

If you do an ll on your directory after you have compiled this, you should see the example2 file in your directory:

Engage thrusters

By the way, if you run into a problem, the compiler will try to help you figure out the problem. If, for example, you were to forget the int before int a; you would get the following error when you try to compile:

Engage thrusters

The error message indicates a problem in the int main() function, and tells you that the variable a was not successfully declared. Once you have the file compiled, to run the executable, type ./example2 and you should be able to create the following result:

Engage thrusters

I will not repeat the entire Python tutorial for C++ here; there are several good tutorials out on the Internet that can help. For example, at http://www.cprogramming.com/tutorial/c-tutorial.html and http://thenewboston.org/list.php?cat=14. There is one more aspect of C++ you will need to know about. The compile process that you just encountered seemed fairly straightforward. However, if you have your functionality distributed between a lot of files, or need lots of libraries, the command-line approach to executing a compile can get unwieldy.

The C++ development environment provides a way to automate this process, it is called the make process. When using this, you create a text program named makefile that defines the files you want to include and compile, and instead of typing a long command or set of commands you simply type make and the system will execute a compile based on the definitions in the makefile. Here is a tutorial that talks more about this system: http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/, or http://mrbook.org/tutorials/make/.

Objective complete – mini debriefing

Now you are equipped to edit and create your own programming files. The next chapters will provide you with lots of opportunity to practice your skills as you translate lines of code into cool robotic capabilities.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.51.157