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.

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

#ifndef athens foo hpp<br />

#define athens foo hpp<br />

double foo (double a, double b);<br />

#endif<br />

Note the ifndef and define C-preprocessor commands. These commands are called include guards<br />

and prevent the file from being included several times. The use of such guards in header files is<br />

quite common.<br />

The source file in the library would be contained in the file foo.cpp.<br />

#include ”foo.hpp”<br />

double foo (double a, double b)<br />

{ return a+b; }<br />

The main program file is contained in the file bar.cpp:<br />

#include <br />

#include ”foo.hpp”<br />

int main() {<br />

double a = 2.1;<br />

double b = 3.9;<br />

std::cout ≪ foo(a,b) ≪ std::endl ;<br />

}<br />

Include files usually contain the interface of software packages and are stored somewhere on<br />

disk. The compiler is told where to look <strong>for</strong> the include files. The programmer can partially<br />

control this as follows:<br />

• #include ”foo.hpp”: the compiler looks in the directory of the including file and the list of<br />

directories it is given.<br />

• #include : the compiler only looks in the list of directories it is given.<br />

Frequently used include files<br />

The types and functions defined in the following include files are in the namespace std.<br />

• : input and output stream, e.g. std::cin and std::cout<br />

• : file input and output<br />

• : For assertions, see §2.6.5.<br />

• : Headers <strong>for</strong> the C functions from math.h, among others: abs, fabs, pow, acos,<br />

asin, atan, atan2, ceil, floor, cos, cosh, sin, sinh, exp, fmod (floating point mod), modf (split in<br />

integer and fractional part (< 1)), log, log10, sqrt, tan, tanh<br />

And other useful functions such as isnan.<br />

• : String operations

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

Saved successfully!

Ooh no, something went wrong!