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.
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.
Now that the tools are installed, let's walk through some simple examples. Here is the first C++ code example:
And here is an explanation of the code:
#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.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.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.int b;
: This defines a variable named b
, of type int
.int c;
: This defines a variable named c
, of type int
.std::cout << "Input value: ";
: This will display the string Input value:
to the screen.std::cin >> a;
: The input that the user types will go into the variable a
.std::cout << "Input second value: ";
: This will display the string Input second value:
to the screen.std::cin >> b;
: The input that the user types will go into the variable a
.c = a + b
: The statement is a simple adding of two values, placing the result into the variable c
.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.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:
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:
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:
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/.
3.16.51.157