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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C ++ Basics<br />

Chapter 2<br />

In this first chapter we will briefly introduce some basic knowledge about C ++. A useful site<br />

with a reference manual to C ++ commands is http://www.cplusplus.com/.<br />

2.1 Our First Program<br />

As an introduction to the C ++ language, let us look at the following example:<br />

#include <br />

int main ()<br />

{<br />

std::cout ≪ ”Answer to the Ultimate Question of Life, the Universe, and Everything is ”<br />

≪ 6 ∗ 7 ≪ std::endl;<br />

return 0;<br />

}<br />

according to Douglas Adams’ “Hitchhiker’s Guide to the Galaxy.” This short example shows<br />

already many things about C ++:<br />

• The first line includes a file name “iostream.” Whatever is defined in this file will be<br />

defined in our program as well. The file “iostream” contains the standard I/O of C ++.<br />

Input and output is not part of the core language in C ++ but part of the standard libraries.<br />

This means that we cannot program I/O commands without including “iostream” (or<br />

something similar). But it also means that this file must exist in every compiler because<br />

it is part of the standard. Include commands should be at the beginning of the file if<br />

possible.<br />

• the main program is called main and has an integer return value, which is set to 0 by the<br />

return command. The caller of a program (usually the operating system) knows that it<br />

finished successfully when a 0 is returned. A return code other than 0 symbolizes that<br />

something went wrong and often the return code also says something about what went<br />

wrong.<br />

• Braces “{ }” denote a block/group of code (also called a compound statement). Variables<br />

declared within “{ }” groups are only accessible within this block.<br />

19

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

Saved successfully!

Ooh no, something went wrong!