Please send questions to
st10@humboldt.edu .
* classes can have (amongst other things!)
data fields
constructor methods
selector methods ("getters")
modifier methods ("setters")
and also... other methods!
* let's demo some other methods by adding a couple -- that
happen to be overloaded -- to boa;
how about: longer_than?
and note: need to follow the DESIGN RECIPE for these other
methods!
* include comments containing signature and purpose
before the implementation of such a method in
the class's .cpp file
* where should the tests go? I think it can work just
fine to fold these into the class test function;
// signature: boa::longer_than: boa -> bool
// purpose: expects another boa, and produces whether the
// calling boa is longer in length than the given
// ther boa
// header step...
// in boa.h
[this doesn't CHANGE the boa, so use const at its end...]
bool longer_than(boa another_boa) const;
// in boa.cpp
bool boa::longer_than(boa another_boa) const
* why not put the tests into boa_test.cpp?
add a bool variable to hold the result of these tests:
bool other_results;
use the boa's existing instances (or create others if helpful)
to test what the other method should produce
e.g.,
other_results = (carol.longer_than(george) == false) and
(george.longer_than(carol) == true) and
(george.longer_than(george) == false);
and then make sure the other_results bool is and'd with the
other result variable for the return value:
return (selector_results and modif_results and other_results);
* should be ready to add this method to boa.h and boa.cpp!
...and then re-compile boa, and then recompile boa_test, and
make sure the tests pass!
* let's make a second, overloaded longer_than that lets the
user provide simply a length, and produces whether the calling
boa's length is longer than that given length;
// signature: boa::longer_than: double -> bool
// purpose: expects a length in meters, and produces whether
// the calling boa is longer than this given length
headers:
...in boa.h:
[this doesn't CHANGE the boa, so use const at its end...]
bool longer_than(double given_length) const;
...in boa.cpp:
bool boa::longer_than(double given_length) const
...add the tests to boa_test.cpp -- I think I'll add
them (and them...) to the statement that is already
setting other_results
add the header to boa.h,
the signature, purpose, header, and body to boa.cpp
and you're ready to compile boa, boa_test, and try 'em out;
* then just discussed pass-by-value a bit more --
point to remember: in pass-by-value (call-by-value),
the argument's *value* is COPIED into the parameter's memory location;
...changing the parameter CANNOT change the calling argument;
* benefit: you can't accidentally change the calling argument;
benefit: ANY expression of the appropriate type