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):
        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

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

Inheritance, Point <-- Circle <-- Cylinder:       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.
Virtual "print" is used for Apple and Orange.
Grape has no "print" routine, uses "print" of Fruit.
The code: fruit.cpp ... with output: output


The print routine is NOT virtual.
All child classes have "print" but because it is not virtual,
the "print" of Fruit is used for all.
The code: fruit.cpp ... with output: output


The print routine is pure virtual.
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.
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


Create new objects through a factory (also is a hash table).
(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 ... factory.cpp ... with ... 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