Please send questions to
st10@humboldt.edu .
* a Racket struct allowed us to define our own
custom data types;
...with data fields I want such data to have, and
with its own operations, if you will, for
creating instances of that type (the constructor)
and for operating on data of that type;
* a C++ class allows us to define our own custom
data types;
......with data fields I want such data to have,
and with its own operations, called METHODS,
for creating instances of that type (constructor
method) -- and such an instance of a class is
called an OBJECT -- and with its own operations,
or methods, for operating on data of that type;
* a C++ class is more general than a Racket struct;
it is general, it powerful, so you get to specify
it all yourself...
* in Racket, the constructor always had the same
name, make-<struct_name>
in C++, there may be more than one constructor
method, and ALL of them have the same name --
the name of the class.
So -- if I have a C++ class named boa,
all of its constructor methods are named boa
* we created a new Racket boa with:
(make-boa "blue" 7.5 "beef-jerky")
you can dynamically-declare a new boa instance
using
new boa("blue", 7.5, "beef-jerky")
(that's an expression)
you can statically-declare a boa using
boa harold("green", 7.5, "donuts");
(that's a statement)
* Racket's selectors ALWAYS had the name
<struct-name>-<datafield-name>
boa-color
C++ selector methods have the name you give them,
BUT our CLASS STYLE STANDARD is to name these
with get followed by what you're getting --
for boa, either: get_color, get_length, get_food
OR getColor, getLength, getFood
* you called a Racket selector function for
a struct like so:
(define HAROLD (make-boa "green" 9.8 "donuts"))
(boa-color HAROLD) --> "green"
You call a C++ method in at least two ways:
IF it is like the statically-declared harold
above,
I put the name of the C++ object,
a period,
the name of the method,
and its arguments in parentheses
(no arguments? you still gotta put empty parentheses!!)
harold.get_color( ) // this has value "green"
harold.get_length( ) // this has the value 9.8
harold.get_food( ) // this has the value "donuts"
We will also find it occasionally useful to
be able to call a method for a dynamically-created
boa --
* you need to write the dynamic-creation expression
in parentheses,
* and follow it with -> and then the method name
and arguments
(new boa("blue", 7.5, "beef-jerky"))->get_food()
// has the value "beef-jerky"
* for a Racket struct boa, we wrote:
(define-struct boa (color length food))
for a C++ class boa, I create 2 files --
I type them in, say using nano or a text
editor of your choice...
I put its class definition in a file named boa.h
and its class implementation in a file named boa.cpp
* START with a comment -- a data definition comment,
both to help you clarify what you're doing
and to serve as documentation for the future:
/*
a boa is a class
new boa(string color, double length,
string food)
...representing a boa with:
a color that is its primary coloring,
a length in meters,
a preferred food
template for a function with a boa parameter a_boa:
ret_type process_boa(boa a_boa)
{
return ... a_boa.get_color() ...
... a_boa.get_length() ...
... a_boa.get_food() ... ;
}
*/
NOW: see boa.h
and boa.cpp (just started so far...)