Sample C++ Programs

This code does NOT use C++ 11. This is simply for convenience because the machine hosting this website has an older C++ compiler. The differences between what is here and using version 11 or later versions are not significant. Here is a good explanation of initialization in C++11.

This code also always uses "using namespace std;" ... for simplicity. In general, it is best to not tie up all names, but instead individually tie an identifier to its namespace with the binary scope operator, e.g., std::cout << "hello world" << std::endl;

Structured (not OO) C++ programming examples:

Bubble sort: bubble.cpp   bubbleWithTemplate.cpp ... using data: bubble.txt ... with ... output
Reading one char at a time using get(): get.cpp
Reading one line, using get(): readArray.cpp   or using getline(): readGetline.cpp ... using data: read.txt ... with ... output
Formatting using setw(): format.cpp ... with ... output
Dynamic two dimensional array: twoDarray.cpp ... with ... output
Convert string object to char array: stringConvert.cpp
Messing around with pointer arithmetic: ptrArith.cpp
More pointer arithmetic, pointers to pointers: ptrptr.cpp
pointers to functions: ptrToFunc.cpp ... with ... output
Mess around with basic pointers, swap pointers: ptr.cpp
Example using a vector from the STL: vector.cpp
Towers of Hanoi: hanoi.cpp ... with ... execution tree
Range-based arrays (since C++11): array-range-based.cpp
Smart pointers: smart.h ... smartdriver.cpp ... valgrind output (shows no leaks)

Rational class:

Simple class, not documented, no overloading operators: rat.h ... rat.cpp ... ratdriver.cpp ... with output
Better class, documented, overload operators, etc.: rat2.h ... rat2.cpp ... rat2driver.cpp ... with output

Array class:

Simple int array class: array.h ... array.cpp ... arraydriver.cpp ... with output
Same int array class with pre/post condition comments: array.h ... array.cpp
Array class as a template (must put all code into the .h file):
    arraytemplate.h includes class def and implementation ... arraytemplatedriver.cpp ... with output

List class:

List class (singly linked list): list.h ... list.cpp ... listdriver.cpp ... listdata.txt ... listdata2.txt
List class as a template: listtemplate.h ... listtemplatedriver.cpp
Nodedata class: nodedata.h ... nodedata.cpp
Employee class: employee.h ... employee.cpp

Stack ADT: stack.h                  

Array implementation:   stacka.cpp  
Linked List implementation:   stackl.cpp  

Queue ADT:   queue.h

Array implementation:   queuea.cpp  
Linked List implementation:   queuel.cpp  


Using a static const data member: staticconst.cpp

Binary Search Tree:   binary search tree  
Tree recursion:   Binary tree problem solving (recursion)  

Sample Manager class:   for bridge game  

Inheritance example:

Simple example using inheritance ... consists of a base class (Fruit) and
three derived classes (Apple, Orange, Grape).   There is a linked list of Fruits.
The items start out as specific Apples, Oranges, or Grapes, then become Fruits in the list.
Inheritance allows the objects to define their own functionality.
All the code: fruit.cpp ... with output: output ... and UML diagram