CPP
Simple C++ Examples


C++ is a standard programming language which combines features of the procedural language C with new object oriented abilities.

Depending on your computer, you may invoke the C++ compiler by a command like c++, CC, cxx, or g++ (which is the name of the Gnu C++ 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"!

For most C++ compilers, it is customary for include files to have the extension ".H" rather than the extension ".h" which is used when the include file is associated with a C routine.

Related Data and Programs:

C is a directory of examples of C programs.

F77 is a directory of examples which demonstrates some features of the FORTRAN77 language.

F90 is a directory of examples which demonstrates some features of the FORTRAN90 language.

MAKEFILES is a directory of examples which demonstrates the use of makefiles with a set of C++ files.

MIXED shows some examples of writing a program partly in C++ and partly in some other language.

Reference:

  1. Harvey Deitel, Paul Deitel,
    C++: How to Program,
    Fourth Edition,
    Prentice Hall, 2002,
    ISBN: 0-13-089571-7.
  2. Steve Oaulline,
    Practical C++ Programming,
    Second Edition,
    O'Reilly, 2003,
    ISBN: 1-56592-139-9.
  3. Bjarne Stroustrup,
    The C++ Programming Language,
    Third Edition,
    Addison-Wesley, 2000.

Examples and Tests:

ARRAYS is a very simple example of how to set up arrays in C++. In particular, we note that vectors are easy to declare with fixed or dynamic dimensions, but that arrays of dimension 2 or greater aren't easy to declar dynamically. The example shows how to work around by settting up a two dimensional array as a one dimensional vector. Files you may copy include:

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!) Files you may copy include:

HELLO is just a "Hello, world!" program. Files you may copy include:

LIMITS prints out some information about the range and accuracy of various numeric types. Files you may copy include:

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! Files you may copy include:

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

RANDOM_NUMBERS shows how to use the standard library routines SRANDOM to set the seed and RANDOM to return random numbers. Files you may copy include:

SIZES prints out the sizes of various datatypes. Files you may copy include:

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


Last revised on 16 October 2007.