Skip to main content

THE C++ SELF-STUDY COURSE

THE C++ SELF-STUDY COURSE

Since the academic year 2002/03, the Department offers an introductory self-study course in C++. The course is self-contained and does not assume any previous knowledge of C++ or of any other programming language. Level 2 students taking Module PHYS2551, “Laboratory Skills and Practice”, will receive a copy of the notes free of charge, together with the self-study Fortran course. All other students are welcome to purchase the notes from the Level 2 Lab Technician at the cost of the photocopy (3 pounds).

The C++ course is entirely optional. It is not assessed and is not a corequisite or a prerequisite of any Physics module.

Dr Potvliege will be happy to hear what you think about the course (eg, whether you found it useful, what you found unclear or far too difficult) and to hear any suggestion for improvements.


Dev-C++

For this course, we recommend that you use an ‘Integrated Development Environment’ (IDE) called Dev-C++. This IDE is based on the standard GCC compiler of the Free Software Foundation. You can freely download it from the web for use on your own PC. Dev-C++ is also installed on the Networked PC Service of the University. (You can access it through Start > Programs > Academic Software > Physics > Dev-C++. Clicking on Dev-C++ starts the system. See below for the error messages you may get.)

Once you have started Dev-C++, you can either open existing C++ programs or start a new project (see the File menu). Working within a project allows you to divide your program into separate files, which are compiled together. Click on New Project and select “Empty Project” from the menu displayed. Choose a suitable name. You’ll then be invited to write a new file in the editor window, under the name ‘Untitled1’. This file can be saved under a new name of your choice. Files can be added and removed from a project through the controls provided by the tool bar.

Due to the way in which Dev-C++ has been installed on the University PC Networked Service, the compiler may return warning messages about inconsistent versions of library files; these can be ignored.

Following a recent change in certain conventions, students using an old (pre-2006) edition of the course may need to add the statement
using namespace std;
after the include statements in each of the programs listed in the printed notes.

The Salford C++ compiler of the Plato system running on the PC Networked Service does not provide certain libraries (now standard) which are required for the course. It should not be used.


Model solutions

The quadratic-1.0.cpp program of Exercise 2.1:

quadratic-1.0.cpp

Model solutions of the programming exercises can be found here:

Exercise 2.2: quadratic-1.0a.cpp
Exercise 2.3: projectile-1.0.cpp
Exercise 3.1: polynomial-2.0.cpp
Exercise 4.1: projectile-2.0.cpp
Exercise 4.1: projectile-data.in
Exercise 4.1: projectile-data.out
Exercise 4.2: quadratic-5.0.cpp
Exercise 4.2: quadratic.in
Exercise 4.3: lin-sol
Exercise 4.3: polynomial-3.0.cpp
Exercise 4.3: polynomial.in
Exercise 4.3: quad-sol
Exercise 6.1: polynomial-4.0.cpp
Exercise 6.2: sort-strings-1.0.cpp
Exercise 6.2: sort-strings-2.0.cpp


A few useful links

An enormous volume of information on C++ can be found on the web. For example:

A list of internet sites and files of interest to C++ users. (Awesome).

The web site of the Association of C & C++ Users. (Links, resources, book reviews, and more. The ACCU is a non-profit organisation devoted to professionalism at all levels in C, C++, Java and Python.)

Bjarne Stroustrup’s web page. (Many useful links.) You will enjoy reading this article of his, ‘Learning C++ as a new language’ (C/C++ Users Journal. pp 43-54. May 1999).

The C++ Lite FAQ. (Frequently asked questions).

C++ Libraries FAQs. (More frequently asked questions.)


Numerical Computation in C++

If you are thinking to use C++ for an undergraduate project in the Department and are not yet an experienced programmer in C++, consider first the following: (1) If your project involves much number crunching, you will probably find it easier to develop your programs in Fortran rather than C++. (2) As far as numerical computation is concerned, there is far more expertise in Fortran programming in the Department than in C++ programming. (3) Using C++ rather than Fortran or C means that you cannot directly use the NAG Library (which is installed on the Unix Service) nor the Fortran 77 or C routines of Numerical Recipes (which are easy to type from the book and are straightforward to implement). Instead, you will have to find and install your own mathematical software. Consult your supervisor about the choice of language before you start !If you go ahead and develop your project in C++, you may find some of the following useful:

An index of libraries, tools, conferences, and mailing lists for Object-Oriented Scientific Computing. From the same organisation, Blitz++, a C++ class library for scientific computing. The current versions provide dense arrays and vectors, random number generators, and small vectors and matrices. Blitz++ is distributed freely under an open source license.

LAPACK++, the Linear Algebra PACKage in C++. (A software library for numerical linear algebra that solves systems of linear equations and eigenvalue problems on high performance computer architectures. This package does not include all of the capabilities of the original Fortran LAPACK. It is being superseded by the Template Numerical Toolkit, which utilizes new features of the ANSI C++ specification.)

Numerical Recipes, 3rd edition. (An excellent collection of numerical routines in object-oriented C++. A copy of the 2nd C++ edition of this book can be borrowed from the Level 2 Lab Technician. However, it may be difficult for you to use the codes unless you purchase the CD ROM.)

The TBCI templated C++ numerical library.

For special functions, see also the TMath basic ROOT class of the CERN ROOT package.

Free information on a variety of topics of interest for those developing applications in sciences or engineering, offered by MathWorks, Inc.

Linking C or Fortran numerical routines to your C++ program may be a solution if you cannot get the C++ numerical routines you need. Linking C functions is meant to be easier: see FAQ 31.5 of the C++ Lite FAQ. Linking Fortran to C++ is fraught with difficulties: see Mixed language programming using C++ and FORTRAN 77 by Carsten A. Arnholm, or Using C and C++ with Fortran by Nelson H F Beebe.

Dr Potvliege is interested to hear about your experience with numerical computation in C++ (what works, what doesn’t, where to get the software from, etc..), so that the course and this web page could be improved in the future.