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;
- g++ main1.cc illustrates class of bank account objects
including constructor and destructor methods all in one file
- 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.
- 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 .
- 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.
- g++ mainFrac.cc Fraction.cc --(uses Fraction.h )
Example of Fraction objects with numerator/denominator.
Illustrates defining operator+.
- g++ Template.cc --example of a template class
- 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.
- g++ BST.cc --Binary Search Trees - incl. Big 3 Methods
- g++ getline.cc --getline methods
- g++ vec.cc --Example of a STL vector.
- g++ list.cc --Example of a STL list.
- g++ set.cc --STL set.
- g++ map.cc --Example of a STL map.
map2.cc
- g++ sort.cc -- Illustrates generic sort algorithm on a container
- inline methods
These need std:: and various include files.
- exceptions in C++
-
- SGI examples
- copy algorithm copies anything iterable to another iterable
- g++ copyToSet.cc --copy into a set.
- Test # 1 covers above.
- C++/C reference
- 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).
- g++ virtual_pet.cc
- g++ pqVirtual.cc
STL priority_queue holding pointers to objects with virtual method.
- g++ pq.cc --
Illustrates STL priority_queue with operator< defined
- g++ rangeDemo.cc programmer defined class with iterator
- g++ ofstream.cc --output to a textfile
- g++ ifstream.cc --input from a textfile
- g++ istringstream.cc --string used for input
- g++ ostringstream.cc --string used for output
- g++ ioiterators.cc --input and output iterators.
- g++ ioLine.cc -- iterate over whole lines
- g++ ioPair.cc -- iterate over tuples
- g++ funObj.cc -- function object
g++ funObj2.cc --with generate algorithm & ostream_iterator
- g++ for_each.cc -- apply fun obj to range
- g++ for_each.cc -- no explicit loop
- g++ dijkstra.cc --Dijkstra's shortest path
- for_each and functor
- g++ map3.cc --map names into sets
g++ map4.cc --map names into count
- g++ sstream.cc --fancy conversions
- set algorithms
- g++ ioAccumulator.cc --copy all input to an object
- g++ sort2.cc -- sort with Compare function
- g++ MyClassmain.cc MyClass.cc
-- uses MyClass.h
Illustrates separate compilation of template classes.
See file separate.comp for discussion.
- g++ -o head head.cc
-- command-line arguments and input from named files.
- g++ pqComp.cc --
STL priority_queue with user-supplied compare function
- g++ slice.cc --
Illustrates slicing of extended objects to parent size and virtual.