66 3 Numerical differentiation and interpolation
In Ansi C and C++ arrays start by default fromi= 0. Moreover, if we wish to add two
vectors we need to explicitely write out a loop as
for(i=0 ; i < n ; i++){
a[i]=b[i]+c[i]
}
However, the strength of C++ over programming languages like C and Fortran 77 is the
possibility to define new data types, tailored to some particular problem. Via new data types
and overloading of operations such as addition and subtraction, we can easily define sets of
operations and data types which allow us to write a vector or matrix addition in exactly the
same way as we would do in Fortran. We could also change the waywe declare a C++ vector
(or matrix) elementai, froma[i]to saya(i), as we would do in Fortran. Similarly, we could also
change the default range from0 :nā 1 to1 :n.
To achieve this we need to introduce two important entities in C++ programming, classes
and templates.
The function and class declarations are fundamental concepts within C++. Functions are
abstractions which encapsulate an algorithm or parts of it and perform specific tasks in a
program. We have already met several examples on how to use functions. Classes can be
defined as abstractions which encapsulate data and operations on these data. The data can
be very complex data structures and the class can contain particular functions which operate
on these data. Classes allow therefore for a higher level of abstraction in computing. The
elements (or components) of the data type are the class data members, and the procedures
are the class member functions.
Classes are user-defined tools used to create multi-purposesoftware which can be reused
by other classes or functions. These user-defined data typescontain data (variables) and
functions operating on the data.
A simple example is that of a point in two dimensions. The datacould be thexandy
coordinates of a given point. The functions we define could besimple read and write functions
or the possibility to compute the distance between two points.
The two examples we elaborate on below demonstrate most of the features of classes.
We develop first a class calledComplexwhich allows us to perform various operations on
complex variables. We extend thereafter our discussion of classes to define a classVector
which allows us to perform various operations on a user-specified one-dimesional array, from
declarations of a vector to mathematical operations such asadditions of vectors. Later, in our
discussion on linear algebra, we will also present our final matrix and vector class.
The classes we define are easy to use in other codes and/or other classes and many of the
details which would be present in C (or Fortran 77) codes are hidden inside the class. The
reuse of a well-written and functional class is normally rather simple. However, to write a
given class is often complicated, especially if we deal withcomplicated matrix operations. In
this text we will rely on ready-made classes in C++ for dealing with matrix operations. We
have chosen to use the libraries like Armadillo or Blitz++, discussed in our linear algebra
chapter. These libraries hide many low-level operations with matrices and vectors, such as
matrix-vector multiplications or allocation and deallocation of memory. Such libraries make
it then easier to build our own high-level classes out of well-tested lower-level classes.
The way we use classes in this text is close to theMODULEdata type in Fortran and we
provide some simple demonstrations at the end of this section.