C++ Basics
- “To my children: Never make fun of having to help me with computer stuff. I taught you how to use a spoon.”
- —Sue Fitzmaurice
In this first chapter, we will guide you through the fundamental features of C++. As for the entire book, we will look at it from different angles but we will not try to expose every possible detail—which is not feasible anyway. For more detailed questions on specific features, we recommend the online manuals http://www.cplusplus.com/ and http://en.cppreference.com.
1.1 Our First Program
As an introduction to the C++ language, let us look at the following example:
#include <iostream>
int main ()
{
std::cout ≪ "The answer to the Ultimate Question of Life,\n"
≪ "the Universe, and Everything is:"
≪ std::endl ≪ 6 * 7 ≪ std::endl;
return 0;
}
which yields
The answer to the Ultimate Question of Life,
the Universe, and Everything is:
42
according to Douglas Adams [2]. This short example already illustrates several features of C++:
- Input and output are not part of the core language but are provided by the library. They must be included explicitly; otherwise we cannot read or write.
- The standard I/O has a stream model and is therefore named <iostream>. To enable its functionality, we include <iostream> in the first line.
- Every C++ program starts by calling the function main. It does return an integer value where 0 represents a successful termination.
- Braces {} denote a block/group of code (also called a compound statement).
- std::cout and std::endl are defined in <iostream>. The former is an output stream that prints text on the screen. std::endl terminates a line. We can also go to a new line with the special character \n.
- The operator ≪ can be used to pass objects to an output stream such as std::cout for performing an output operation.
- std:: denotes that the type or function is used from the standard Namespace. Namespaces help us to organize our names and to deal with naming conflicts; see §3.2.1.
- String constants (more precisely literals) are enclosed in double quotes.
- The expression 6 * 7 is evaluated and passed as an integer to std::cout. In C++, every expression has a type. Sometimes, we as programmers have to declare the type explicitly and other times the compiler can deduce it for us. 6 and 7 are literal constants of type int and accordingly their product is int as well.
Before you continue reading, we strongly recommend that you compile and run this little program on your computer. Once it compiles and runs, you can play a little bit with it, for example, adding more operations and output (and looking at some error messages). Finally, the only way to really learn a language is to use it. If you already know how to use a compiler or even a C++ IDE, you can skip the remainder of this section.
Linux: Every distribution provides at least the GNU C++ compiler—usually already installed (see the short intro in Section B.1). Say we call our program hello42.cpp; it is easily compiled with the command
g++ hello42.cpp
Following a last-century tradition, the resulting binary is called a.out by default. One day we might have more than one program, and then we can use more meaningful names with the output flag:
g++ hello42.cpp -o hello42
We can also use the build tool make (overview in §7.2.2.1) that provides (in recent versions) default rules for building binaries. Thus, we could call
make hello42
and make will look in the current directory for a similarly named program source. It will find hello42.cpp, and as .cpp is a standard file suffix for C++ sources, it will call the system’s default C++ compiler. Once we have compiled our program, we can call it on the command line as
./hello42
Our binary can be executed without needing any other software, and we can copy it to another compatible Linux system1 and run it there.
Windows: If you are running MinGW, you can compile in the same manner as under Linux. If you use Visual Studio, you will need to create a project first. To begin, the easiest way is to use the project template for a console application, as described, for instance, at http://www.cplusplus.com/doc/tutorial/introduction/visualstudio. When you run the program, you have a few milliseconds to read the output before the console closes. To extend the reading phase to a second, simply insert the non-portable command Sleep(1000); and include <windows.h>. With C++11 or higher, the waiting phase can be implemented portably:
std::this_thread::sleep_for(std::chrono::seconds(1));
after including <chrono> and <thread>. Microsoft offers free versions of Visual Studio called “Express” which provide the support for the standard language like their professional counterparts. The difference is that the professional editions come with more developer libraries. Since those are not used in this book, you can use the “Express” version to try our examples.
IDE: Short programs like the examples in this book can be easily handled with an ordinary editor. In larger projects it is advisable to use an Integrated Development Environment to see where a function is defined or used, to show the in-code documentation, to search or replace names project-wide, et cetera. KDevelop is a free IDE from the KDE community written in C++. It is probably the most efficient IDE on Linux and integrates well with git and CMake. Eclipse is developed in Java and perceivably slower. However, a lot of effort was recently put into it for improving the C++ support, and many developers are quite productive with it. Visual Studio is a very solid IDE that comes with some unique features such as a miniaturized colored page view as scroll bar.
To find the most productive environment takes some time and experimentation and is of course subject to personal and collaborative taste. As such, it will also evolve over time.