I don't bite.... [ plan.html ]
And if I'm not in, check any nearby open door... we all know C++ and most of us like to help you solve problem. We all enjoy sorting out confusing error messages....
Bring a printout of the code and the evidence of a problem!
Example of C++ Code for CSci202 and CSci201
An archive of C++ examples:
[ examples ]
Tutors
[ Subjects.html ]
Computer Science Club
[ http://club.csci.csusb.edu/ ]
Jokes
[ http://www.userfriendly.org/ ]
[ jokes ]
Functions
[ functions.html ]
Strings
A list of string handling functions in Standard C++:
[ string ]
also see stringstreams.
Typedef
[ typedef.html ]
Input, output, Files, and Streams
Examples of code:
Documentation on fstream:
[ iomanip.html ]
Input/Output Manipulators
[ iomanip.html ]
stringstreams
In our older(1997) C++ system(Gnu C++ vn 2.8.1) these are called 'strstreams'.
The book uses the ANSI/ISO standard library that is becoming available
on more and more C++ systems.
(strstream): vs
(stringstream):
[ tss.cpp ]
Complex Projects
The UML Component diagram is a way to think out, record, and present to others your plans for breaking a program into modules.
For example think about the <math> library: you want to know how to use parts of it but not how it is programmed. The sqrt function does square roots for you without you knowing how it does it. Similarly, we can use strings, vectors, and streams and ignore the work done to make them work correctly. Most programmers need to know how use a module but don't have the time to worry about how the module actually works. In other words a module has two views. This is like a pet owners view of a cat:
The rules for using a module form the modules specification. The content of the module (the gory details) are the modules implementation. The specification of the math library for example shows a function called "sqrt" which is called with an double argument and returns a double value:
double sqrt( double);The documentation further tells us that the argument must not be negative, and if so the returned value when multiplied by itself will be close to the value of the argument.
Some languages, Java and the UML for example, provide an interface construct that lets us specify a way in which data can be used. C however was much more direct: You list the specification in one file and put the implementation in a different file. The implementation is compiled independently of the programs that use it. The client programs (that use the module) use the #include directive to include the specification. C++ inherits C's approach: a module has two files: the specification full of function headers, and the implementation full of function bodies..
C++ Modules
In detail then: to create an independently compiled C++ module you must
create two files. One specifies how to use the module. The other
describes the implementation. In the specification you typically find:
Typical C++ Specification File
Name.h
#ifndef NAME
#define NAME
Type function ( arguments );
class Class_name {
public:
Class_name(arguments);//constructor
...
Type mutator_name(arguments);
...
Type accessor_name(arguments) const;
...
private:
Type variable;
...
}; //class Class_name
#endif
Class_name(arguments)
{
...
}//constructor
...
Type mutator_name(arguments)
{
...
}//mutator_name
...
Type accessor_name(arguments) const
{
...
}//accessor_name
...
Rules for Modules in CS202
To earn full credit for modularizing a program in CS202 each C++ module
should
appear as two source files and one compiled file. The source code should be
a header file (modulename.h) and a compilable file
(modulename.cpp). The .cpp file must include the .h file. The
implementation .cpp file must be compiled into a .o file. A program the
uses the module must #include the specification (modulename.h). It must
not include (and so recompile!) the implementation (modulename.cpp).
The specification needs to have comments to earn readability points!
The specification (*.h files) are '#include'd in the source code that needs to use them. They are compiled in and recompiled every time the programs that use them are compiled. Normally, we pre-compile the implementations (*.cpp or *.cc) files into object files (*.o) like this:
c++ -c .... class.cc
UML">
There are two exceptions to the guideline that implementation is in one file (.cpp) and specification/header are in a different one (.h). See Templates and Inline functions below.
Skeleton files
The above rules apply whether you are defining a collection of
functions, or are defining a new class. To create a separately
compiled class you need a header(specification) like this:
[ class.h ]
and a separate implementation that looks like this:
[ class.cpp ]
and defines mutators:
[ mutator.cpp ]
[ void_mutator.cpp ]
and accessors:
[ void_accessor.cpp ]
[ accessor.cpp ]
Templates
There is a dispensation from the rule of separating headers
and code files in C++ in the case of templates.
If you declare a
template function or a template class
then their complete implementation has to be
included in a program for most compilers to use it. Thus you may place
the implementations of templates inside header files. This will slow
down compilation, but may save you rewriting the code.
Inline functions
The second dispensation occurs when defining a class that has a very small
function that you wish to be executed quickly. These are called #inline
functions. They are put inside a class specification. The compiler
treats them in a special way. Instead of compiling a call, it copies them
into the code where they are called. These functions can be put in
header files. However (1) they should no more than one line long,
and (2) you should be sure that you are not going to change them.
For example, the following code is an acceptable header file for a simple counter class that will never need changing:
class Counter {
private: int i;
public: Counter(){i=0;}
int value(){return i;}
void up(){ i++; }
void down(){ i--; }
};
. . . . . . . . . ( end of section Modules) <<Contents | End>>
There are tools that can then make the development
work at the terminal simpler. These recall the way the project
is constructed and after a change reconstruct the new versions.
On UNIX the simplest of these tools is called 'make'.
Make
The UNIX 'make' program simplifies the task of rebuilding a
program when some of the files are changed. It calculates precisely
the smallest number of compilations and other operations needed
to reconstruct a new version and carries them out in the correct order.
'make' is controlled by a 'Makefile'.
Write a 'Makefile' using your favorite editor, just be careful to save it with the name 'Makefile'. The file is a text file. It is made of lots of little recipes. Each recipe starts with the name of the file to made, followed by a colon(:) and a list of ingredients. On the following lines come the instructions for making the target from the ingredients. These commands must have a Tab character in front of them. Here is a simple Makefile to help somebody do laboratory number 1:
flipper : flipper.cpp
g++ -o flipper flipper.cpp
lab01: lab01.cpp
g++ -o lab01 lab01.cpp
If you are in a directory with the above Makefile+flipper.cpp+lab01.cpp then
make lab01compiles lab01.cpp into lab01. And
make flippercompiles flipper.cpp into flipper. Further
make(with no target) picks the first target in the file: flipper.
For more about 'make' please see our introductory notes [ make.html ] and detailed documentation [ make.html ] and an advanced model makefile for typical projects: [ makefile.FAQ ] We also have a downloadable UNIX manual page: [ make.man ]
. . . . . . . . . ( end of section Complex Projects) <<Contents | End>>
Algorithms
Here are some examples from the book and other sources: [ mergeSort.h ] [ mergeSort.cpp ] [ integers2 ] [ selectSort.h ] [ selectSort.cpp ] [ iovect.h ] [ iovect.cpp ] (generic templates [ giovect.cpp ] ) [ selectMain.cpp ] [ integers1 ]
. . . . . . . . . ( end of section Algorithms) <<Contents | End>>
Pointers
Handout:
[ pointers.html ]
Example code:
[ elist1.cpp ]
, UML diagrams
[ uml1b.html ]
Example UML model of a family tree linked data structure: [ family.gif ] [ family.mdl ] with possible code: [ Person.h ] [ Family.cpp ] [ Person.cpp ] [ NewFamily.cpp ]
UML diagram of Employee Link and Employee List [ elist2.cpp ] [ elist.jpg ] You can also (shift/Click) download the Rational Rose model: [ elist.mdl ]
Linked list with cursor: [ elist3.cpp ]
Other linked lists developed from elist2: integer [ ilist.cpp ] and generic [ tilist.cpp ] [ tilist2.cpp ]
Linked Queue:
[ LinkedQueue.h ]
[ LinkedQueue.cpp ]
[ testLinkedQueue.cpp ]
Data Structures
[ linked.html ]
Stack: [ inline_stack.cpp ]
Linked Queue: [ LinkedQueue.h ] [ LinkedQueue.cpp ] [ testLinkedQueue.cpp ]
Also see the generic forms (Templates) below.
The Standard Library and Templates
Here are some notes on C++ templates.
[ Template Functions in functions ]
[ templates.html ]
Generic Linked Lists: [ tilist.cpp ] [ tilist2.cpp ]
Here is a sample generic input/output vector package. It will read and write files and cin and cout of data of any type into a vector of items of that type [ giovect.cpp ]
Notes on the STL(Standard Template Library): [ stl.html ] The STL is part of the new American and International Standard C++. Here is a copy (legal) of the draft standard: [ index.html ]
Inline Functions
[ Inline Functions in functions ]
Down loadable (shift/click!) models that can be used with Rose: [ lab01.mdl ] [ Stack.mdl ] [ elist.mdl ] [ family.mdl ] [ family.mdl ] [ mem.mdl ] [ CSciNetwork.mdl ]
Examples from text book: [ elist.mdl ] [ clock1.gif ] [ clock2.gif ]
A package defining parts of C++ that can be imported
and used in other models:
[ c++.ptl ]
Deriving new class from old ones
Examples: [ poly1.cpp ] [ poly2.cpp ] [ poly3.cpp ] [ poly4.cpp ] [ poly5.cpp ] and [ polystud.cpp ] [ polystud2.cpp ] [ polystud3.cpp ]
. . . . . . . . . ( end of section Deriving new class from old ones) <<Contents | End>>
Quizzes
[ q1a.htm ]
[ q1.cpp ]
C++
Frequently Asked Questions and Answers for C++(Lite)
[ http://www.cerfnet.com/~mpcline/C++-FAQs-Lite/ ]
General C++ sources: [ c++.html ]
(C++ Tutorial):
[ http://www.cs.uwa.edu.au/programming/c++.tutorial/ ]
[ typedef.html ]
(C++ Examples):
[ http://csci.csusb.edu/dick/examples/ ]
(USENET): Discussions from USENET News:
[ pointers.html ]
comp.lang.c++.moderated
(Standard):
[ http://www.csci.csusb.edu/dick/c++std/ ]
[ index.html ]
UNIX Books
Here is a $10 book on UNIX from a usually reliable publisher:
Daniel J Barrett, Linux pocket Guide, O'Reilly Feb 2004 $9.95 ISBN
0-596-00628-4. Look in your local Barnes and Noble or Borders
bookstore.
I have a sampling of other books in my office that you can look at.
UNIX Links
[ commands.html ]
[ unix.FAQ ]
[ CS_FAQ.html ]
The command
~dick/bin/umenuwill run a menu-driven shell that helps you do most of the things you might want to do.
Vi
[ vi ]
[ vi.FAQ ]
[ vi.commands ]
[ vi.archives ]
Software Design
[ wiki?SevenPrinciplesOfSoftwareDevelopment ]
. . . . . . . . . ( end of section Resources) <<Contents | End>>
Abbreviations