type HullCharacteristics {
// Ship 1
float lbp_ship1=425.0; // length between perpendiculars
float lwl_ship1=425.0; // waterline length
float loa_ship1=425.0; // overall length
float b_ship1=57.0; // beam
float h_ship1=42.0; // draft
float d_ship1=28.0; // depth
float cb_ship1=0.775; // block coefficient
float cp_ship1=0.782; // prismatic coefficient
// Ship 2
float lbp_ship2=550.0; // length between perpendiculars
float lwl_ship2=550.0; // waterline length
float loa_ship2=562.0; // overall length
float b_ship2=60.0; // beam
float h_ship2=51.0; // draft
float d_ship2=29.0; // depth
float cb_ship2=0.773; // block coefficient
float cp_ship1=0.779; // prismatic coefficient
}
However, this approach is both tedious and error-prone (note the
"cut-and-paste" error on the last line).The concept of classes was invented to make creation of multiple objects of the same type easier. A class is both
Because a class defines a new data type, variables of that new type - such as HullCharacteristics - can be created in a manner similar to variables of language-defined types such as float. Each variable of a class type represents a different object.
The syntax for creating an object from a type depends on the OO programming language being used. In Java, for example, two objects might be created from the HullCharacteristics class as follows:
HullCharacteristics ship1, ship2; // declare the variables
ship1 = new HullCharacteristics ( // create the ship1 object
425.0, 425.0, 425.0, 57.0, 42.0, 28.0, 0.775, 0.782);
ship2 = new HullCharacteristics ( // create the ship2 object
550.0, 550.0, 562.0, 60.0, 51.0, 29.0, 0.773, 0.779);
This is much more succinct than explicitly declaring each variable of
each object. It is also more powerful notationally, for a reference to
the variable ship1 (or ship2) implicitly refers to all the
fields for ship1 declared in the HullCharacteristics class. With the
explicit approach, individual variables, such as lwl_ship1, may be
referenced, but the variables of the object may not be referenced in
the aggregate.Given an object reference variable such as ship1, individual field variables may be referenced by writing the variable name followed by a dot and the name of the desired field variable. For example,
ship1.lwl // ship1's waterline length
This syntax is often called "dot" notation, for obvious reasons.
An object's methods are also defined in the class, and referenced
using dot notation. For example,
ship1.getLWL() // ship1's getLWL() method
By now it should be no suprise that a class contains the same kinds of
information as the objects that it is used to create:
As in the case of creating objects, the syntax for creating a class depends on the OO programming language being used. In Java, the code for the HullCharacteristics class might be written as follows:
public class HullCharacteristics { // class declaration
// Field Variables:
private float lbp; // length between perpendiculars
private float lwl; // waterline length
private float loa; // overall length
private float b; // beam
private float h; // draft
private float d; // depth
private float cb; // block coefficient
private float cp; // prismatic coefficient
// Field Access Methods:
public void setLWL ( // method to set the waterline length
float lwl_value) { // argument supplying new length value
lwl = lwl_value; // set the field to the supplied value
} // end of setLWL() method
public float getLWL () { // method to get the waterline length
return lwl; // give waterline length to caller
} // end of getLWL() method
} // end of class
Note that methods to access the other fields besides lwl would normally
also be defined, but are omitted here for simplicity.A single copy of a class can be used to create as many objects of the classes' type as needed. Thus, only one copy of the class itself is needed, and only one copy of the code for the class needs to be written regardless of the number of objects it is used to create.