03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

20 CHAPTER 2. <strong>C++</strong> BASICS<br />

• std::cout and std::endl are defined in “iostream.” The <strong>for</strong>mer is an output stream that prints<br />

text on the screen (unless it is redirected). With std::endl a line is terminated.<br />

• The special operator≪ is used to pass objects to the output to an stream std::cout that is<br />

to print it on that stream.<br />

• The double quotes surround string constants, more precisely string literals. This is the<br />

same as in C. For string manipulation, however, one should use C ++’s string class instead<br />

of C’s cumbersome and error-prone functions.<br />

• The expression 6 ∗ 7 is evaluated and a temporary integer is passed to std::cout. In C ++<br />

everything has a type. Sometimes we as programmers have to declare the type and<br />

sometimes the compiler deduces it <strong>for</strong> us. 6 and 7 are literal constants that have type int<br />

and so has their product.<br />

This was a lot of in<strong>for</strong>mation <strong>for</strong> such a short program. So let us start step by step.<br />

TODO: A little explanation how to compile and run it. For g++ and Visual Studio.<br />

2.2 Variables<br />

In contrast to most scripting languages C ++ is strongly typed, that is every variable has a type<br />

and this type never change. A variable is declared by a statement TYPE varname. 1 Basic types<br />

are int, unsigned int, long, float, double, char, and bool.<br />

int integer1 = 2;<br />

int integer2, integer3;<br />

float pi = 3.14159;<br />

char mycharacter = ’a’;<br />

bool cmp = integer1 < pi;<br />

Each statement has to be terminated by a “;”. In the following section, we show operations<br />

that are often applied to integer and float types. In contrast to other languages like Python,<br />

where ’ and ” is used <strong>for</strong> both characters and strings, C ++ distinguishes between the two of<br />

them. The C ++ compiler considers ’a’ as the character ‘a’ (it has type char) and ”a” is the string<br />

containing ‘a’ (it has type char[1]). If you are used to Python please pay attention to this.<br />

Advise<br />

Define variables right be<strong>for</strong>e using them the first time. This makes your<br />

programs more readable when they grow long. It also allows the compiler to<br />

use your memory more efficiently when you have nested scopes (more details<br />

later). Old C versions required to define all variables at the beginning of a<br />

function and several people stick to this till today. However, in C ++ it leads<br />

generally to higher efficiency and more importantly to higher readability to<br />

define variables as late as possible.<br />

1 TODO: too simple, variable lists and in-place initialization is missing

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!