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.

66 CHAPTER 3. CLASSES<br />

apply symm blk2x2 rowmajor dnsvec multhr athlon(L.data addr, L.nrows, L.ncols,<br />

L.ldim, L.blksch, v.data addr, v.size);<br />

Developing software in this fashion is far from being fun. It wastes so much energy of the<br />

programmer. Getting such calls right is of course much more work than the <strong>for</strong>mer notations.<br />

If one of the arguments is stored in a different <strong>for</strong>mat, the function call must be meticulously<br />

adapted. Remember the person who implements the linear projection wanted to do science,<br />

actually.<br />

The cardinal error of scientific software providing such interfaces — there is even worse than<br />

our example — is to commit to too many technical details in the user interface. The reason lies<br />

partly in the usage of simplistic programming languages as C and Fortran 77 or in the ef<strong>for</strong>t to<br />

interoperate with software in these languages.<br />

Advise<br />

If you ever get <strong>for</strong>ced to write software that interoperates with C or Fortran,<br />

write your software first with a concise and intuitive interface in C ++ <strong>for</strong><br />

yourself and other C ++ programmer and add the C and Fortran interface on<br />

top of it.<br />

The elegant way of writing scientific software is to use and to provide the best abstraction. A<br />

good implementation reduces the user interface to the essential behavior and omits all surplus<br />

commitments to technical details. Applications with a concise and intuitive interface can be as<br />

efficient as their ugly and detail-obsessed counterparts.<br />

In our example, this is achieved by providing a class <strong>for</strong> every specific linear operator and implement<br />

the projection type-dependently. 1 This way, we can apply the projection without given<br />

all details and the user application is short and nice. This chapter will show the foundations of<br />

how providing new abstraction in scientific software and the following chapters will elaborate<br />

this.<br />

3.2 Class members<br />

Object types are called classes in C ++, defined by the class keyword. A class defines a new data<br />

type, which can be used to create objects. A class is a collection of:<br />

• data;<br />

• functions which are also referred to as member functions or methods;<br />

• types<br />

Furthermore class members can be public or private and classes can inherit from each other.<br />

Let us now give an example to illustrate the class concept. To have something tangible <strong>for</strong><br />

scientists, we refrain from foo and bar examples but implement gradually a class complex (al-<br />

1 Specializations <strong>for</strong> specific plat<strong>for</strong>ms can also be handled with the type system.

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

Saved successfully!

Ooh no, something went wrong!