Docsity

C++ Programming Tools, Vectors, Strings, Inheritance, and Debugging, Slides of Computational and Statistical Data Analysis

Partial preview of the text

Download C++ Programming Tools, Vectors, Strings, Inheritance, and Debugging and more Slides Computational and Statistical Data Analysis in PDF only on Docsity!

Computing and Statistical Data Analysis

Lecture 7

STL and the Standard C++ Library

vector, string, ...

Inheritance (quick tour)

Some tools:

compiling/linking with gmake

debugging with ddd

The Standard C++ Library

We’ve already seen parts of the standard library such as iostream

and cmath. Here are some more:

What you #include What it does

useful algorithms (sort, search, ...)

complex number class

a linked list

a stack (push, pop, etc.)

proper strings (better than C-style)

often used instead of arrays

Most of these define classes using templates, i.e., we can have a

vector of objects or of type double, int, float, etc. They form

what is called the Standard Template Library (STL).

Sorting elements of a vector

Here is sample code that uses the sort function in algorithm:

#include #include using namespace std; bool descending(double x, double y){ return (x>y); } int main() { ... // u, v are unsorted vectors; overwritten by sort. // Default sort is ascending; also use user- // defined comparison function for descending order. sort(u.begin(), u.end()); sort(v.begin(), v.end(), descending);

Iterators

To loop over the elements of a vector v, we could do this:

vector v = ... // define vector v for (int i=0; i<v.size(); i++){ cout << v[i] << endl; }

Alternatively, we can use an iterator, which is defined by the

vector class (and all of the STL container classes):

vector v = ... // define vector v vector::iterator it; for (it = v.begin(); it != v.end(); ++it){ cout << *it << endl; }

vector’s begin and end functions point to the first and last elements.

++ tells the iterator to go to the next element.

* gives the object (vector element) pointed to (note no index used).

Inheritance

Often we define a class which is similar to an existing one. For

example, we could have a class

class Animal { public: double weight(); double age(); ... private: double m_weight; double m_age; ... };

Related classes

Now suppose the objects in question are dogs. We want

class Dog { public: double weight(); double age(); bool hasFleas(); void bark(); private: double m_weight; double m_age; bool m_hasFleas; ... };

Dog contains some (perhaps many) features of the Animal class but

it requires a few extra ones.

The relationship is of the form “X is a Y”: a dog is an animal.

Polymorphism, virtual functions, etc.

We might redefine a member function of Animal to do or mean

something else in Dog. This is function “overriding”. (Contrast this

with function overloading.)

We could have age() return normal years for Animal, but “dog

years” for Dog. This is an example of polymorphism. The function

takes on different forms, depending on the type of object calling it.

We can also declare functions in the base class as "pure virtual" (or

"abstract"). In the declaration use the keyword virtual and set

equal to zero; we do not supply any definition for the function in

the base class:

virtual double age() = 0;

This would mean we cannot create an Animal object. A derived

class must define the function if it is to create objects.

Compiling and linking with gmake

For our short test programs it was sufficient to put the compile and

link commands in a short file (e.g. build.sh).

For large programs with many files, however, compiling and

linking can take a long time, and we should therefore recompile

only those files that have been modified.

This can be done with the Unix program make (gnu version gmake).

Homepage www.gnu.org/software/make

Manual ~150 pages (many online mini-tutorials).

Widely used in High Energy Physics (and elsewhere).

Simple example without gmake

Usually we compile with

g++ -o hello hello.cc goodbye.cc

which is really shorthand for compiling and linking steps:

Now suppose we modify goodbye.cc. To rebuild, really we only

need to recompile this file.

But in general it’s difficult to keep track of what needs to be

recompiled, especially if we change a header file.

Using date/time information from the files plus user supplied

information, gmake recompiles only those files that need to be and

links the program.

g++ -c hello.cc g++ -c goodbye.cc g++ -o hello hello.o goodbye.o

Simple example with gmake

The first step is to create a “makefile”. gmake looks in the current

directory for the makefile under the names GNUmakefile,

makefile and Makefile (in that order).

The makefile can contain several types of statements, the most

important of which is a “rule”. General format of a rule:

target : dependencies command

The target is usually the name of a file we want to produce and the

dependencies are the other files on which the target depends.

On the next line there is a command which must always be

preceded by a tab character (spaces no good). The command tells

gmake what to do to produce the target.

gmake refinements

In the makefile we can also define variables (i.e., symbols). E.g.,

rather than repeating hello.o goodbye.o we can define

When gmake encounters $(objects) it makes the substitution.

objects = hello.o goodbye.o hello : $(objects) g++ -o hello $(objects) ...

We can also make gmake figure out the command. We see that

hello.o depends on a source file with suffix .cc and a header file

with suffix .h. Provided certain defaults are set up right, it will

work if we say e.g.

hello.o : hello.cc goodbye.h

gmake for experts

makefiles can become extremely complicated and cryptic.

Often they are hundreds or thousands of lines long.

Often they are themselves not written by “humans” but rather

constructed by an equally obscure shell script.

The goal here has been to give you some feel for what gmake does

and how to work with makefiles provided by others.

Often software packages are distributed with a makefile. You

might have to edit a few lines depending on the local set up

(probably explained in the comments) and then type gmake.

We will put some simple and generalizable examples on the

course web site.

Using ddd

The ddd homepage is www.gnu.org/software/ddd

There are extensive online tutorials, manuals, etc.

To use ddd, you must compile your code with the -g option:

g++ -g -o MyProg MyProg.cc

Then type

ddd MyProg

You should see a window with your program’s source code and a

bunch of controls.

When you start ddd

From the ddd online manual: