Sample Code

Sample Code from CSS 342

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 are not significant.

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;

An example of well-documented code using pre and post conditions:   array.h     array.cpp    
An example of well-documented code not using pre and post conditions:   array.h     array.cpp    
Driver to test the code:   arraydriver.cpp    

Using valgrind - to find memory leaks and errors (done on linux machine):
      Code using a pointer with no errors:   pointergood.cpp    
      What "valgrind ./a.out" shows (what you want):   valgrind output    

      Code using a pointer with errors:   pointerbad.cpp    
      What "valgrind ./a.out" shows (the errors):   valgrind output    

      For more information on memory leaks, enter:   valgrind --leak-check=full ./a.out
      For a description of what valgrind errors mean, see valgrind explanation

Binary Search Tree:   binary search tree and insert  
      binary search tree remove  
      AVL Tree:   AVL insert  

(Please excuse the simplistic ascii UML. Sometimes it's just simpler to use text-based files. Don't use ascii UML for anything significant.)

Inheritance, Point <-- Circle <-- Cylinder   . . .   VIDEO (by me) discussing code
      Point-Circle-Cylinder UML diagram
      sample main
      Point ... point.h       point.cpp
      Circle ...       circle.h       circle.cpp
      Cylinder ...   cylindr.h       cylindr.cpp

Inheritance examples:
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 when virtual functions are used.
Fruit UML diagram

The print routine is virtual   . . .   VIDEO (by me) - virtual code
Virtual "print" is used for Apple and Orange.
Grape has no "print" routine, uses "print" of Fruit.
The code: fruit.cpp ... with output: output
Visual of List built:   Linked List of Fruit objects

The print routine is NOT virtual.
Exact same code as before except print() is not virtual,
the "print" of Fruit is used for all in the execution.
The code: fruit.cpp ... with output: output

The print routine is pure virtual   . . .   VIDEO (by me) - pure virtual code
All child classes must provide an
implementation of "print" and it is used.
All the code: fruit.cpp ... with output: output

Same code (pure virtual print),
shown in separate files with proper #includes.
main.cpp   list.h   list.cpp   fruit.h   fruit.cpp  
apple.h   apple.cpp   orange.h   orange.cpp   grape.h   grape.cpp  

A better fruit example showing how to copy   . . .   VIDEO (by me) - copying inherited objects
Fruit is an abstract base class.
Fruit and List have destructor and copy constructor.
Fruit has a virtual destructor and a pure virtual method,
clone, so the copy constructor will copy correctly.
The copy constructor and destructors print so you can follow execution.
The code: betterfruit.cpp ... with output: betterfruitoutput
Reminder of List object built:   Linked List of Fruit objects

Create new objects through a factory (also is a hash table)   . . .   VIDEO (by me) - factory code
(Wrapping a function in an object is a Functor; in this case the function creates objects.)
(An array/hash table of these create functions produces a Factory):
UML ........ factoryOLD.cpp     OLDoutput (video goes over this one)
Uses data file:   factory.txt     factory.cpp     output

Static vs. dynamic casting.
      Static cast: caststatic.cpp ... with output: caststaticoutput
      Dynamic cast: castdynamic.cpp ... with output: castdynamicoutput

Private inheritance - everything is inherited as private
      Override to public: inheritPriv.cpp ... with output: inheritPrivoutput

Some virtual/non-virtual examples (class B, derived class D)
      testF(), testG(), virtual f(), g() -- D inherits testF(), testG():   bd.cpp ... output
      testF(), virtual testG(), virtual f(), g() -- D inherits testF(), testG():   bd2.cpp ... output
      testF(), virtual testG(), virtual f(), g() -- D inherits only testF():   bd3.cpp ... output