CPP
Simple C++ Examples


C++ is a directory of C++ examples which illustrate some of the features of the language.

Depending on your computer, you may invoke the C++ compiler by a command like c++, CC, cxx, g++ (the Gnu compiler), icc (the Intel compiler), pcCC (the Portland Group compiler), or xlc++ (the IBM compiler).

Depending on your compiler, your C++ files may need to have the extension ".C", ".cc", ".cxx", ".cpp" or even ".c++". In particular, Microsoft Visual C++ does not "know" the difference between upper and lower case in file extensions. If you call your file "fred.C", it will think your file is a C file, not a C++ file. For Microsoft Visual C++, always use the file name extension ".cpp"!

It is customary for include files to have the extension ".H", but I have also seen ".h", ".hxx" and ".hpp" extensions used.

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Languages:

Directories related to CPP are available in a C version and a C++ version and a FORTRAN77 version and a FORTRAN90 version and a MATLAB version and a Python version.

Related Data and Programs:

CPP_ARRAYS, C++ programs which illustrate the use of vectors, matrices and tensors.

CPP_INTRINSICS, a C++ program which illustrates the use of intrinsic functions.

CPP_RANDOM, C++ programs which illustrate the use of the random number generator routines.

MAKEFILES, C++ programs which illustrate the use of MAKEFILES for maintaining a software project;

MIXED, C++ programs which call a function written in another programming language.

MPI, C++ programs which illustrate the use of the MPI application program interface for carrying out parallel computations in a distributed memory environment.

OPENMP, C++ programs which illustrate the use of the OpenMP application program interface for carrying out parallel computations in a shared memory environment.

Reference:

  1. Paul Deitel, Harvey Deitel,
    C++: How to Program,
    Seventh Edition,
    Prentice Hall, 2011,
    ISBN: 978-013-216541-9,
    LC: QA76.73.C153.D45.
  2. Steve Oaulline,
    Practical C++ Programming,
    Second Edition,
    O'Reilly, 2003,
    ISBN: 1-56592-139-9,
    LC: QA76.73.C15.O84.
  3. Bjarne Stroustrup,
    The C++ Programming Language,
    Addison-Wesley, 2000,
    ISBN: 0-201-70073-5,
    LC: QA76.73.C153.S77.

Examples and Tests:

ARRAY_RETURN demonstrates how a function can call another function which creates and returns several arrays in its argument list. To do this requires some tricky use of pointers.

BIG_INTS_REAL shows what can go wrong when you try to move large integer values into and out of real variables.

CHARACTER_ARG demonstrates how a C++ function can return character information through its argument.

COMPLEX_VALUES demonstrates the use of the ANSI COMPLEX class for complex arithmetic. (At the moment, I am having a terrible time just trying to use the assignment operator!)

DYNAMIC_ARRAY_2D shows how to create a 2D array dynamically, so that addresses like "a[2][5]" are legal.

FUNCTION_POINTER shows how a variable can be created which can point to a function; the target of the pointer can be changed so that a different function is indicated.

FUNCTION_POINTER_ARRAY shows how a variable can be created which can be an array of pointers to function.

FUNCTION_POINTER_ARRAY_NEW shows how a variable can be created which can be a DYNAMICALLY ALLOCATED array of pointers to function. In other words, this is one way to create an array of function pointers whose dimension is not specified in advance. Then the array can be sized with the NEW command and freed with the DELETE command.

HELLO is just a "Hello, world!" program.

LIMITS prints out some information about the range and accuracy of various numeric types.

NOT_ALLOCATED_ARRAYS shows that you should initialize your array pointers to NULL, and reset them to NULL after you delete your arrays. Otherwise, an unallocated or delete array is liable to have a deceptive nonnull value!

POISSON solves the Poisson equation on a 2D grid. This program uses dynamically allocated doubly dimensioned arrays.

PRECISION_OUTPUT shows how the manipulator function setprecision() changes the precision for all subsequent floating point output operations, and how the precision() function can be better used, to record the initial precision, change it, or reset it to the default value.

SIZES prints out the sizes of various datatypes.

You can go up one level to the C++ source codes.


Last revised on 15 March 2012.