Please send questions to
st10@humboldt.edu .
CIS 130 - Week 8, Wednesday, 03-10-10
* I'd like to write a function for the area of a ring...
* (note: I HAVE the circ_area function available...!)
* OK, I know that if I'm given the ring's "outer" radius,
and the radius of its "hole", then I have everything I need
to compute its area...
* // contract: ring_area: double double -> double
* // purpose: expects the outer radius of a ring and
// the radius of its "hole", and produces
// the area of that ring
* its header:
double ring_area(double outer, double inner)
* examples:
ring_area(10, 1) == circ_area(10) - circ_area(1)
ring_area(10, 1) == 311.01741
* named constants:
...turns out, since I'm using circ_area thst already defines PI,
I *don't* need to redefine it here;
(in fact, I SHOULDN'T redefine it, because C++, like Scheme
does NOT like it if you define the same name twice in the
same scope)
* body:
{
return circ_area(outer) - circ_area(inner);
}
* Not all C++ functions involve numbers...
let's write one that just involves strings.
note: + used between two string expressions will
produce concatenation of those 2 strings
(or, one string consisting of both)
"alp" + "ha" --> "alpha"
* Let's say I'd like a function that expects two
strings, a first name and a last name, and produces
a single string that is a "pretty" form of that
full name: last_name, first_name
* contract: pretty_name: string string -> string
* purpose: expects a first name and a last name,
and it produces a single string
that is <last_name>, <first_name>
(the last name, then a comma and a blank,
and then the first name)
* header:
string pretty_name(string first, string last)
* examples:
pretty_name("Frank", "Rizzo") == "Rizzo, Frank"
* body:
{
return last + ", " + first;
}
* how about conditionals?
* the most basic C++ conditional statement is the if statement
* in Scheme:
(cond
[test1 result1]
[test2 result2]
...
[else result-else]
)
You could do the same thing as the cond above in C++ with the
following:
if (test1)
return result1;
else if (test2)
return result2;
...
else
return result_else;
or -- if you might ever do more than 1 thing because of a test...
if (test1)
{
return result1;
}
else if (test2)
{
return result2;
}
...
else
{
return result_else;
}
* CLASS CODING STYLE STANDARD:
each curly brace is on its OWN line,
lined up (the opening brace lined up with the preceding line),
with the statement(s) within indented by 3 or more spaces.
* you only NEEEED the { } when the action takes more than 1 statement
* the simplest if statement in C++:
if (test)
action;
* ...more COURSE STYLE: the action for an if is on its OWN line,
even if we don't use the curly braces...
* semantics:
if test is true, do action;
otherwise, just skip the action and go on.
* the else is optional:
if (test)
if_action;
else
else_action;
* COURSE STYLE: the else's action should be indented
also! (even if no curly braces)
if (test)
{
action1;
action2;
}
if (test)
{
action1;
action2;
}
else
{
action3;
action4;
action5;
}
...I'll do action1 and action2,
OR action3, action4, action5 --- NEVER all 5!!!
* MORE STYLE: WHEN it is a choice of 3 or more options,
use the style we first demonstration:
if (test1)
action;
else if (test2)
action2;
else if (test3)
action3;
...
else
action_else;