You are on page 1of 5

C++ Examples (ANSI/ISO Standard Version)

Copyright C. S. Tritt, Ph.D.

Linked are four C++ programs that illustrate some of the major features of the language. For
more information on many of these language features, see my C++ Quick Reference Page.

Some of these examples use function prototypes and put function definitions after main(). An
alternate approach is to skip the function prototyhpes and put function definitions before main().
The prototype approach scales up better, while the functions before main() approach is more
convenient for small programs.

sum.cpp swap.cpp
stdlib.cpp arrays.cpp
strings.cpp complex.cpp
comments.cpp cppstrings.html
cstrings.cpp cstrings2.cpp
stlstrings.cpp stlstrings2.cpp
stringswitch.cpp polytropic.cpp
formatedio.cpp rectangle.cpp

The program sum.cpp illustrates:

 Overall program structure including function declaration with default values.


 Inclusion of header files.
 Declaration of variables of built in types.
 Output formating.
 Writing to the screen (console output, i.e. cout) << (the stream insertion operator).
 Reading from the keyboard (console in, i.e. cin) >> (the stream extraction operator).
 Simple selection structures (if statements).
 Use of functions that return values but don't change their arguments.

Return to Table of Contents

The program swap.cpp illustrates:

 Declaration of variables of built in types with initialization.


 File I/O using streams.
 Use of functions that change the values of their arguments using references.
 Use of the assignment operator (=).

Return to Table of Contents


The program stdlib.cpp illustrates:

 Using command line arguments.


 Using compiler directives and conditional compilation.
 Using the assert() mechanism for error handling.
 Getting the system time and using it to seed a random number generator.
 Random number generation with srand(int seed) and rand functions.
 Improving a poor random number generator.
 System calls with the system(char* command) function.

Return to Table of Contents

The program arrays.cpp reads from file asum.in and illustrates:

 Use of const (constant) values.


 Creation of vectors.
 Passing vectors as function arguments.
 Reading from files of unknown size (monitoring istream status).
 Repetitive structures (while and for loops).
 The increment operators (++).
 Selection structures (if-else statements).
 Use of the .size, .empty, .begin, .insert, .erase, .resize, .clear and .swap vector class
member functions.

Return to Table of Contents

The program strings.cpp illustrates:

 Creation of strings.
 Concatenation of strings.
 Use of the .insert, .find, .length and .substr string class member functions.

Return to Table of Contents

The program complex.cpp reads from file complex.dat and illustrates:

 Declaration and definition of classes and user defined types.


 Declaration, definition and use of member functions.
 Creating objects.
 Using member functions.
 Function overloading.
 The char built-in type.
 Skipping comments in data files.
 Use of standard library math functions declared in cmath.

Note: Modern C++ has a complex template class (defined in <complex>).


Return to Table of Contents

The program comments.cpp reads text from the file sometext.txt and illustrates:

 How to look ahead (peek) while reading from a file (ifstream).


 How to selectively skip data (comments) in a file (ifstream).
 How to extract multi-word text from a stream into an STL string using getline().
 How to extract data from files of arbitary length.

Return to Table of Contents

The web page cppstrings.html discusses various ways in which to handle textual data in modern
C++.

Return to Table of Contents

The program cstrings.cpp illustrates:

 A way to use C strings.


 A way to get and respond to textual input from a user.
 How to use constants to support multiple languages in program.

Return to Table of Contents

The program cstrings2.cpp illustrates:

 Another way to use C strings.


 Another way to get and respond to textual input from a user.
 How to use the strcmp() function to compare C stings.

Return to Table of Contents

The program stlstrings.cpp illustrates:

 A way to use STL strings.


 A way to get and respond to textual input from a user.
 How to compare characters within STL strings.

Return to Table of Contents

The program stlstrings2.cpp illustrates:

 Another way to use STL strings.


 Another way to get and respond to textual input from a user.
 How to use the STL string comparison operators (i.e., ==).
Return to Table of Contents

The program stringswitch.cpp illustrates:

 Yet another way to use STL strings.


 Yet another way to get and respond to textual input from a user.
 The use of the switch control construct.
 The use of the [] STL string operator.

Return to Table of Contents

The program polytropic.cppreads data from the file ideal.dat and illustrates:

 How to use C++ objects to solve a simple thermodynamics problem.


 How to place constants in objects.
 How to declare and define classes.
 How to create objects.
 How to selectively skip comments in data files.
 How to extract data from files of arbitary length.

Return to Table of Contents

The program formatedio.cpp illustrates:

 Use of formated stream output.

Return to Table of Contents

The program rectangle.cpp illustrates:

 Using Horstmann's graphics classes.


 Getting mouse input.

Return to Table of Contents

Among the "advanced" C++ topics not covered in these examples are:

 Type conversion and casting.


 Multidimensional arrays.
 Loop interruption statements (break and continue).
 Separate compilation of multiple source files and use of non-standard libraries.
 Pointers and dynamic memory management (new and delete).
 User defined destructors.
 I/O redirection in DOS and UNIX.
 Bitwise operations.
 Operator overloading.
 Exception handling (try-catch blocks), but assert() is illustrated.
 Advanced object oriented design.
 Use of the Standard Template Library (STL) beyond the use of the string and vector
classes.
 Windows programming using C++ beyond using simple graphics libraries.

Send comments and suggestions about these examples to: Charles S. Tritt, Ph.D.
This page last updated 9/26/99

You might also like