Sample C++ for CS3132 Everything in the C++ STL library is now in a separate namespace which means that they all need to be prefixed with "std::". For small programs it would suffice to include
using namespace std;
  1. g++ main1.cc illustrates class of bank account objects
    including constructor and destructor methods all in one file
  2. g++ main2.cc Account.cc (uses Account.h )
    This is the same program as main1 with the class moved to Account.h which has the method prototypes only and the methods are in Account.cc . The constructor in Account.cc illustrates initialization lists.
  3. g++ main3.cc NamedAcct.cc Account.cc --uses Account.h and NamedAcct.h
    Class NamedAcct is derived from class Account. ctor for the car must pass some values to ctor for Engine. The new methods are in NamedAcct.cc and the rest are inherited from Account.cc .
  4. nested.cc illustrates Engine object inside Car object
    Shows use of initialization list in c-tor.
    The above 3 examples show 3 uses of initialization lists in C++ constructors.
  5. g++ mainFrac.cc Fraction.cc --(uses Fraction.h )
    Example of Fraction objects with numerator/denominator. Illustrates defining operator+.
  6. g++ Template.cc --example of a template class
  7. g++ DEList.cc -- illustrates template mechanism
    A DELisT is a doubled-ended list which can be used as a stack or queue. These DELisT objects can hold information of almost any type T. Illustrates a struct inside a class and new/delete. This example includes the "Big 3" methods.
  8. g++ BST.cc --Binary Search Trees - incl. Big 3 Methods
  9. g++ getline.cc --getline methods
  10. g++ vec.cc --Example of a STL vector.
  11. g++ list.cc --Example of a STL list.
  12. g++ set.cc --STL set.
  13. g++ map.cc --Example of a STL map. map2.cc
  14. g++ sort.cc -- Illustrates generic sort algorithm on a container
  15. inline methods These need std:: and various include files.
  16. exceptions in C++
  17. SGI examples
  18. copy algorithm copies anything iterable to another iterable
  19. g++ copyToSet.cc --copy into a set.
  20. Test # 1 covers above.
  21. C++/C reference
  22. g++ virtual.cc -lsrgp -lX11
    A drawing is an ordered list of Shape subclass objects. Illustrates virtual methods/polymorphism/subclassing. Shows how to interface to C code (graphics library).
  23. g++ virtual_pet.cc
  24. g++ pqVirtual.cc
    STL priority_queue holding pointers to objects with virtual method.
  25. g++ pq.cc -- Illustrates STL priority_queue with operator< defined
  26. g++ rangeDemo.cc programmer defined class with iterator
  27. g++ ofstream.cc --output to a textfile
  28. g++ ifstream.cc --input from a textfile
  29. g++ istringstream.cc --string used for input
  30. g++ ostringstream.cc --string used for output
  31. g++ ioiterators.cc --input and output iterators.
  32. g++ ioLine.cc -- iterate over whole lines
  33. g++ ioPair.cc -- iterate over tuples
  34. g++ funObj.cc -- function object
    g++ funObj2.cc --with generate algorithm & ostream_iterator
  35. g++ for_each.cc -- apply fun obj to range
  36. g++ for_each.cc -- no explicit loop
  37. g++ dijkstra.cc --Dijkstra's shortest path
  38. for_each and functor
  39. g++ map3.cc --map names into sets g++ map4.cc --map names into count
  40. g++ sstream.cc --fancy conversions
  41. set algorithms
  42. g++ ioAccumulator.cc --copy all input to an object
  43. g++ sort2.cc -- sort with Compare function
  44. g++ MyClassmain.cc MyClass.cc -- uses MyClass.h
    Illustrates separate compilation of template classes. See file separate.comp for discussion.
  45. g++ -o head head.cc -- command-line arguments and input from named files.
  46. g++ pqComp.cc -- STL priority_queue with user-supplied compare function
  47. g++ slice.cc --
    Illustrates slicing of extended objects to parent size and virtual.