Please send questions to
st10@humboldt.edu .
topic 1 - appending to a file
* still an ofstream
* BUT: if you call open for that ofstream with TWO
arguments, and the SECOND is the special C++ constant:
ios::app
...then you'll open the file for appending (instead of
deleting its current contents)
ofstream outStream;
outStream.open("stuff.txt", ios::app);
outStream << "next item" << endl;
outStream.close();
* example: two_writes.cpp
(an example that includes writing an integer to
one file, and appending that same integer to another)
topic 2 - another branching statement
* C++ provides a more-specialized branching statement
in addition to its if-statement: the switch statement
Like the for loop is more specialized than the while loop,
so the switch branch is more specialized than the if branch;
* IF you are choosing 0 OR 1 option from 3 or more choices,
AND
the options are expressed as one of the integer, bool, char,
or enumerated
types (the so-called integral types),
THEN you can use the switch statement, if you'd like:
* syntax:
switch(expr)
{
case value:
statement;
statement;
...
[break;]
case value:
statement;
statement;
...
[break;]
...
[default:
statement;
statement;
...]
}
* semantics: when you reach the switch, the expression
is evaluated, and compared to each case value in turn
and for the first that matches, its statements are done
(and each statement from that point on until a
break is reached to throw you out of the switch...)
IF no case's value matches the expr's value, the default's
actions are done. If there is not default in this case,
nothing is done -- you just pop out.
* describe_grade.cpp: printing a description for a char grade
* describe_grade2.cpp: doing so using a switch statement
* common pitfalls
* thou shalt not switch on a non-integral type...
(no doubles, no strings, etc.)
* thou shalt not put conditions as case values;
* thou shalt use breaks unless you want to do
all the rest of the statements in the switch
* 130lect15_lect_ex.txt - switch-practice lecture exercise
* PARAMETER PASSING in C++
...or, how does the parameter get the argument's value,
anyway?
* TWO ways of doing this in C++...
* default, for scalar values: PASS-BY-VALUE
* one function calls another,
a COPY of the argument value is sent to the called
function, and that COPY is copied into the
parameter's memory
* if you change the parameter, then, in the function,
ONLY that function's copy of the argument is nuked;
the argument itself is UNCHANGED
* if you play with looky.cpp, test_looky.cpp,
UGLYlooky.cpp, and test_UGLYlooky.cpp,
then if you UNDESTAND their output, that's a good sign
that you may be getting what's going on here;
* (and the potential for reader-confusion is why it
is considered poor style to change a pass-by-value
parameter in a function)
* BUT: what if a function NEEDS/WANTS/INTENDS to change
the argument corresponding to a parameter?
...you need to pass those parameters DIFFERENTLY
...in C++, you can use PASS-BY-REFERENCE
* in PASS-BY-REFERENCE, the function gets passed WHERE
TO FIND THE ACTUAL ARGUMENT, not a copy of its
value!
...and so the argument MUST be something that CAN
be changed, like a variable (or an array reference)
* to get pass-by-reference for a scalar, you have
to use special parameter-declaration syntax:
you put an & in the parameter declaration,
right after the type:
e.g.:
void swap(int& val1, int& val2)
to call swap:
int mine;
int yours;
mine = 15;
yours = 27;
swap(mine, yours);
* when you change a pass-by-reference parameter,
you REALLY change its argument!
* BY DEFAULT, arrays are passed by reference!
(they are NOT passed by value!!!)
you can't pass an array by value, BUT you
CAN ensure that it isn't changed by a function
by putting keyword const in front of its parameter
declaration:
void myfun(const double dont_change_me[], int size)