Feeding time is normally 11:15-11:45 in JBH309.
I don't bite.... [ calendar.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!
A list of string handling functions in Standard C++: [ string ] also see stringstreams.
./example <input >output
Do this if possible!
The good news is that all input streams work like cin and all output streams like cout. All you need to learn is how to connect a stream to a file.
Here is the pattern for using a file
#include <fstream>
ifstream input;
ofstream output;
fstream input_and_output; //but not both at once
input.open("Name_of_file");
output.open("Name_of_file");
input_and_output.open("Name_of_file", ios::in); //for reading
input_and_output.open("Name_of_file", ios::out); // for writing
If the name of the file is in a string variable then you have to add ".data()" to it to extract the oldfashoined "char*" inside.
input.open(Name_of_file_as_string.data());
output.open(Name_of_file_as_string.data());
input_and_output.open(Name_of_file_as_string.data(), ios::in);
input_and_output.open(Name_of_file_as_string.data(), ios::out);
input.close();
output.close();
input_and_output.close();
input >> variable ....;inputs data upto the next space or end-of-line. Spaces are not skipped.
input.get(character_variable);Inputs the next character -- including spaces and end-of-line characters.
getline(input, string_variable);Input the next line (including white space but not the end-of-line.
output << expression <<...Evaluates the expresssion and outputs the value. You can also control the formatting of the output using the <iomanip> library.
output.put(charactar_expression);
. . . . . . . . . ( end of section Examples of code) <<Contents | End>> Older Examples of code:
Documentation on fstream: [ iomanip.html ]
(strstream): vs
(stringstream):
[ tss.cpp ]
. . . . . . . . . ( end of section Input, output, Files, and Streams) <<Contents | End>>
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..
#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
...
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.
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'.
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.txt ] We also have a downloadable UNIX manual page: [ make.man ]
. . . . . . . . . ( end of section Complex Projects) <<Contents | End>>
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>>
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 ]
Stack: [ inline_stack.cpp ]
Linked Queue: [ LinkedQueue.h ] [ LinkedQueue.cpp ] [ testLinkedQueue.cpp ]
Also see the generic forms (Templates) below.
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 ]
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 ]
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>>
General C++ sources: [ c++.html ]
(C++ Tutorial):
[ http://www.cs.uwa.edu.au/programming/c++.tutorial/ ]
[ typedef.html ]
(C++ Examples):
[ http://cse.csusb.edu/dick/examples/ ]
(USENET): Discussions from USENET News:
[ pointers.html ]
comp.lang.c++.moderated
(Standard):
[ http://cse.csusb.edu/dick/c++std/ ]
[ index.html ]
I have a sampling of other books in my office that you can look at.
The command
~dick/bin/umenuwill run a menu-driven shell that helps you do most of the things you might want to do.
. . . . . . . . . ( end of section Resources) <<Contents | End>>